@@ -589,6 +589,25 @@ fn compute_file_metrics(
589589 }
590590 }
591591
592+ // Batch-load import counts per file from DB (distinct imported files,
593+ // matching the fast-path semantics in update_changed_file_metrics)
594+ let mut import_counts: HashMap < String , i64 > = HashMap :: new ( ) ;
595+ if let Ok ( mut stmt) = tx. prepare (
596+ "SELECT n1.file, COUNT(DISTINCT n2.file) FROM edges e \
597+ JOIN nodes n1 ON e.source_id = n1.id \
598+ JOIN nodes n2 ON e.target_id = n2.id \
599+ WHERE e.kind = 'imports' \
600+ GROUP BY n1.file",
601+ ) {
602+ if let Ok ( rows) = stmt. query_map ( [ ] , |row| {
603+ Ok ( ( row. get :: < _ , String > ( 0 ) ?, row. get :: < _ , i64 > ( 1 ) ?) )
604+ } ) {
605+ for row in rows. flatten ( ) {
606+ import_counts. insert ( row. 0 , row. 1 ) ;
607+ }
608+ }
609+ }
610+
592611 {
593612 let mut upsert = match tx. prepare (
594613 "INSERT OR REPLACE INTO node_metrics \
@@ -607,7 +626,7 @@ fn compute_file_metrics(
607626
608627 let line_count = line_count_map. get ( rel_path) . copied ( ) . unwrap_or ( 0 ) ;
609628 let symbol_count = symbol_counts. get ( rel_path) . copied ( ) . unwrap_or ( 0 ) ;
610- let import_count = symbols . imports . len ( ) as i64 ;
629+ let import_count = import_counts . get ( rel_path ) . copied ( ) . unwrap_or ( 0 ) ;
611630 let export_count = symbols. exports . len ( ) as i64 ;
612631 let fan_in = fan_in_map. get ( rel_path) . copied ( ) . unwrap_or ( 0 ) ;
613632 let fan_out = fan_out_map. get ( rel_path) . copied ( ) . unwrap_or ( 0 ) ;
0 commit comments