11use criterion:: measurement:: WallTime ;
22use criterion:: {
3- criterion_group, criterion_main, Bencher , BenchmarkGroup , BenchmarkId , Criterion , Throughput ,
3+ black_box, criterion_group, criterion_main, Bencher , BenchmarkGroup , BenchmarkId , Criterion ,
4+ Throughput ,
45} ;
56use rustpython_compiler:: Mode ;
67use rustpython_parser:: ast;
78use rustpython_parser:: Parse ;
8- use rustpython_vm:: { Interpreter , PyResult } ;
9+ use rustpython_vm:: { Interpreter , PyResult , Settings } ;
910use std:: collections:: HashMap ;
1011use std:: path:: Path ;
1112
1213fn bench_cpython_code ( b : & mut Bencher , source : & str ) {
13- let gil = cpython:: Python :: acquire_gil ( ) ;
14- let python = gil. python ( ) ;
15-
16- b. iter ( || {
17- let res: cpython:: PyResult < ( ) > = python. run ( source, None , None ) ;
18- if let Err ( e) = res {
19- e. print ( python) ;
20- panic ! ( "Error running source" )
21- }
22- } ) ;
14+ pyo3:: Python :: with_gil ( |py| {
15+ b. iter ( || {
16+ let module =
17+ pyo3:: types:: PyModule :: from_code ( py, source, "" , "" ) . expect ( "Error running source" ) ;
18+ black_box ( module) ;
19+ } )
20+ } )
2321}
2422
2523fn bench_rustpy_code ( b : & mut Bencher , name : & str , source : & str ) {
2624 // NOTE: Take long time.
27- Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
25+ let mut settings = Settings :: default ( ) ;
26+ settings. path_list . push ( "Lib/" . to_string ( ) ) ;
27+ settings. dont_write_bytecode = true ;
28+ settings. no_user_site = true ;
29+ Interpreter :: without_stdlib ( settings) . enter ( |vm| {
2830 // Note: bench_cpython is both compiling and executing the code.
2931 // As such we compile the code in the benchmark loop as well.
3032 b. iter ( || {
@@ -36,16 +38,12 @@ fn bench_rustpy_code(b: &mut Bencher, name: &str, source: &str) {
3638 } )
3739}
3840
39- pub fn benchmark_file_execution (
40- group : & mut BenchmarkGroup < WallTime > ,
41- name : & str ,
42- contents : & String ,
43- ) {
41+ pub fn benchmark_file_execution ( group : & mut BenchmarkGroup < WallTime > , name : & str , contents : & str ) {
4442 group. bench_function ( BenchmarkId :: new ( name, "cpython" ) , |b| {
45- bench_cpython_code ( b, & contents)
43+ bench_cpython_code ( b, contents)
4644 } ) ;
4745 group. bench_function ( BenchmarkId :: new ( name, "rustpython" ) , |b| {
48- bench_rustpy_code ( b, name, & contents)
46+ bench_rustpy_code ( b, name, contents)
4947 } ) ;
5048}
5149
@@ -55,44 +53,20 @@ pub fn benchmark_file_parsing(group: &mut BenchmarkGroup<WallTime>, name: &str,
5553 b. iter ( || ast:: Suite :: parse ( contents, name) . unwrap ( ) )
5654 } ) ;
5755 group. bench_function ( BenchmarkId :: new ( "cpython" , name) , |b| {
58- let gil = cpython:: Python :: acquire_gil ( ) ;
59- let py = gil. python ( ) ;
60-
61- let code = std:: ffi:: CString :: new ( contents) . unwrap ( ) ;
62- let fname = cpython:: PyString :: new ( py, name) ;
63-
64- b. iter ( || parse_program_cpython ( py, & code, & fname) )
56+ pyo3:: Python :: with_gil ( |py| {
57+ let builtins =
58+ pyo3:: types:: PyModule :: import ( py, "builtins" ) . expect ( "Failed to import builtins" ) ;
59+ let compile = builtins. getattr ( "compile" ) . expect ( "no compile in builtins" ) ;
60+ b. iter ( || {
61+ let x = compile
62+ . call1 ( ( contents, name, "exec" ) )
63+ . expect ( "Failed to parse code" ) ;
64+ black_box ( x) ;
65+ } )
66+ } )
6567 } ) ;
6668}
6769
68- fn parse_program_cpython (
69- py : cpython:: Python < ' _ > ,
70- code : & std:: ffi:: CStr ,
71- fname : & cpython:: PyString ,
72- ) {
73- extern "C" {
74- fn PyArena_New ( ) -> * mut python3_sys:: PyArena ;
75- fn PyArena_Free ( arena : * mut python3_sys:: PyArena ) ;
76- }
77- use cpython:: PythonObject ;
78- let fname = fname. as_object ( ) ;
79- unsafe {
80- let arena = PyArena_New ( ) ;
81- assert ! ( !arena. is_null( ) ) ;
82- let ret = python3_sys:: PyParser_ASTFromStringObject (
83- code. as_ptr ( ) as _ ,
84- fname. as_ptr ( ) ,
85- python3_sys:: Py_file_input ,
86- std:: ptr:: null_mut ( ) ,
87- arena,
88- ) ;
89- if ret. is_null ( ) {
90- cpython:: PyErr :: fetch ( py) . print ( py) ;
91- }
92- PyArena_Free ( arena) ;
93- }
94- }
95-
9670pub fn benchmark_pystone ( group : & mut BenchmarkGroup < WallTime > , contents : String ) {
9771 // Default is 50_000. This takes a while, so reduce it to 30k.
9872 for idx in ( 10_000 ..=30_000 ) . step_by ( 10_000 ) {
0 commit comments