Skip to content

Commit 83add10

Browse files
committed
Merge branch 'master' into coolreader18/ctx-rust-classes
2 parents ea4f2ce + 9e54dbe commit 83add10

24 files changed

+2710
-349
lines changed

Cargo.lock

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ edition = "2018"
77
[workspace]
88
members = [".", "derive", "vm", "wasm/lib", "parser"]
99

10+
[[bench]]
11+
name = "bench"
12+
path = "./benchmarks/bench.rs"
13+
14+
1015
[dependencies]
1116
log="0.4.1"
1217
env_logger="0.5.10"
@@ -15,3 +20,6 @@ rustpython_parser = {path = "parser"}
1520
rustpython_vm = {path = "vm"}
1621
rustyline = "2.1.0"
1722
xdg = "2.2.0"
23+
24+
[dev-dependencies.cpython]
25+
version = "0.2"

Lib/os.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
from _os import *
22

3+
if name == 'nt':
4+
sep = '\\'
5+
else:
6+
sep = '/'
7+
38
# Change environ to automatically call putenv(), unsetenv if they exist.
49
from _collections_abc import MutableMapping
510

benchmarks/bench.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#![feature(test)]
2+
3+
extern crate cpython;
4+
extern crate rustpython_parser;
5+
extern crate rustpython_vm;
6+
extern crate test;
7+
8+
use rustpython_vm::pyobject::PyResult;
9+
use rustpython_vm::{compile, VirtualMachine};
10+
11+
#[bench]
12+
fn bench_tokenization(b: &mut test::Bencher) {
13+
use rustpython_parser::lexer::{make_tokenizer, Tok};
14+
15+
let source = include_str!("./benchmarks/minidom.py");
16+
17+
b.bytes = source.len() as _;
18+
b.iter(|| {
19+
let lexer = make_tokenizer(source);
20+
for res in lexer {
21+
let _token: Tok = res.unwrap().1;
22+
}
23+
})
24+
}
25+
26+
#[bench]
27+
fn bench_rustpy_parse_to_ast(b: &mut test::Bencher) {
28+
use rustpython_parser::parser::parse_program;
29+
30+
let source = include_str!("./benchmarks/minidom.py");
31+
32+
b.bytes = source.len() as _;
33+
b.iter(|| parse_program(source).unwrap())
34+
}
35+
36+
#[bench]
37+
fn bench_cpython_parse_to_ast(b: &mut test::Bencher) {
38+
let source = include_str!("./benchmarks/minidom.py");
39+
40+
let gil = cpython::Python::acquire_gil();
41+
let python = gil.python();
42+
43+
let globals = None;
44+
let locals = cpython::PyDict::new(python);
45+
46+
locals.set_item(python, "SOURCE_CODE", source).unwrap();
47+
48+
let code = "compile(SOURCE_CODE, mode=\"exec\", filename=\"minidom.py\")";
49+
50+
b.bytes = source.len() as _;
51+
b.iter(|| {
52+
let res: cpython::PyResult<cpython::PyObject> = python.eval(code, globals, Some(&locals));
53+
assert!(res.is_ok());
54+
})
55+
}
56+
57+
#[bench]
58+
fn bench_cpython_nbody(b: &mut test::Bencher) {
59+
let source = include_str!("./benchmarks/nbody.py");
60+
61+
let gil = cpython::Python::acquire_gil();
62+
let python = gil.python();
63+
64+
let globals = None;
65+
let locals = None;
66+
67+
b.iter(|| {
68+
let res: cpython::PyResult<()> = python.run(source, globals, locals);
69+
assert!(res.is_ok());
70+
})
71+
}
72+
73+
#[bench]
74+
fn bench_cpython_mandelbrot(b: &mut test::Bencher) {
75+
let source = include_str!("./benchmarks/mandelbrot.py");
76+
77+
let gil = cpython::Python::acquire_gil();
78+
let python = gil.python();
79+
80+
let globals = None;
81+
let locals = None;
82+
83+
b.iter(|| {
84+
let res: cpython::PyResult<()> = python.run(source, globals, locals);
85+
assert!(res.is_ok());
86+
})
87+
}
88+
89+
#[bench]
90+
fn bench_rustpy_nbody(b: &mut test::Bencher) {
91+
// NOTE: Take long time.
92+
let source = include_str!("./benchmarks/nbody.py");
93+
94+
let vm = VirtualMachine::new();
95+
96+
let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
97+
Ok(code) => code,
98+
Err(e) => panic!("{:?}", e),
99+
};
100+
101+
b.iter(|| {
102+
let scope = vm.ctx.new_scope();
103+
let res: PyResult = vm.run_code_obj(code.clone(), scope);
104+
assert!(res.is_ok());
105+
})
106+
}
107+
108+
#[bench]
109+
fn bench_rustpy_mandelbrot(b: &mut test::Bencher) {
110+
// NOTE: Take long time.
111+
let source = include_str!("./benchmarks/mandelbrot.py");
112+
113+
let vm = VirtualMachine::new();
114+
115+
let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
116+
Ok(code) => code,
117+
Err(e) => panic!("{:?}", e),
118+
};
119+
120+
b.iter(|| {
121+
let scope = vm.ctx.new_scope();
122+
let res: PyResult = vm.run_code_obj(code.clone(), scope);
123+
assert!(res.is_ok());
124+
})
125+
}

benchmarks/benchmarks/mandelbrot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
i = i+1
2222

2323
if Tr+Ti <= 4:
24-
print('*', end='')
24+
# print('*', end='')
25+
pass
2526
else:
26-
print('·', end='')
27+
# print('·', end='')
28+
pass
2729

2830
x = x+1
2931

30-
print()
32+
# print()
3133
y = y+1

0 commit comments

Comments
 (0)