1212//! - typescript-language-server: npm install -g typescript-language-server typescript
1313//! - rust-analyzer: rustup component add rust-analyzer
1414
15+ #![ allow( unused_imports) ]
16+ #![ allow( dead_code) ]
17+
1518use criterion:: { black_box, criterion_group, criterion_main, BenchmarkId , Criterion } ;
1619use lsp_types:: {
1720 DocumentSymbolParams , InitializeParams , InitializeResult , PartialResultParams ,
@@ -24,8 +27,8 @@ use semfora_engine::socket_server::{index_directory, IndexOptions};
2427use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
2528use std:: io:: { BufRead , BufReader , Read , Write } ;
2629use std:: path:: { Path , PathBuf } ;
27- use std:: str:: FromStr ;
2830use std:: process:: { Child , Command , Stdio } ;
31+ use std:: str:: FromStr ;
2932use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
3033use std:: time:: Duration ;
3134
@@ -136,7 +139,9 @@ impl LspClient {
136139
137140 // Write request
138141 let stdin = self . process . stdin . as_mut ( ) . ok_or ( "No stdin" ) ?;
139- stdin. write_all ( message. as_bytes ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
142+ stdin
143+ . write_all ( message. as_bytes ( ) )
144+ . map_err ( |e| e. to_string ( ) ) ?;
140145 stdin. flush ( ) . map_err ( |e| e. to_string ( ) ) ?;
141146
142147 // Read responses, skipping notifications until we get our response
@@ -147,7 +152,8 @@ impl LspClient {
147152 let body = read_lsp_message ( & mut reader) ?;
148153
149154 // Try to parse as a response with our id
150- let json: serde_json:: Value = serde_json:: from_slice ( & body) . map_err ( |e| e. to_string ( ) ) ?;
155+ let json: serde_json:: Value =
156+ serde_json:: from_slice ( & body) . map_err ( |e| e. to_string ( ) ) ?;
151157
152158 // Check if this is a response to our request (has matching id)
153159 if let Some ( response_id) = json. get ( "id" ) {
@@ -182,7 +188,9 @@ impl LspClient {
182188 let stdin = self . process . stdin . as_mut ( ) . ok_or ( "No stdin" ) ?;
183189 let body = r#"{"jsonrpc":"2.0","method":"initialized","params":{}}"# ;
184190 let message = format ! ( "Content-Length: {}\r \n \r \n {}" , body. len( ) , body) ;
185- stdin. write_all ( message. as_bytes ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
191+ stdin
192+ . write_all ( message. as_bytes ( ) )
193+ . map_err ( |e| e. to_string ( ) ) ?;
186194 stdin. flush ( ) . map_err ( |e| e. to_string ( ) ) ?;
187195 Ok ( ( ) )
188196 }
@@ -203,7 +211,10 @@ impl LspClient {
203211 }
204212
205213 /// Search workspace symbols (equivalent to semfora's search_symbols)
206- fn workspace_symbols ( & mut self , query : & str ) -> Result < Vec < lsp_types:: SymbolInformation > , String > {
214+ fn workspace_symbols (
215+ & mut self ,
216+ query : & str ,
217+ ) -> Result < Vec < lsp_types:: SymbolInformation > , String > {
207218 let params = WorkspaceSymbolParams {
208219 query : query. to_string ( ) ,
209220 work_done_progress_params : WorkDoneProgressParams :: default ( ) ,
@@ -334,7 +345,8 @@ fn bench_document_symbols_latency(c: &mut Criterion) {
334345 & file_uri,
335346 |b, file_uri| {
336347 // Initialize LSP server once
337- let mut client = LspClient :: spawn ( "typescript-language-server" , & [ "--stdio" ] ) . unwrap ( ) ;
348+ let mut client =
349+ LspClient :: spawn ( "typescript-language-server" , & [ "--stdio" ] ) . unwrap ( ) ;
338350 let root_uri = format ! ( "file://{}" , repo_path. display( ) ) ;
339351 client. initialize ( & root_uri) . unwrap ( ) ;
340352 client. initialized ( ) . unwrap ( ) ;
@@ -349,7 +361,7 @@ fn bench_document_symbols_latency(c: &mut Criterion) {
349361 ) ;
350362
351363 // Benchmark semfora
352- let Some ( ( cache , _) ) = setup_semfora_index ( repo_name) else {
364+ let Some ( ( _cache , _) ) = setup_semfora_index ( repo_name) else {
353365 continue ;
354366 } ;
355367
@@ -395,7 +407,8 @@ fn bench_symbol_search_latency(c: &mut Criterion) {
395407 BenchmarkId :: new ( format ! ( "{}/{}/lsp" , repo_name, query) , query) ,
396408 & root_uri,
397409 |b, root_uri| {
398- let mut client = LspClient :: spawn ( "typescript-language-server" , & [ "--stdio" ] ) . unwrap ( ) ;
410+ let mut client =
411+ LspClient :: spawn ( "typescript-language-server" , & [ "--stdio" ] ) . unwrap ( ) ;
399412 client. initialize ( root_uri) . unwrap ( ) ;
400413 client. initialized ( ) . unwrap ( ) ;
401414 std:: thread:: sleep ( Duration :: from_millis ( 500 ) ) ;
@@ -470,8 +483,14 @@ fn bench_information_richness(c: &mut Criterion) {
470483 group. bench_function ( BenchmarkId :: new ( * repo_name, "compare" ) , |b| {
471484 b. iter ( || {
472485 // LSP fields available
473- let lsp_fields = vec ! [
474- "name" , "kind" , "range" , "selectionRange" , "children" , "deprecated" , "detail" ,
486+ let lsp_fields = [
487+ "name" ,
488+ "kind" ,
489+ "range" ,
490+ "selectionRange" ,
491+ "children" ,
492+ "deprecated" ,
493+ "detail" ,
475494 ] ;
476495
477496 // Semfora fields available
@@ -510,7 +529,10 @@ fn bench_information_richness(c: &mut Criterion) {
510529 eprintln ! ( " calls, state_changes, control_flow, behavioral_risk," ) ;
511530 eprintln ! ( " complexity, cognitive_complexity, dependencies" ) ;
512531 eprintln ! ( "\n Additional data:" ) ;
513- eprintln ! ( " - Dependencies: {} items" , summary. added_dependencies. len( ) ) ;
532+ eprintln ! (
533+ " - Dependencies: {} items" ,
534+ summary. added_dependencies. len( )
535+ ) ;
514536 eprintln ! ( " - Calls: {} items" , summary. calls. len( ) ) ;
515537 eprintln ! ( " - State changes: {} items" , summary. state_changes. len( ) ) ;
516538 }
@@ -599,7 +621,7 @@ fn bench_token_efficiency(c: &mut Criterion) {
599621 eprintln ! ( " Semfora tokens: ~{}" , semfora_json. len( ) / 4 ) ;
600622 eprintln ! ( " TOON tokens: ~{}" , toon_output. len( ) / 4 ) ;
601623
602- if lsp_json. len ( ) > 0 && toon_output. len ( ) > 0 {
624+ if ! lsp_json. is_empty ( ) && ! toon_output. is_empty ( ) {
603625 let savings = 100.0 * ( 1.0 - ( toon_output. len ( ) as f64 / lsp_json. len ( ) as f64 ) ) ;
604626 eprintln ! ( "\n TOON vs LSP savings: {:.1}%" , savings) ;
605627 }
0 commit comments