11use :: criterion:: SamplingMode ;
2+ use rayon:: ThreadPoolBuilder ;
23use ruff_benchmark:: criterion;
34
45use criterion:: { BatchSize , Criterion , criterion_group, criterion_main} ;
@@ -56,6 +57,8 @@ fn bench_project(
5657 ) ;
5758 }
5859
60+ setup_rayon ( ) ;
61+
5962 let setup_project = project. setup ( ) . expect ( "Failed to setup project" ) ;
6063
6164 let root = SystemPathBuf :: from_path_buf ( setup_project. path . clone ( ) ) . unwrap ( ) ;
@@ -77,11 +80,16 @@ fn bench_project(
7780
7881 let mut group = criterion. benchmark_group ( "project" ) ;
7982 group. sampling_mode ( SamplingMode :: Flat ) ;
80- group. sample_size ( match size {
81- Size :: Small => 30 ,
82- Size :: Medium => 20 ,
83- Size :: Large => 10 ,
84- } ) ;
83+
84+ if cfg ! ( feature = "codspeed" ) {
85+ group. sample_size ( 10 ) ;
86+ } else {
87+ group. sample_size ( match size {
88+ Size :: Small => 30 ,
89+ Size :: Medium => 20 ,
90+ Size :: Large => 10 ,
91+ } ) ;
92+ }
8593
8694 group. bench_function ( setup_project. config . name , |b| {
8795 b. iter_batched_ref (
@@ -190,5 +198,21 @@ fn sympy(criterion: &mut Criterion) {
190198 bench_project ( project, criterion, 13000 , Size :: Large ) ;
191199}
192200
201+ static RAYON_INITIALIZED : std:: sync:: Once = std:: sync:: Once :: new ( ) ;
202+
203+ fn setup_rayon ( ) {
204+ // Initialize the rayon thread pool outside the benchmark because it has a significant cost.
205+ // We limit the thread pool to only one (the current thread) because we're focused on
206+ // where ty spends time and less about how well the code runs concurrently.
207+ // We might want to add a benchmark focusing on concurrency to detect congestion in the future.
208+ RAYON_INITIALIZED . call_once ( || {
209+ ThreadPoolBuilder :: new ( )
210+ . num_threads ( 1 )
211+ . use_current_thread ( )
212+ . build_global ( )
213+ . unwrap ( ) ;
214+ } ) ;
215+ }
216+
193217criterion_group ! ( project, colour_science, freqtrade, pandas, pydantic, sympy) ;
194218criterion_main ! ( project) ;
0 commit comments