1- use criterion:: { Criterion , criterion_group, criterion_main} ;
2- use giallo:: { HighlightOptions , Registry , ThemeVariant } ;
31use std:: fs;
42
5- fn highlight_jquery_benchmark ( c : & mut Criterion ) {
6- // Load registry once for all benchmarks
7- let mut registry =
8- Registry :: load_from_file ( "builtin.zst" ) . expect ( "Failed to load registry from builtin.zst" ) ;
9- registry. link_grammars ( ) ;
10-
11- // Read jQuery file content once
12- let jquery_content =
13- fs:: read_to_string ( "src/fixtures/samples/jquery.js" ) . expect ( "Failed to read jQuery file" ) ;
14-
15- let options = HighlightOptions :: new ( "javascript" , ThemeVariant :: Single ( "vitesse-black" ) ) ;
3+ use criterion:: { BatchSize , Criterion , criterion_group, criterion_main} ;
4+ use giallo:: { HighlightOptions , Registry , ThemeVariant } ;
165
17- c. bench_function ( "highlight jquery.js" , |b| {
18- b. iter ( || {
19- registry. clear_pattern_cache ( ) ;
20- let result = registry. highlight ( & jquery_content, & options) . unwrap ( ) ;
21- std:: hint:: black_box ( result) ;
22- } )
23- } ) ;
6+ const SAMPLES : & [ ( & str , & str ) ] = & [
7+ ( "javascript" , "src/fixtures/samples/jquery.js" ) ,
8+ ( "javascript" , "grammars-themes/samples/javascript.sample" ) ,
9+ ( "typescript" , "grammars-themes/samples/typescript.sample" ) ,
10+ ( "tsx" , "grammars-themes/samples/tsx.sample" ) ,
11+ ( "rust" , "grammars-themes/samples/rust.sample" ) ,
12+ ( "c" , "grammars-themes/samples/c.sample" ) ,
13+ ( "markdown" , "grammars-themes/samples/markdown.sample" ) ,
14+ ( "python" , "grammars-themes/samples/python.sample" ) ,
15+ ( "html" , "grammars-themes/samples/html.sample" ) ,
16+ ( "css" , "grammars-themes/samples/css.sample" ) ,
17+ ( "ruby" , "grammars-themes/samples/ruby.sample" ) ,
18+ ( "go" , "grammars-themes/samples/go.sample" ) ,
19+ ( "astro" , "grammars-themes/samples/astro.sample" ) ,
20+ ( "c#" , "grammars-themes/samples/csharp.sample" ) ,
21+ ( "java" , "grammars-themes/samples/java.sample" ) ,
22+ ( "php" , "grammars-themes/samples/php.sample" ) ,
23+ ( "json" , "grammars-themes/samples/json.sample" ) ,
24+ ( "shellscript" , "grammars-themes/samples/shellscript.sample" ) ,
25+ ] ;
26+
27+ fn bench_name ( grammar : & ' static str , path : & str ) -> & ' static str {
28+ if path. contains ( "jquery" ) {
29+ "jquery"
30+ } else {
31+ grammar
32+ }
2433}
2534
2635fn highlight_simple_benchmark ( c : & mut Criterion ) {
@@ -33,11 +42,11 @@ fn highlight_simple_benchmark(c: &mut Criterion) {
3342 let options = HighlightOptions :: new ( "typescript" , ThemeVariant :: Single ( "vitesse-black" ) ) ;
3443
3544 c. bench_function ( "highlight simple.ts" , |b| {
36- b. iter ( || {
37- registry. clear_pattern_cache ( ) ;
38- let result = registry. highlight ( & ts_content, & options) . unwrap ( ) ;
39- std :: hint :: black_box ( result ) ;
40- } )
45+ b. iter_batched (
46+ || registry. clear_caches ( ) ,
47+ | ( ) | std :: hint :: black_box ( registry. highlight ( & ts_content, & options) . unwrap ( ) ) ,
48+ BatchSize :: PerIteration ,
49+ )
4150 } ) ;
4251}
4352
@@ -51,62 +60,57 @@ fn highlight_multiple_simple_benchmark(c: &mut Criterion) {
5160 let options = HighlightOptions :: new ( "typescript" , ThemeVariant :: Single ( "vitesse-black" ) ) ;
5261
5362 c. bench_function ( "highlight multiple simple.ts" , |b| {
54- b. iter ( || {
55- // should not be 5x slower than "highlight simple.ts"
56- registry. clear_pattern_cache ( ) ;
57- let result = registry. highlight ( & ts_content, & options) . unwrap ( ) ;
58- std:: hint:: black_box ( result) ;
59- let result = registry. highlight ( & ts_content, & options) . unwrap ( ) ;
60- std:: hint:: black_box ( result) ;
61- let result = registry. highlight ( & ts_content, & options) . unwrap ( ) ;
62- std:: hint:: black_box ( result) ;
63- let result = registry. highlight ( & ts_content, & options) . unwrap ( ) ;
64- std:: hint:: black_box ( result) ;
65- let result = registry. highlight ( & ts_content, & options) . unwrap ( ) ;
66- std:: hint:: black_box ( result) ;
67- } )
63+ b. iter_batched (
64+ || registry. clear_caches ( ) ,
65+ |( ) | {
66+ // should not be 5x slower than "highlight simple.ts"
67+ for _ in 0 ..5 {
68+ std:: hint:: black_box ( registry. highlight ( & ts_content, & options) . unwrap ( ) ) ;
69+ }
70+ } ,
71+ BatchSize :: PerIteration ,
72+ )
6873 } ) ;
6974}
7075
71- fn highlight_sample_benchmark ( c : & mut Criterion , grammar : & str ) {
72- let mut registry =
73- Registry :: load_from_file ( "builtin.zst" ) . expect ( "Failed to load registry from builtin.zst" ) ;
74- registry. link_grammars ( ) ;
75-
76- let sample_path = format ! ( "grammars-themes/samples/{grammar}.sample" ) ;
77- let content = fs:: read_to_string ( & sample_path)
78- . unwrap_or_else ( |_| panic ! ( "Failed to read sample {sample_path}" ) ) ;
79-
80- let options = HighlightOptions :: new ( grammar, ThemeVariant :: Single ( "vitesse-black" ) ) ;
81-
82- c. bench_function ( & format ! ( "highlight {grammar}.sample" ) , |b| {
83- b. iter ( || {
84- registry. clear_pattern_cache ( ) ;
85- let result = registry. highlight ( & content, & options) . unwrap ( ) ;
86- std:: hint:: black_box ( result) ;
87- } )
88- } ) ;
89- }
90-
91- fn highlight_rust_sample_benchmark ( c : & mut Criterion ) {
92- highlight_sample_benchmark ( c, "rust" ) ;
93- }
94-
95- fn highlight_markdown_sample_benchmark ( c : & mut Criterion ) {
96- highlight_sample_benchmark ( c, "markdown" ) ;
76+ fn highlight_cold_benchmark ( c : & mut Criterion ) {
77+ let registry = Registry :: load_from_file ( "builtin.zst" ) . unwrap ( ) ;
78+ let mut group = c. benchmark_group ( "highlight cold" ) ;
79+ group. sample_size ( 50 ) ;
80+ for & ( grammar, path) in SAMPLES {
81+ let content = fs:: read_to_string ( path) . unwrap ( ) ;
82+ let options = HighlightOptions :: new ( grammar, ThemeVariant :: Single ( "vitesse-black" ) ) ;
83+ registry. highlight ( & content, & options) . unwrap ( ) ;
84+ group. bench_function ( bench_name ( grammar, path) , |b| {
85+ b. iter_batched (
86+ || registry. clear_caches ( ) ,
87+ |( ) | std:: hint:: black_box ( registry. highlight ( & content, & options) . unwrap ( ) ) ,
88+ BatchSize :: PerIteration ,
89+ )
90+ } ) ;
91+ }
92+ group. finish ( ) ;
9793}
9894
99- fn highlight_javascript_sample_benchmark ( c : & mut Criterion ) {
100- highlight_sample_benchmark ( c, "javascript" ) ;
95+ fn highlight_warm_benchmark ( c : & mut Criterion ) {
96+ let registry = Registry :: load_from_file ( "builtin.zst" ) . unwrap ( ) ;
97+ let mut group = c. benchmark_group ( "highlight warm" ) ;
98+ for & ( grammar, path) in SAMPLES {
99+ let content = fs:: read_to_string ( path) . unwrap ( ) ;
100+ let options = HighlightOptions :: new ( grammar, ThemeVariant :: Single ( "vitesse-black" ) ) ;
101+ registry. highlight ( & content, & options) . unwrap ( ) ;
102+ group. bench_function ( bench_name ( grammar, path) , |b| {
103+ b. iter ( || std:: hint:: black_box ( registry. highlight ( & content, & options) . unwrap ( ) ) )
104+ } ) ;
105+ }
106+ group. finish ( ) ;
101107}
102108
103109criterion_group ! (
104110 benches,
105- highlight_jquery_benchmark,
106111 highlight_simple_benchmark,
107112 highlight_multiple_simple_benchmark,
108- highlight_rust_sample_benchmark,
109- highlight_markdown_sample_benchmark,
110- highlight_javascript_sample_benchmark
113+ highlight_cold_benchmark,
114+ highlight_warm_benchmark,
111115) ;
112116criterion_main ! ( benches) ;
0 commit comments