@@ -45,11 +45,8 @@ fn bench_cpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &MicroBen
4545 pyo3:: types:: PyModule :: import ( py, "builtins" ) . expect ( "Failed to import builtins" ) ;
4646 let exec = builtins. getattr ( "exec" ) . expect ( "no exec in builtins" ) ;
4747
48- let bench_func = |( globals, locals) : & mut (
49- pyo3:: Bound < ' _ , pyo3:: types:: PyDict > ,
50- pyo3:: Bound < ' _ , pyo3:: types:: PyDict > ,
51- ) | {
52- let res = exec. call ( ( & code, & * globals, & * locals) , None ) ;
48+ let bench_func = |globals : & mut pyo3:: Bound < ' _ , pyo3:: types:: PyDict > | {
49+ let res = exec. call ( ( & code, & * globals) , None ) ;
5350 if let Err ( e) = res {
5451 e. print ( py) ;
5552 panic ! ( "Error running microbenchmark" )
@@ -58,23 +55,24 @@ fn bench_cpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &MicroBen
5855
5956 let bench_setup = |iterations| {
6057 let globals = pyo3:: types:: PyDict :: new ( py) ;
61- let locals = pyo3 :: types :: PyDict :: new ( py ) ;
58+ globals . set_item ( "__name__" , "__main__" ) . unwrap ( ) ;
6259 if let Some ( idx) = iterations {
6360 globals. set_item ( "ITERATIONS" , idx) . unwrap ( ) ;
6461 }
6562
66- let res = exec. call ( ( & setup_code, & globals, & locals ) , None ) ;
63+ let res = exec. call ( ( & setup_code, & globals) , None ) ;
6764 if let Err ( e) = res {
6865 e. print ( py) ;
6966 panic ! ( "Error running microbenchmark setup code" )
7067 }
71- ( globals, locals )
68+ globals
7269 } ;
7370
7471 if bench. iterate {
7572 for idx in iteration_counts ( ) {
7673 group. throughput ( Throughput :: Elements ( idx as u64 ) ) ;
77- group. bench_with_input ( BenchmarkId :: new ( "cpython" , & bench. name ) , & idx, |b, idx| {
74+ let id = BenchmarkId :: new ( "cpython" , format ! ( "{}[{idx}]" , bench. name) ) ;
75+ group. bench_with_input ( id, & idx, |b, idx| {
7876 b. iter_batched_ref (
7977 || bench_setup ( Some ( * idx) ) ,
8078 bench_func,
@@ -128,6 +126,10 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &Micro
128126
129127 let bench_setup = |iterations| {
130128 let scope = vm. new_scope_with_builtins ( ) ;
129+ scope
130+ . globals
131+ . set_item ( "__name__" , vm. ctx . new_str ( "__main__" ) . into ( ) , vm)
132+ . unwrap ( ) ;
131133 if let Some ( idx) = iterations {
132134 scope
133135 . locals
@@ -145,17 +147,18 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &Micro
145147 if bench. iterate {
146148 for idx in iteration_counts ( ) {
147149 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- ) ;
150+ let name = if is_codspeed ( ) {
151+ bench. name . clone ( )
152+ } else {
153+ format ! ( "{}[{idx}]" , bench. name)
154+ } ;
155+ group. bench_with_input ( BenchmarkId :: new ( "rustpython" , name) , & idx, |b, idx| {
156+ b. iter_batched (
157+ || bench_setup ( Some ( * idx) ) ,
158+ bench_func,
159+ BatchSize :: LargeInput ,
160+ ) ;
161+ } ) ;
159162 }
160163 } else {
161164 group. bench_function ( BenchmarkId :: new ( "rustpython" , & bench. name ) , move |b| {
0 commit comments