-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbindgen.rs
126 lines (117 loc) · 3.46 KB
/
bindgen.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
criterion::criterion_group!(benches, criterion_benchmark);
criterion::criterion_main!(benches);
fn criterion_benchmark(crit: &mut criterion::Criterion) {
bench_from_str(crit);
bench_mod(crit);
}
fn bench_from_str(crit: &mut criterion::Criterion) {
let mut group_from_str = crit.benchmark_group("bindgen_str");
group_from_str
.warm_up_time(std::time::Duration::from_millis(250))
.sample_size(100);
macro_rules! bench_impl {
{
|$criterion:ident| $(,)?
$bench_name:ident $(,)?
$(py)?$(python)?$(:)? $code_py:literal $(,)?
} => {
{
const CODE_PY: &str = indoc::indoc! { $code_py };
$criterion.bench_function(stringify!($bench_name), |b| {
b.iter(|| {
pyo3_bindgen_engine::Codegen::default()
.module_from_str(
criterion::black_box(CODE_PY),
criterion::black_box(concat!("bench_mod_", stringify!($bench_name)))
)
.unwrap()
.generate()
.unwrap()
});
});
}
};
}
bench_impl! {
|group_from_str|
attribute
r#"
t_const_float: float = 0.42
"#
}
bench_impl! {
|group_from_str|
function
r#"
def t_fn(t_arg1: str) -> int:
"""t_docs"""
...
"#
}
bench_impl! {
|group_from_str|
class
r#"
from typing import Dict, Optional
class t_class:
"""t_docs"""
def __init__(self, t_arg1: str, t_arg2: Optional[int] = None):
"""t_docs_init"""
...
def t_method(self, t_arg1: Dict[str, int], **kwargs):
"""t_docs_method"""
...
@property
def t_prop(self) -> int:
...
@t_prop.setter
def t_prop(self, value: int):
...
"#
}
group_from_str.finish();
}
fn bench_mod(crit: &mut criterion::Criterion) {
let mut group_module = crit.benchmark_group("bindgen_mod");
group_module
.warm_up_time(std::time::Duration::from_secs(2))
.sample_size(10);
macro_rules! bench_impl {
(
|$criterion:ident| $(,)?
$(module:)? $module_name:literal $(,)?
) => {
$criterion.bench_function($module_name, |b| {
b.iter(|| {
pyo3_bindgen_engine::Codegen::default()
.module_name(
criterion::black_box($module_name)
)
.unwrap()
.generate()
.unwrap()
});
});
};
{
|$criterion:ident| $(,)?
$(modules:)? [ $($module:literal),+ $(,)? ] $(,)?
} => {
$(
bench_impl!(|$criterion| $module);
)+
};
}
bench_impl! {
|group_module|
modules: [
"io",
"math",
"os",
"re",
"sys",
"time",
]
}
group_module.finish();
}