11use criterion:: { BenchmarkId , Criterion , Throughput , criterion_group, criterion_main} ;
2+ use prmt:: detector:: { DetectionContext , detect} ;
23use prmt:: { ModuleContext , ModuleRegistry , Template , execute, parse} ;
4+ use std:: collections:: HashSet ;
35use std:: hint:: black_box;
46use std:: time:: Duration ;
57
@@ -21,6 +23,24 @@ fn setup_registry() -> ModuleRegistry {
2123 registry
2224}
2325
26+ fn detection_for ( markers : & [ & ' static str ] ) -> DetectionContext {
27+ if markers. is_empty ( ) {
28+ return DetectionContext :: default ( ) ;
29+ }
30+
31+ let required: HashSet < & str > = markers. iter ( ) . copied ( ) . collect ( ) ;
32+
33+ detect ( & required)
34+ }
35+
36+ fn ctx ( no_version : bool , exit_code : Option < i32 > , markers : & [ & ' static str ] ) -> ModuleContext {
37+ ModuleContext {
38+ no_version,
39+ exit_code,
40+ detection : detection_for ( markers) ,
41+ }
42+ }
43+
2444fn bench_parser_scenarios ( c : & mut Criterion ) {
2545 let mut group = c. benchmark_group ( "parser_scenarios" ) ;
2646
@@ -66,45 +86,50 @@ fn bench_template_rendering(c: &mut Criterion) {
6686 let mut group = c. benchmark_group ( "template_rendering" ) ;
6787
6888 let registry = setup_registry ( ) ;
69- let context_with_version = ModuleContext {
70- no_version : false ,
71- exit_code : Some ( 0 ) ,
72- } ;
73- let context_no_version = ModuleContext {
74- no_version : true ,
75- exit_code : Some ( 0 ) ,
76- } ;
77- let context_error = ModuleContext {
78- no_version : true ,
79- exit_code : Some ( 1 ) ,
80- } ;
89+ let ctx_minimal = ctx ( true , Some ( 0 ) , & [ ] ) ;
90+ let ctx_git = ctx ( true , Some ( 0 ) , & [ ".git" ] ) ;
91+ let ctx_error = ctx ( true , Some ( 1 ) , & [ ".git" ] ) ;
92+ let ctx_with_versions = ctx ( false , Some ( 0 ) , & [ ".git" , "Cargo.toml" , "package.json" ] ) ;
93+ let ctx_all = ctx (
94+ true ,
95+ Some ( 0 ) ,
96+ & [
97+ ".git" ,
98+ "Cargo.toml" ,
99+ "package.json" ,
100+ "requirements.txt" ,
101+ "go.mod" ,
102+ "deno.json" ,
103+ "bun.lockb" ,
104+ ] ,
105+ ) ;
81106
82107 let scenarios = vec ! [
83- ( "minimal" , "{path}" , context_no_version . clone( ) ) ,
108+ ( "minimal" , "{path}" , ctx_minimal . clone( ) ) ,
84109 (
85110 "typical_success" ,
86111 "{path:cyan} {git:purple} {ok:green:✓}" ,
87- context_no_version . clone( ) ,
112+ ctx_git . clone( ) ,
88113 ) ,
89114 (
90115 "typical_error" ,
91116 "{path:cyan} {git:purple} {fail:red:✗}" ,
92- context_error . clone( ) ,
117+ ctx_error . clone( ) ,
93118 ) ,
94119 (
95120 "with_versions" ,
96121 "{path} {rust} {node} {git}" ,
97- context_with_version . clone( ) ,
122+ ctx_with_versions . clone( ) ,
98123 ) ,
99124 (
100125 "complex_styled" ,
101126 "{path:cyan.bold:short:[:]} {git:purple.italic::on :}" ,
102- context_no_version . clone( ) ,
127+ ctx_git . clone( ) ,
103128 ) ,
104129 (
105130 "all_modules" ,
106131 "{path} {rust} {node} {python} {go} {deno} {bun} {git} {ok}" ,
107- context_no_version . clone( ) ,
132+ ctx_all . clone( ) ,
108133 ) ,
109134 ] ;
110135
@@ -154,38 +179,32 @@ fn bench_end_to_end_scenarios(c: &mut Criterion) {
154179 group. finish ( ) ;
155180}
156181
157- fn bench_cache_effectiveness ( c : & mut Criterion ) {
158- let mut group = c. benchmark_group ( "cache_effectiveness " ) ;
182+ fn bench_memo_effectiveness ( c : & mut Criterion ) {
183+ let mut group = c. benchmark_group ( "memo_effectiveness " ) ;
159184
160- // First call (cold cache )
161- group. bench_function ( "git_cold_cache " , |b| {
185+ // First call (nothing memoized )
186+ group. bench_function ( "git_cold_memo " , |b| {
162187 use prmt:: Module ;
163188 use prmt:: modules:: git:: GitModule ;
164189
165190 let module = GitModule ;
166- let context = ModuleContext {
167- no_version : false ,
168- exit_code : None ,
169- } ;
191+ let context = ctx ( false , None , & [ ".git" ] ) ;
170192
171193 b. iter ( || {
172- // Clear cache would go here if we had a method for it
194+ // Clear memo would go here if we had a method for it
173195 module. render ( black_box ( "full" ) , black_box ( & context) )
174196 } ) ;
175197 } ) ;
176198
177- // Warmed cache
178- group. bench_function ( "git_warm_cache " , |b| {
199+ // Warm memo
200+ group. bench_function ( "git_warm_memo " , |b| {
179201 use prmt:: Module ;
180202 use prmt:: modules:: git:: GitModule ;
181203
182204 let module = GitModule ;
183- let context = ModuleContext {
184- no_version : false ,
185- exit_code : None ,
186- } ;
205+ let context = ctx ( false , None , & [ ".git" ] ) ;
187206
188- // Warm the cache
207+ // Warm the memoized value
189208 let _ = module. render ( "full" , & context) ;
190209
191210 b. iter ( || module. render ( black_box ( "full" ) , black_box ( & context) ) ) ;
@@ -197,10 +216,7 @@ fn bench_cache_effectiveness(c: &mut Criterion) {
197216 use prmt:: modules:: rust:: RustModule ;
198217
199218 let module = RustModule ;
200- let context = ModuleContext {
201- no_version : false ,
202- exit_code : None ,
203- } ;
219+ let context = ctx ( false , None , & [ "Cargo.toml" ] ) ;
204220
205221 b. iter ( || module. render ( black_box ( "full" ) , black_box ( & context) ) ) ;
206222 } ) ;
@@ -211,10 +227,7 @@ fn bench_cache_effectiveness(c: &mut Criterion) {
211227 use prmt:: modules:: rust:: RustModule ;
212228
213229 let module = RustModule ;
214- let context = ModuleContext {
215- no_version : true ,
216- exit_code : None ,
217- } ;
230+ let context = ctx ( true , None , & [ "Cargo.toml" ] ) ;
218231
219232 b. iter ( || module. render ( black_box ( "full" ) , black_box ( & context) ) ) ;
220233 } ) ;
@@ -355,7 +368,7 @@ criterion_group!(
355368 bench_parser_scenarios,
356369 bench_template_rendering,
357370 bench_end_to_end_scenarios,
358- bench_cache_effectiveness ,
371+ bench_memo_effectiveness ,
359372 bench_string_operations,
360373 bench_unicode_operations,
361374 bench_style_parsing,
0 commit comments