Skip to content

Commit 4d66c97

Browse files
perf: improve parser performance
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 957b62b commit 4d66c97

3 files changed

Lines changed: 120 additions & 111 deletions

File tree

‎crates/parser/src/conversion.rs‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result<Arc<FuncTy
180180
ty.composite_type
181181
)));
182182
};
183-
let params: Vec<_> = ty.params().iter().map(convert_valtype).collect();
184-
let params = params.into_iter().collect::<Result<Vec<_>>>()?;
183+
let params = ty.params().iter().map(convert_valtype).collect::<Result<Vec<_>>>()?;
185184
let results = ty.results().iter().map(convert_valtype).collect::<Result<Vec<_>>>()?;
186185
Ok(FuncType::new(&params, &results).into())
187186
}
@@ -209,17 +208,20 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> Result<WasmType>
209208
}
210209

211210
pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[ConstInstruction]>> {
212-
let ops = ops.into_iter().collect::<wasmparser::Result<Vec<_>>>()?;
213-
// In practice, the len can never be something other than 2,
214-
// but we'll keep this here since it's part of the spec
215-
// Invalid modules will be rejected by the validator anyway (there are also tests for this in the testsuite)
216-
debug_assert!(ops.len() >= 2);
217-
debug_assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
211+
let mut out = Vec::new();
212+
let mut operator_count = 0;
213+
let mut end_reached = false;
214+
215+
for op in ops {
216+
let op = op?;
217+
operator_count += 1;
218+
if matches!(op, wasmparser::Operator::End) {
219+
end_reached = true;
220+
break;
221+
}
218222

219-
let mut out = Vec::with_capacity(ops.len().saturating_sub(1));
220-
for op in ops.iter().take(ops.len() - 1) {
221223
let instr = match op {
222-
wasmparser::Operator::RefNull { hty } => match convert_heaptype(*hty)? {
224+
wasmparser::Operator::RefNull { hty } => match convert_heaptype(hty)? {
223225
WasmType::RefFunc => ConstInstruction::RefFunc(None),
224226
WasmType::RefExtern => ConstInstruction::RefExtern(None),
225227
other => {
@@ -228,13 +230,13 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
228230
)));
229231
}
230232
},
231-
wasmparser::Operator::RefFunc { function_index } => ConstInstruction::RefFunc(Some(*function_index)),
232-
wasmparser::Operator::I32Const { value } => ConstInstruction::I32Const(*value),
233-
wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(*value),
233+
wasmparser::Operator::RefFunc { function_index } => ConstInstruction::RefFunc(Some(function_index)),
234+
wasmparser::Operator::I32Const { value } => ConstInstruction::I32Const(value),
235+
wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(value),
234236
wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())),
235237
wasmparser::Operator::F64Const { value } => ConstInstruction::F64Const(f64::from_bits(value.bits())),
236238
wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(*value.bytes()),
237-
wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(*global_index),
239+
wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(global_index),
238240
wasmparser::Operator::I32Add => ConstInstruction::I32Add,
239241
wasmparser::Operator::I32Sub => ConstInstruction::I32Sub,
240242
wasmparser::Operator::I32Mul => ConstInstruction::I32Mul,
@@ -250,6 +252,10 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
250252
out.push(instr);
251253
}
252254

255+
if operator_count < 2 || !end_reached {
256+
return Err(crate::ParseError::Other("constant expression did not end correctly".into()));
257+
}
258+
253259
Ok(out.into_boxed_slice())
254260
}
255261

‎crates/parser/src/lib.rs‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Parser {
177177
#[cfg(feature = "std")]
178178
fn read_more(stream: &mut impl std::io::Read, buffer: &mut alloc::vec::Vec<u8>, hint: usize) -> Result<usize> {
179179
let len = buffer.len();
180-
buffer.extend((0..hint).map(|_| 0u8));
180+
buffer.resize(len + hint, 0);
181181
let read_bytes = stream
182182
.read(&mut buffer[len..])
183183
.map_err(|e| ParseError::Other(alloc::format!("Error reading from stream: {e}")))?;
@@ -227,10 +227,16 @@ impl Parser {
227227
let mut buffer = alloc::vec::Vec::new();
228228
let mut parser = wasmparser::Parser::new(0);
229229
let mut eof = false;
230+
let mut buffer_offset = 0;
230231

231232
loop {
232-
match parser.parse(&buffer, eof)? {
233+
match parser.parse(&buffer[buffer_offset..], eof)? {
233234
wasmparser::Chunk::NeedMoreData(hint) => {
235+
if buffer_offset != 0 {
236+
buffer.copy_within(buffer_offset.., 0);
237+
buffer.truncate(buffer.len() - buffer_offset);
238+
buffer_offset = 0;
239+
}
234240
let read_bytes = Self::read_more(&mut stream, &mut buffer, hint as usize)?;
235241
eof = read_bytes == 0;
236242
}
@@ -263,30 +269,31 @@ impl Parser {
263269
reader.process_payload(payload, validator.as_mut())?;
264270
}
265271
}
266-
buffer.drain(..consumed);
272+
buffer_offset += consumed;
267273

268274
#[cfg(parallel_parser)]
269275
if let Some((count, body_offset, section_size)) = deferred_code_section {
270-
while buffer.len() < section_size {
271-
let remaining = section_size - buffer.len();
276+
while buffer.len() - buffer_offset < section_size {
277+
let remaining = section_size - (buffer.len() - buffer_offset);
272278
let read_bytes = Self::read_more(&mut stream, &mut buffer, remaining)?;
273279
if read_bytes == 0 {
274280
return Err(ParseError::ParseError {
275281
message: "unexpected end-of-file".into(),
276-
offset: body_offset + buffer.len(),
282+
offset: body_offset + buffer.len() - buffer_offset,
277283
});
278284
}
279285
}
280286

281-
let section_bytes = alloc::sync::Arc::<[u8]>::from(buffer[..section_size].to_vec());
287+
let section_end = buffer_offset + section_size;
288+
let section_bytes = alloc::sync::Arc::<[u8]>::from(buffer[buffer_offset..section_end].to_vec());
282289
reader.queue_owned_code_section(count, body_offset, section_bytes, validator.as_mut())?;
283290
parser.skip_section();
284-
buffer.drain(..section_size);
291+
buffer_offset = section_end;
285292
continue;
286293
}
287294

288295
if reader.end_reached {
289-
if !buffer.is_empty() {
296+
if buffer_offset != buffer.len() {
290297
return Err(ParseError::Other("trailing bytes after end of module".into()));
291298
}
292299

‎crates/parser/src/parallel.rs‎

Lines changed: 83 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloc::sync::Arc;
44
use alloc::vec::Vec;
55
use core::ops::Range;
66
use tinywasm_types::ValueCounts;
7-
use wasmparser::{FuncValidatorAllocations, ValidatorResources};
7+
use wasmparser::{FuncValidatorAllocations, OperatorsReaderAllocations, ValidatorResources};
88

99
pub(crate) enum FunctionBodyInput<'a> {
1010
Borrowed(wasmparser::FunctionBody<'a>),
@@ -22,41 +22,37 @@ pub(crate) struct OwnedFunctionBody {
2222
pub(crate) struct PendingFunction<'a> {
2323
pub ordinal: usize,
2424
pub results: ValueCounts,
25-
pub ty_idx: u32,
2625
pub func_to_validate: Option<wasmparser::FuncToValidate<ValidatorResources>>,
26+
pub ty_idx: u32,
2727
pub body: FunctionBodyInput<'a>,
2828
}
2929

30-
pub(crate) const MIN_FUNCTIONS: usize = 8;
31-
const MIN_CODE_SECTION_BYTES: usize = 32 * 1024;
32-
const MIN_FUNCTION_BODY_BYTES: usize = 4;
30+
const MIN_FUNCTIONS: usize = 8;
31+
const MIN_CODE_SECTION_BYTES: usize = 16 * 1024;
32+
const MIN_FUNCTION_BODY_BYTES: usize = 16;
33+
const MIN_FUNCTIONS_PER_WORKER: usize = 4;
34+
const MAX_WORKERS: usize = 12;
3335

34-
pub(crate) fn should_parallelize_function(body_len: usize) -> bool {
35-
body_len >= MIN_FUNCTION_BODY_BYTES
36+
fn body_len(body: &FunctionBodyInput<'_>) -> usize {
37+
match body {
38+
FunctionBodyInput::Borrowed(func) => func.as_bytes().len(),
39+
FunctionBodyInput::Owned(body) => body.body_range.len(),
40+
}
3641
}
3742

3843
pub(crate) fn should_use_parallel(options: &ParserOptions, num_functions: usize, code_section_bytes: usize) -> bool {
39-
if num_functions < MIN_FUNCTIONS || code_section_bytes < MIN_CODE_SECTION_BYTES {
40-
return false;
41-
}
42-
43-
worker_count(options, num_functions) > 1
44+
num_functions >= MIN_FUNCTIONS
45+
&& code_section_bytes >= MIN_CODE_SECTION_BYTES
46+
&& code_section_bytes / num_functions >= MIN_FUNCTION_BODY_BYTES
47+
&& worker_count(options, num_functions) > 1
4448
}
4549

4650
fn worker_count(options: &ParserOptions, num_functions: usize) -> usize {
4751
let requested = options
4852
.parser_threads()
4953
.unwrap_or_else(|| std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1))
5054
.max(1);
51-
52-
requested.min(num_functions).max(1)
53-
}
54-
55-
fn body_len(body: &FunctionBodyInput<'_>) -> usize {
56-
match body {
57-
FunctionBodyInput::Borrowed(func) => func.as_bytes().len(),
58-
FunctionBodyInput::Owned(body) => body.body_range.len(),
59-
}
55+
requested.min(MAX_WORKERS).min(num_functions.div_ceil(MIN_FUNCTIONS_PER_WORKER)).max(1)
6056
}
6157

6258
fn process_function_job(
@@ -65,16 +61,18 @@ fn process_function_job(
6561
options: &ParserOptions,
6662
imported_func_count: usize,
6763
imported_memory_count: u32,
68-
) -> Result<(usize, FunctionCode)> {
69-
let validator = job.func_to_validate.map(|func| func.into_validator(FuncValidatorAllocations::default()));
70-
let (code, _, _) = match job.body {
64+
validator_allocs: Option<FuncValidatorAllocations>,
65+
reader_allocs: OperatorsReaderAllocations,
66+
) -> Result<(FunctionCode, Option<FuncValidatorAllocations>, OperatorsReaderAllocations)> {
67+
let validator = job.func_to_validate.map(|func| func.into_validator(validator_allocs.unwrap_or_default()));
68+
let (code, validator_allocs, reader_allocs) = match job.body {
7169
FunctionBodyInput::Borrowed(func) => {
72-
conversion::convert_module_code(func, validator, Default::default(), metadata, job.ty_idx)?
70+
conversion::convert_module_code(func, validator, reader_allocs, metadata, job.ty_idx)?
7371
}
7472
FunctionBodyInput::Owned(body) => {
7573
let reader = wasmparser::BinaryReader::new(&body.section_bytes[body.body_range], body.body_offset);
7674
let func = wasmparser::FunctionBody::new(reader);
77-
conversion::convert_module_code(func, validator, Default::default(), metadata, job.ty_idx)?
75+
conversion::convert_module_code(func, validator, reader_allocs, metadata, job.ty_idx)?
7876
}
7977
};
8078

@@ -86,79 +84,77 @@ fn process_function_job(
8684
imported_memory_count,
8785
)?;
8886

89-
Ok((job.ordinal, code))
87+
Ok((code, validator_allocs, reader_allocs))
9088
}
9189

92-
pub(crate) fn process_pending(
93-
pending: Vec<PendingFunction<'_>>,
90+
fn process_chunk<'a>(
91+
jobs: impl IntoIterator<Item = PendingFunction<'a>>,
9492
metadata: &crate::visit::ModuleMetadata,
9593
options: &ParserOptions,
9694
imported_func_count: usize,
9795
imported_memory_count: u32,
9896
) -> Result<Vec<FunctionCode>> {
99-
let (small_jobs, large_jobs): (Vec<_>, Vec<_>) =
100-
pending.into_iter().partition(|job| !should_parallelize_function(body_len(&job.body)));
97+
let mut validator_allocs = None;
98+
let mut reader_allocs = OperatorsReaderAllocations::default();
99+
let jobs = jobs.into_iter();
100+
let mut codes = Vec::with_capacity(jobs.size_hint().0);
101+
102+
for job in jobs {
103+
let (code, next_validator_allocs, next_reader_allocs) = process_function_job(
104+
job,
105+
metadata,
106+
options,
107+
imported_func_count,
108+
imported_memory_count,
109+
validator_allocs,
110+
reader_allocs,
111+
)?;
112+
codes.push(code);
113+
validator_allocs = next_validator_allocs;
114+
reader_allocs = next_reader_allocs;
115+
}
101116

102-
let mut codes = small_jobs
103-
.into_iter()
104-
.map(|job| process_function_job(job, metadata, options, imported_func_count, imported_memory_count))
105-
.collect::<Result<Vec<_>>>()?;
117+
Ok(codes)
118+
}
106119

107-
let num_workers = worker_count(options, large_jobs.len());
120+
pub(crate) fn process_pending(
121+
pending: Vec<PendingFunction<'_>>,
122+
metadata: &crate::visit::ModuleMetadata,
123+
options: &ParserOptions,
124+
imported_func_count: usize,
125+
imported_memory_count: u32,
126+
) -> Result<Vec<FunctionCode>> {
127+
let num_workers = worker_count(options, pending.len());
108128
if num_workers == 1 {
109-
codes.extend(
110-
large_jobs
111-
.into_iter()
112-
.map(|job| process_function_job(job, metadata, options, imported_func_count, imported_memory_count))
113-
.collect::<Result<Vec<_>>>()?,
114-
);
115-
} else {
116-
let chunk_size = large_jobs.len().div_ceil(num_workers);
117-
let chunks = {
118-
let mut chunks = Vec::with_capacity(num_workers);
119-
let mut iter = large_jobs.into_iter();
120-
while let Some(first) = iter.next() {
121-
let mut chunk = alloc::vec![first];
122-
for _ in 1..chunk_size {
123-
match iter.next() {
124-
Some(job) => chunk.push(job),
125-
None => break,
126-
}
127-
}
128-
chunks.push(chunk);
129+
return process_chunk(pending, metadata, options, imported_func_count, imported_memory_count);
130+
}
131+
let code_count = pending.len();
132+
let chunk_size = pending.len().div_ceil(num_workers);
133+
let chunk_bytes = pending.iter().map(|job| body_len(&job.body)).sum::<usize>().div_ceil(num_workers);
134+
std::thread::scope(|scope| {
135+
let mut jobs = pending.into_iter();
136+
let mut handles = Vec::with_capacity(num_workers);
137+
while let Some(first) = jobs.next() {
138+
let mut chunk = Vec::with_capacity(chunk_size);
139+
let mut bytes = body_len(&first.body);
140+
chunk.push(first);
141+
while bytes < chunk_bytes
142+
&& let Some(job) = jobs.next()
143+
{
144+
bytes += body_len(&job.body);
145+
chunk.push(job);
129146
}
130-
chunks
131-
};
132-
133-
let results: Vec<Result<(usize, FunctionCode)>> = std::thread::scope(|s| {
134-
let handles: Vec<_> = chunks
135-
.into_iter()
136-
.map(|chunk| {
137-
s.spawn(move || {
138-
chunk
139-
.into_iter()
140-
.map(|job| {
141-
process_function_job(job, metadata, options, imported_func_count, imported_memory_count)
142-
})
143-
.collect::<Vec<_>>()
144-
})
145-
})
146-
.collect();
147-
148147
handles
149-
.into_iter()
150-
.flat_map(|handle| match handle.join() {
151-
Ok(results) => results,
152-
Err(_) => alloc::vec![Err(ParseError::Other("worker thread panicked".into()))],
153-
})
154-
.collect()
155-
});
156-
157-
for result in results {
158-
codes.push(result?);
148+
.push(scope.spawn(move || {
149+
process_chunk(chunk, metadata, options, imported_func_count, imported_memory_count)
150+
}));
159151
}
160-
}
161152

162-
codes.sort_by_key(|(ordinal, _)| *ordinal);
163-
Ok(codes.into_iter().map(|(_, code)| code).collect())
153+
let mut codes = Vec::with_capacity(code_count);
154+
for handle in handles {
155+
let chunk = handle.join().map_err(|_| ParseError::Other("worker thread panicked".into()))??;
156+
codes.extend(chunk);
157+
}
158+
Ok(codes)
159+
})
164160
}

0 commit comments

Comments
 (0)