1- use codspeed_criterion_compat:: { black_box, criterion_group, criterion_main, Bencher , Criterion } ;
1+ use std:: hint:: black_box;
2+
3+ use codspeed_criterion_compat:: { criterion_group, criterion_main, Bencher , Criterion } ;
24
35use pyo3:: {
46 prelude:: * ,
@@ -7,9 +9,9 @@ use pyo3::{
79
810fn extract_str_extract_success ( bench : & mut Bencher < ' _ > ) {
911 Python :: with_gil ( |py| {
10- let s = & PyString :: new_bound ( py, "Hello, World!" ) ;
12+ let s = PyString :: new_bound ( py, "Hello, World!" ) . into_any ( ) ;
1113
12- bench. iter ( || black_box ( s) . extract :: < & str > ( ) . unwrap ( ) ) ;
14+ bench. iter ( || black_box ( & s) . extract :: < & str > ( ) . unwrap ( ) ) ;
1315 } ) ;
1416}
1517
@@ -19,18 +21,18 @@ fn extract_str_extract_fail(bench: &mut Bencher<'_>) {
1921
2022 bench. iter ( || match black_box ( & d) . extract :: < & str > ( ) {
2123 Ok ( v) => panic ! ( "should err {}" , v) ,
22- Err ( e) => black_box ( e ) ,
24+ Err ( e) => e ,
2325 } ) ;
2426 } ) ;
2527}
2628
2729#[ cfg( any( Py_3_10 , not( Py_LIMITED_API ) ) ) ]
2830fn extract_str_downcast_success ( bench : & mut Bencher < ' _ > ) {
2931 Python :: with_gil ( |py| {
30- let s = & PyString :: new_bound ( py, "Hello, World!" ) ;
32+ let s = PyString :: new_bound ( py, "Hello, World!" ) . into_any ( ) ;
3133
3234 bench. iter ( || {
33- let py_str = black_box ( s) . downcast :: < PyString > ( ) . unwrap ( ) ;
35+ let py_str = black_box ( & s) . downcast :: < PyString > ( ) . unwrap ( ) ;
3436 py_str. to_str ( ) . unwrap ( )
3537 } ) ;
3638 } ) ;
@@ -42,20 +44,16 @@ fn extract_str_downcast_fail(bench: &mut Bencher<'_>) {
4244
4345 bench. iter ( || match black_box ( & d) . downcast :: < PyString > ( ) {
4446 Ok ( v) => panic ! ( "should err {}" , v) ,
45- Err ( e) => black_box ( e ) ,
47+ Err ( e) => e ,
4648 } ) ;
4749 } ) ;
4850}
4951
5052fn extract_int_extract_success ( bench : & mut Bencher < ' _ > ) {
5153 Python :: with_gil ( |py| {
52- let int_obj: PyObject = 123 . into_py ( py) ;
53- let int = int_obj. as_ref ( py) ;
54+ let int = 123 . to_object ( py) . into_bound ( py) ;
5455
55- bench. iter ( || {
56- let v = black_box ( int) . extract :: < i64 > ( ) . unwrap ( ) ;
57- black_box ( v) ;
58- } ) ;
56+ bench. iter ( || black_box ( & int) . extract :: < i64 > ( ) . unwrap ( ) ) ;
5957 } ) ;
6058}
6159
@@ -65,26 +63,22 @@ fn extract_int_extract_fail(bench: &mut Bencher<'_>) {
6563
6664 bench. iter ( || match black_box ( & d) . extract :: < i64 > ( ) {
6765 Ok ( v) => panic ! ( "should err {}" , v) ,
68- Err ( e) => black_box ( e ) ,
66+ Err ( e) => e ,
6967 } ) ;
7068 } ) ;
7169}
7270
73- #[ cfg( not( codspeed) ) ]
7471fn extract_int_downcast_success ( bench : & mut Bencher < ' _ > ) {
7572 Python :: with_gil ( |py| {
76- let int_obj: PyObject = 123 . into_py ( py) ;
77- let int = int_obj. as_ref ( py) ;
73+ let int = 123 . to_object ( py) . into_bound ( py) ;
7874
7975 bench. iter ( || {
80- let py_int = black_box ( int) . downcast :: < PyInt > ( ) . unwrap ( ) ;
81- let v = py_int. extract :: < i64 > ( ) . unwrap ( ) ;
82- black_box ( v) ;
76+ let py_int = black_box ( & int) . downcast :: < PyInt > ( ) . unwrap ( ) ;
77+ py_int. extract :: < i64 > ( ) . unwrap ( )
8378 } ) ;
8479 } ) ;
8580}
8681
87- #[ cfg( not( codspeed) ) ]
8882fn extract_int_downcast_fail ( bench : & mut Bencher < ' _ > ) {
8983 Python :: with_gil ( |py| {
9084 let d = PyDict :: new_bound ( py) . into_any ( ) ;
@@ -96,16 +90,11 @@ fn extract_int_downcast_fail(bench: &mut Bencher<'_>) {
9690 } ) ;
9791}
9892
99- #[ cfg( not( codspeed) ) ]
10093fn extract_float_extract_success ( bench : & mut Bencher < ' _ > ) {
10194 Python :: with_gil ( |py| {
102- let float_obj: PyObject = 23.42 . into_py ( py) ;
103- let float = float_obj. as_ref ( py) ;
95+ let float = 23.42 . to_object ( py) . into_bound ( py) ;
10496
105- bench. iter ( || {
106- let v = black_box ( float) . extract :: < f64 > ( ) . unwrap ( ) ;
107- black_box ( v) ;
108- } ) ;
97+ bench. iter ( || black_box ( & float) . extract :: < f64 > ( ) . unwrap ( ) ) ;
10998 } ) ;
11099}
111100
@@ -115,21 +104,18 @@ fn extract_float_extract_fail(bench: &mut Bencher<'_>) {
115104
116105 bench. iter ( || match black_box ( & d) . extract :: < f64 > ( ) {
117106 Ok ( v) => panic ! ( "should err {}" , v) ,
118- Err ( e) => black_box ( e ) ,
107+ Err ( e) => e ,
119108 } ) ;
120109 } ) ;
121110}
122111
123- #[ cfg( not( codspeed) ) ]
124112fn extract_float_downcast_success ( bench : & mut Bencher < ' _ > ) {
125113 Python :: with_gil ( |py| {
126- let float_obj: PyObject = 23.42 . into_py ( py) ;
127- let float = float_obj. as_ref ( py) ;
114+ let float = 23.42 . to_object ( py) . into_bound ( py) ;
128115
129116 bench. iter ( || {
130- let py_int = black_box ( float) . downcast :: < PyFloat > ( ) . unwrap ( ) ;
131- let v = py_int. extract :: < f64 > ( ) . unwrap ( ) ;
132- black_box ( v) ;
117+ let py_float = black_box ( & float) . downcast :: < PyFloat > ( ) . unwrap ( ) ;
118+ py_float. value ( )
133119 } ) ;
134120 } ) ;
135121}
@@ -140,7 +126,7 @@ fn extract_float_downcast_fail(bench: &mut Bencher<'_>) {
140126
141127 bench. iter ( || match black_box ( & d) . downcast :: < PyFloat > ( ) {
142128 Ok ( v) => panic ! ( "should err {}" , v) ,
143- Err ( e) => black_box ( e ) ,
129+ Err ( e) => e ,
144130 } ) ;
145131 } ) ;
146132}
@@ -151,20 +137,15 @@ fn criterion_benchmark(c: &mut Criterion) {
151137 #[ cfg( any( Py_3_10 , not( Py_LIMITED_API ) ) ) ]
152138 c. bench_function ( "extract_str_downcast_success" , extract_str_downcast_success) ;
153139 c. bench_function ( "extract_str_downcast_fail" , extract_str_downcast_fail) ;
154- #[ cfg( not( codspeed) ) ]
155140 c. bench_function ( "extract_int_extract_success" , extract_int_extract_success) ;
156141 c. bench_function ( "extract_int_extract_fail" , extract_int_extract_fail) ;
157- #[ cfg( not( codspeed) ) ]
158142 c. bench_function ( "extract_int_downcast_success" , extract_int_downcast_success) ;
159- #[ cfg( not( codspeed) ) ]
160143 c. bench_function ( "extract_int_downcast_fail" , extract_int_downcast_fail) ;
161- #[ cfg( not( codspeed) ) ]
162144 c. bench_function (
163145 "extract_float_extract_success" ,
164146 extract_float_extract_success,
165147 ) ;
166148 c. bench_function ( "extract_float_extract_fail" , extract_float_extract_fail) ;
167- #[ cfg( not( codspeed) ) ]
168149 c. bench_function (
169150 "extract_float_downcast_success" ,
170151 extract_float_downcast_success,
0 commit comments