@@ -3,68 +3,72 @@ use std::{
33 env, fs,
44 path:: { Path , PathBuf } ,
55 str,
6+ sync:: LazyLock ,
67 time:: Instant ,
78} ;
89
910use anyhow:: Context ;
10- use lazy_static:: lazy_static;
1111use tree_sitter:: { Language , Parser , Query } ;
1212use tree_sitter_loader:: { CompileConfig , Loader } ;
1313
1414include ! ( "../src/tests/helpers/dirs.rs" ) ;
1515
16- lazy_static ! {
17- static ref LANGUAGE_FILTER : Option < String > =
18- env :: var ( "TREE_SITTER_BENCHMARK_LANGUAGE_FILTER" ) . ok ( ) ;
19- static ref EXAMPLE_FILTER : Option < String > =
20- env :: var ( "TREE_SITTER_BENCHMARK_EXAMPLE_FILTER" ) . ok ( ) ;
21- static ref REPETITION_COUNT : usize = env:: var( "TREE_SITTER_BENCHMARK_REPETITION_COUNT" )
16+ static LANGUAGE_FILTER : LazyLock < Option < String > > =
17+ LazyLock :: new ( || env :: var ( "TREE_SITTER_BENCHMARK_LANGUAGE_FILTER" ) . ok ( ) ) ;
18+ static EXAMPLE_FILTER : LazyLock < Option < String > > =
19+ LazyLock :: new ( || env :: var ( "TREE_SITTER_BENCHMARK_EXAMPLE_FILTER" ) . ok ( ) ) ;
20+ static REPETITION_COUNT : LazyLock < usize > = LazyLock :: new ( || {
21+ env:: var ( "TREE_SITTER_BENCHMARK_REPETITION_COUNT" )
2222 . map ( |s| s. parse :: < usize > ( ) . unwrap ( ) )
23- . unwrap_or( 5 ) ;
24- static ref TEST_LOADER : Loader = Loader :: with_parser_lib_path( SCRATCH_DIR . clone( ) ) ;
25- static ref EXAMPLE_AND_QUERY_PATHS_BY_LANGUAGE_DIR : BTreeMap <PathBuf , ( Vec <PathBuf >, Vec <PathBuf >) > = {
26- fn process_dir( result: & mut BTreeMap <PathBuf , ( Vec <PathBuf >, Vec <PathBuf >) >, dir: & Path ) {
27- if dir. join( "grammar.js" ) . exists( ) {
28- let relative_path = dir. strip_prefix( GRAMMARS_DIR . as_path( ) ) . unwrap( ) ;
29- let ( example_paths, query_paths) =
30- result. entry( relative_path. to_owned( ) ) . or_default( ) ;
31-
32- if let Ok ( example_files) = fs:: read_dir( dir. join( "examples" ) ) {
33- example_paths. extend( example_files. filter_map( |p| {
34- let p = p. unwrap( ) . path( ) ;
35- if p. is_file( ) {
36- Some ( p)
37- } else {
38- None
39- }
40- } ) ) ;
41- }
23+ . unwrap_or ( 5 )
24+ } ) ;
25+ static TEST_LOADER : LazyLock < Loader > =
26+ LazyLock :: new ( || Loader :: with_parser_lib_path ( SCRATCH_DIR . clone ( ) ) ) ;
27+
28+ #[ allow( clippy:: type_complexity) ]
29+ static EXAMPLE_AND_QUERY_PATHS_BY_LANGUAGE_DIR : LazyLock <
30+ BTreeMap < PathBuf , ( Vec < PathBuf > , Vec < PathBuf > ) > ,
31+ > = LazyLock :: new ( || {
32+ fn process_dir ( result : & mut BTreeMap < PathBuf , ( Vec < PathBuf > , Vec < PathBuf > ) > , dir : & Path ) {
33+ if dir. join ( "grammar.js" ) . exists ( ) {
34+ let relative_path = dir. strip_prefix ( GRAMMARS_DIR . as_path ( ) ) . unwrap ( ) ;
35+ let ( example_paths, query_paths) = result. entry ( relative_path. to_owned ( ) ) . or_default ( ) ;
36+
37+ if let Ok ( example_files) = fs:: read_dir ( dir. join ( "examples" ) ) {
38+ example_paths. extend ( example_files. filter_map ( |p| {
39+ let p = p. unwrap ( ) . path ( ) ;
40+ if p. is_file ( ) {
41+ Some ( p)
42+ } else {
43+ None
44+ }
45+ } ) ) ;
46+ }
4247
43- if let Ok ( query_files) = fs:: read_dir( dir. join( "queries" ) ) {
44- query_paths. extend( query_files. filter_map( |p| {
45- let p = p. unwrap( ) . path( ) ;
46- if p. is_file( ) {
47- Some ( p)
48- } else {
49- None
50- }
51- } ) ) ;
52- }
53- } else {
54- for entry in fs:: read_dir( dir) . unwrap( ) {
55- let entry = entry. unwrap( ) . path( ) ;
56- if entry. is_dir( ) {
57- process_dir( result, & entry) ;
48+ if let Ok ( query_files) = fs:: read_dir ( dir. join ( "queries" ) ) {
49+ query_paths. extend ( query_files. filter_map ( |p| {
50+ let p = p. unwrap ( ) . path ( ) ;
51+ if p. is_file ( ) {
52+ Some ( p)
53+ } else {
54+ None
5855 }
56+ } ) ) ;
57+ }
58+ } else {
59+ for entry in fs:: read_dir ( dir) . unwrap ( ) {
60+ let entry = entry. unwrap ( ) . path ( ) ;
61+ if entry. is_dir ( ) {
62+ process_dir ( result, & entry) ;
5963 }
6064 }
6165 }
66+ }
6267
63- let mut result = BTreeMap :: new( ) ;
64- process_dir( & mut result, & GRAMMARS_DIR ) ;
65- result
66- } ;
67- }
68+ let mut result = BTreeMap :: new ( ) ;
69+ process_dir ( & mut result, & GRAMMARS_DIR ) ;
70+ result
71+ } ) ;
6872
6973fn main ( ) {
7074 let max_path_length = EXAMPLE_AND_QUERY_PATHS_BY_LANGUAGE_DIR
0 commit comments