@@ -389,21 +389,25 @@ impl FileItem {
389389/// Options for creating a [`FilePicker`].
390390pub struct FilePickerOptions {
391391 pub base_path : String ,
392- pub warmup_mmap_cache : bool ,
392+ /// Pre-populate mmap caches for top-frecency files after the initial scan.
393+ pub enable_mmap_cache : bool ,
394+ /// Build content index after the initial scan for faster content-aware filtering.
395+ pub enable_content_indexing : bool ,
396+ /// Mode of the picker impact the way file watcher events are handled and the scoring logic
393397 pub mode : FFFMode ,
394398 /// Explicit cache budget. When `None`, the budget is auto-computed from
395399 /// the repo size after the initial scan completes.
396400 pub cache_budget : Option < ContentCacheBudget > ,
397401 /// When `false`, `new_with_shared_state` skips the background file watcher.
398- /// Files are still scanned, warmed up, and bigram-indexed.
399402 pub watch : bool ,
400403}
401404
402405impl Default for FilePickerOptions {
403406 fn default ( ) -> Self {
404407 Self {
405408 base_path : "." . into ( ) ,
406- warmup_mmap_cache : false ,
409+ enable_mmap_cache : false ,
410+ enable_content_indexing : false ,
407411 mode : FFFMode :: default ( ) ,
408412 cache_budget : None ,
409413 watch : true ,
@@ -421,7 +425,8 @@ pub struct FilePicker {
421425 watcher_ready : Arc < AtomicBool > ,
422426 scanned_files_count : Arc < AtomicUsize > ,
423427 background_watcher : Option < BackgroundWatcher > ,
424- warmup_mmap_cache : bool ,
428+ enable_mmap_cache : bool ,
429+ enable_content_indexing : bool ,
425430 watch : bool ,
426431 cancelled : Arc < AtomicBool > ,
427432 // This is a soft lock that we use to prevent rescan be triggered while the
@@ -479,8 +484,16 @@ impl FilePicker {
479484 . and_then ( |p| p. to_str ( ) )
480485 }
481486
482- pub fn need_warmup_mmap_cache ( & self ) -> bool {
483- self . warmup_mmap_cache
487+ pub fn need_enable_mmap_cache ( & self ) -> bool {
488+ self . enable_mmap_cache
489+ }
490+
491+ pub fn need_enable_content_indexing ( & self ) -> bool {
492+ self . enable_content_indexing
493+ }
494+
495+ pub fn need_watch ( & self ) -> bool {
496+ self . watch
484497 }
485498
486499 pub fn mode ( & self ) -> FFFMode {
@@ -632,7 +645,8 @@ impl FilePicker {
632645 post_scan_busy : Arc :: new ( AtomicBool :: new ( false ) ) ,
633646 scanned_files_count : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
634647 sync_data : FileSync :: new ( ) ,
635- warmup_mmap_cache : options. warmup_mmap_cache ,
648+ enable_mmap_cache : options. enable_mmap_cache ,
649+ enable_content_indexing : options. enable_content_indexing ,
636650 watch : options. watch ,
637651 watcher_ready : Arc :: new ( AtomicBool :: new ( false ) ) ,
638652 } )
@@ -648,13 +662,15 @@ impl FilePicker {
648662 let picker = Self :: new ( options) ?;
649663
650664 info ! (
651- "Spawning background threads: base_path={}, warmup={}, mode={:?}" ,
665+ "Spawning background threads: base_path={}, warmup={}, content_indexing={}, mode={:?}" ,
652666 picker. base_path. display( ) ,
653- picker. warmup_mmap_cache,
667+ picker. enable_mmap_cache,
668+ picker. enable_content_indexing,
654669 picker. mode,
655670 ) ;
656671
657- let warmup = picker. warmup_mmap_cache ;
672+ let warmup = picker. enable_mmap_cache ;
673+ let content_indexing = picker. enable_content_indexing ;
658674 let watch = picker. watch ;
659675 let mode = picker. mode ;
660676
@@ -678,6 +694,7 @@ impl FilePicker {
678694 watcher_ready,
679695 synced_files_count,
680696 warmup,
697+ content_indexing,
681698 watch,
682699 mode,
683700 shared_picker,
@@ -1400,13 +1417,15 @@ impl FilePicker {
14001417
14011418 /// Spawn a background thread to rebuild the bigram index after rescan.
14021419 pub ( crate ) fn spawn_post_rescan_rebuild ( & self , shared_picker : SharedPicker ) -> bool {
1403- if ! self . warmup_mmap_cache || self . cancelled . load ( Ordering :: Relaxed ) {
1420+ if self . cancelled . load ( Ordering :: Relaxed ) {
14041421 return false ;
14051422 }
14061423
14071424 let post_scan_busy = Arc :: clone ( & self . post_scan_busy ) ;
14081425 let cancelled = Arc :: clone ( & self . cancelled ) ;
14091426 let auto_budget = !self . has_explicit_cache_budget ;
1427+ let do_warmup = self . enable_mmap_cache ;
1428+ let do_content_indexing = self . enable_content_indexing ;
14101429
14111430 post_scan_busy. store ( true , Ordering :: Release ) ;
14121431
@@ -1450,7 +1469,7 @@ impl FilePicker {
14501469
14511470 if let Some ( ( files, budget, bp, arena) ) = files_snapshot {
14521471 // Warmup mmap caches.
1453- if !cancelled. load ( Ordering :: Acquire ) {
1472+ if do_warmup && !cancelled. load ( Ordering :: Acquire ) {
14541473 let t = std:: time:: Instant :: now ( ) ;
14551474 warmup_mmaps ( files, & budget, & bp, arena) ;
14561475 info ! (
@@ -1462,7 +1481,7 @@ impl FilePicker {
14621481 }
14631482
14641483 // Build bigram index (lock-free).
1465- if !cancelled. load ( Ordering :: Acquire ) {
1484+ if do_content_indexing && !cancelled. load ( Ordering :: Acquire ) {
14661485 let t = std:: time:: Instant :: now ( ) ;
14671486 info ! (
14681487 "Rescan: starting bigram index build for {} files..." ,
@@ -1495,8 +1514,10 @@ impl FilePicker {
14951514
14961515 post_scan_busy. store ( false , Ordering :: Release ) ;
14971516 info ! (
1498- "Rescan post-scan warmup + bigram total: {:.2}s" ,
1517+ "Rescan post-scan phase total: {:.2}s (warmup={}, content_indexing={}) " ,
14991518 phase_start. elapsed( ) . as_secs_f64( ) ,
1519+ do_warmup,
1520+ do_content_indexing,
15001521 ) ;
15011522 } ) ;
15021523
@@ -1617,7 +1638,8 @@ fn spawn_scan_and_watcher(
16171638 scan_signal : Arc < AtomicBool > ,
16181639 watcher_ready : Arc < AtomicBool > ,
16191640 synced_files_count : Arc < AtomicUsize > ,
1620- warmup_mmap_cache : bool ,
1641+ enable_mmap_cache : bool ,
1642+ enable_content_indexing : bool ,
16211643 watch : bool ,
16221644 mode : FFFMode ,
16231645 shared_picker : SharedPicker ,
@@ -1725,7 +1747,10 @@ fn spawn_scan_and_watcher(
17251747
17261748 watcher_ready. store ( true , Ordering :: Release ) ;
17271749
1728- if warmup_mmap_cache && !cancelled. load ( Ordering :: Acquire ) {
1750+ let need_post_scan =
1751+ ( enable_mmap_cache || enable_content_indexing) && !cancelled. load ( Ordering :: Acquire ) ;
1752+
1753+ if need_post_scan {
17291754 post_scan_busy. store ( true , Ordering :: Release ) ;
17301755 let phase_start = std:: time:: Instant :: now ( ) ;
17311756
@@ -1768,9 +1793,11 @@ fn spawn_scan_and_watcher(
17681793 None
17691794 } ;
17701795
1796+ // both of this is using a custom soft lock not guaranteed by compiler
1797+ // this is required to keep the picker functioning if someone opened a really crazy
1798+ // e.g 10m files directory but potentially unsafe
17711799 if let Some ( ( files, budget, arena) ) = files_snapshot {
1772- // Warmup: populate mmap caches for top-frecency files.
1773- if !cancelled. load ( Ordering :: Acquire ) {
1800+ if enable_mmap_cache && !cancelled. load ( Ordering :: Acquire ) {
17741801 let warmup_start = std:: time:: Instant :: now ( ) ;
17751802 warmup_mmaps ( files, & budget, & base_path, arena) ;
17761803 info ! (
@@ -1781,16 +1808,9 @@ fn spawn_scan_and_watcher(
17811808 ) ;
17821809 }
17831810
1784- // Build bigram index — entirely lock-free.
1785- if !cancelled. load ( Ordering :: Acquire ) {
1786- let bigram_start = std:: time:: Instant :: now ( ) ;
1787- info ! ( "Starting bigram index build for {} files..." , files. len( ) ) ;
1811+ if enable_content_indexing && !cancelled. load ( Ordering :: Acquire ) {
17881812 let ( index, content_binary) =
17891813 build_bigram_index ( files, & budget, & base_path, arena) ;
1790- info ! (
1791- "Bigram index ready in {:.2}s" ,
1792- bigram_start. elapsed( ) . as_secs_f64( ) ,
1793- ) ;
17941814
17951815 if let Ok ( mut guard) = shared_picker. write ( )
17961816 && let Some ( ref mut picker) = * guard
@@ -1813,8 +1833,10 @@ fn spawn_scan_and_watcher(
18131833 post_scan_busy. store ( false , Ordering :: Release ) ;
18141834
18151835 info ! (
1816- "Post-scan warmup + bigram total: {:.2}s" ,
1836+ "Post-scan phase total: {:.2}s (warmup={}, content_indexing={}) " ,
18171837 phase_start. elapsed( ) . as_secs_f64( ) ,
1838+ enable_mmap_cache,
1839+ enable_content_indexing,
18181840 ) ;
18191841 }
18201842
@@ -1897,6 +1919,7 @@ pub(crate) fn warmup_mmaps(
18971919/// so reading further adds no new information to the index.
18981920pub const BIGRAM_CONTENT_CAP : usize = 64 * 1024 ;
18991921
1922+ #[ tracing:: instrument( skip_all, name = "Building Bigram Index" , level = Level :: DEBUG ) ]
19001923pub ( crate ) fn build_bigram_index (
19011924 files : & [ FileItem ] ,
19021925 budget : & ContentCacheBudget ,
0 commit comments