Skip to content

Commit 1005577

Browse files
authored
Merge pull request RustPython#5163 from dchiquito/fix-benches
Fix benchmarks by replacing rust-cpython with pyo3 - Replace `cpython` and `python3-sys` dependencies with `pyo3`. - Add `html_report` feature to `criterion`, it will be required in a future release. - Remove `anyhow`. It was unused and cargo cleaned it up automatically. - Use `compile` and `exec` from within the CPython context to compile and execute benchmarks. There's probably more overhead to that than the internal API had, but that overhead should be consistent per benchmark.
2 parents 61e40de + ea1f72e commit 1005577

4 files changed

Lines changed: 157 additions & 162 deletions

File tree

‎Cargo.lock‎

Lines changed: 68 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ rustpython-format = { git = "https://github.com/RustPython/Parser.git", rev = "2
4141
# rustpython-format = { path = "../RustPython-parser/format" }
4242

4343
ahash = "0.8.3"
44-
anyhow = "1.0.45"
4544
ascii = "1.0"
4645
atty = "0.2.14"
4746
bitflags = "2.4.0"
@@ -118,9 +117,8 @@ libc = { workspace = true }
118117
rustyline = { workspace = true }
119118

120119
[dev-dependencies]
121-
cpython = "0.7.0"
122-
criterion = "0.3.5"
123-
python3-sys = "0.7.1"
120+
criterion = { version = "0.3.5", features = ["html_reports"] }
121+
pyo3 = { version = "0.20.2", features = ["auto-initialize"] }
124122

125123
[[bench]]
126124
name = "execution"

‎benches/execution.rs‎

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
use criterion::measurement::WallTime;
22
use 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
};
56
use rustpython_compiler::Mode;
67
use rustpython_parser::ast;
78
use rustpython_parser::Parse;
8-
use rustpython_vm::{Interpreter, PyResult};
9+
use rustpython_vm::{Interpreter, PyResult, Settings};
910
use std::collections::HashMap;
1011
use std::path::Path;
1112

1213
fn 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

2523
fn 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-
9670
pub 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

Comments
 (0)