@@ -111,58 +111,61 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &Micro
111111 settings. user_site_directory = false ;
112112
113113 let builder = Interpreter :: builder ( settings) ;
114- let defs = rustpython_stdlib:: stdlib_module_defs ( & builder. ctx ) ;
115- let interp = builder. add_native_modules ( & defs) . build ( ) ;
116- interp. enter ( |vm| {
117- let setup_code = vm
118- . compile ( & bench. setup , Mode :: Exec , & bench. name )
119- . expect ( "Error compiling setup code" ) ;
120- let bench_code = vm
121- . compile ( & bench. code , Mode :: Exec , & bench. name )
122- . expect ( "Error compiling bench code" ) ;
123-
124- let bench_func = |scope| {
125- let res: PyResult = vm. run_code_obj ( bench_code. clone ( ) , scope) ;
126- vm. unwrap_pyresult ( res) ;
127- } ;
114+ let defs = rustpython_stdlib:: stdlib_module_defs ( unsafe { builder. context ( ) } ) ;
115+ let interp = unsafe { builder. add_native_modules ( & defs) } . build ( ) ;
116+ // SAFETY: benchmark scopes and code objects are released before leaving.
117+ unsafe {
118+ interp. enter_unchecked ( |vm| {
119+ let setup_code = vm
120+ . compile ( & bench. setup , Mode :: Exec , & bench. name )
121+ . expect ( "Error compiling setup code" ) ;
122+ let bench_code = vm
123+ . compile ( & bench. code , Mode :: Exec , & bench. name )
124+ . expect ( "Error compiling bench code" ) ;
125+
126+ let bench_func = |scope| {
127+ let res: PyResult = vm. run_code_obj ( bench_code. clone ( ) , scope) ;
128+ vm. unwrap_pyresult ( res) ;
129+ } ;
128130
129- let bench_setup = |iterations| {
130- let scope = vm. new_scope_with_builtins ( ) ;
131- if let Some ( idx) = iterations {
131+ let bench_setup = |iterations| {
132+ let scope = vm. new_scope_with_builtins ( ) ;
133+ if let Some ( idx) = iterations {
134+ scope
135+ . locals
136+ . as_ref ( )
137+ . expect ( "new_scope_with_builtins always provides locals" )
138+ . as_object ( )
139+ . set_item ( "ITERATIONS" , vm. new_pyobj ( idx) , vm)
140+ . expect ( "Error adding ITERATIONS local variable" ) ;
141+ }
142+ let setup_result = vm. run_code_obj ( setup_code. clone ( ) , scope. clone ( ) ) ;
143+ vm. unwrap_pyresult ( setup_result) ;
132144 scope
133- . locals
134- . as_ref ( )
135- . expect ( "new_scope_with_builtins always provides locals" )
136- . as_object ( )
137- . set_item ( "ITERATIONS" , vm. new_pyobj ( idx) , vm)
138- . expect ( "Error adding ITERATIONS local variable" ) ;
139- }
140- let setup_result = vm. run_code_obj ( setup_code. clone ( ) , scope. clone ( ) ) ;
141- vm. unwrap_pyresult ( setup_result) ;
142- scope
143- } ;
145+ } ;
144146
145- if bench. iterate {
146- for idx in iteration_counts ( ) {
147- group. throughput ( Throughput :: Elements ( idx as u64 ) ) ;
148- group. bench_with_input (
149- BenchmarkId :: new ( "rustpython" , & bench. name ) ,
150- & idx,
151- |b, idx| {
152- b. iter_batched (
153- || bench_setup ( Some ( * idx) ) ,
154- bench_func,
155- BatchSize :: LargeInput ,
156- ) ;
157- } ,
158- ) ;
147+ if bench. iterate {
148+ for idx in iteration_counts ( ) {
149+ group. throughput ( Throughput :: Elements ( idx as u64 ) ) ;
150+ group. bench_with_input (
151+ BenchmarkId :: new ( "rustpython" , & bench. name ) ,
152+ & idx,
153+ |b, idx| {
154+ b. iter_batched (
155+ || bench_setup ( Some ( * idx) ) ,
156+ bench_func,
157+ BatchSize :: LargeInput ,
158+ ) ;
159+ } ,
160+ ) ;
161+ }
162+ } else {
163+ group. bench_function ( BenchmarkId :: new ( "rustpython" , & bench. name ) , move |b| {
164+ b. iter_batched ( || bench_setup ( None ) , bench_func, BatchSize :: LargeInput ) ;
165+ } ) ;
159166 }
160- } else {
161- group. bench_function ( BenchmarkId :: new ( "rustpython" , & bench. name ) , move |b| {
162- b. iter_batched ( || bench_setup ( None ) , bench_func, BatchSize :: LargeInput ) ;
163- } ) ;
164- }
165- } )
167+ } )
168+ }
166169}
167170
168171/// `true` when the benchmarks are executed by the CodSpeed runner.
0 commit comments