@@ -4,7 +4,7 @@ use alloc::sync::Arc;
44use alloc:: vec:: Vec ;
55use core:: ops:: Range ;
66use tinywasm_types:: ValueCounts ;
7- use wasmparser:: { FuncValidatorAllocations , ValidatorResources } ;
7+ use wasmparser:: { FuncValidatorAllocations , OperatorsReaderAllocations , ValidatorResources } ;
88
99pub ( crate ) enum FunctionBodyInput < ' a > {
1010 Borrowed ( wasmparser:: FunctionBody < ' a > ) ,
@@ -22,41 +22,37 @@ pub(crate) struct OwnedFunctionBody {
2222pub ( 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
3843pub ( 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
4650fn 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
6258fn 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