Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/fff-core/src/file_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ impl FilePicker {
SEARCH_THREAD_POOL.install(|| {
grep_search(
self.get_files(),
self.live_file_count(),
query,
options,
self.cache_budget(),
Expand Down
14 changes: 12 additions & 2 deletions crates/fff-core/src/grep/fuzzy_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub(super) fn fuzzy_grep_search<'a>(
arena: ArenaPtr,
overflow_arena: ArenaPtr,
) -> GrepResult<'a> {
#[cfg(unix)]
let base_dir = std::fs::File::open(base_path).ok();
#[cfg(not(unix))]
let base_dir = None;
// max_typos controls how many *needle* characters can be unmatched.
// A transposition (e.g. "shcema" -> "schema") costs ~1 typo with
// default gap penalties. We scale max_typos by needle length:
Expand Down Expand Up @@ -169,8 +173,14 @@ pub(super) fn fuzzy_grep_search<'a>(
arena
};

let file_bytes =
file.get_content_for_search(buf, mmap_slot, file_arena, base_path, budget)?;
let file_bytes = file.get_content_for_search(
buf,
mmap_slot,
file_arena,
base_path,
budget,
base_dir.as_ref(),
)?;

if min_chars_required > 0 {
let mut chars_found = 0usize;
Expand Down
36 changes: 23 additions & 13 deletions crates/fff-core/src/grep/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::index::{
regex_candidates,
};
use crate::simd_string_utils::memmem;
use crate::types::{ContentCacheBudget, FileItem, FileSliceExt, MmapSlot};
use crate::types::{ContentCacheBudget, FileItem, MmapSlot};
use fff_grep::{
Searcher, SearcherBuilder, Sink, SinkMatch,
matcher::{Match, Matcher, NoError},
Expand Down Expand Up @@ -191,6 +191,7 @@ impl Sink for PlainTextSink<'_> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn grep_search<'a>(
files: &'a [FileItem],
total_files: usize,
query: &FFFQuery<'_>,
options: &GrepSearchOptions,
budget: &ContentCacheBudget,
Expand All @@ -203,6 +204,7 @@ pub(crate) fn grep_search<'a>(
) -> GrepResult<'a> {
let result = grep_search_parsed(
files,
total_files,
query,
options,
budget,
Expand Down Expand Up @@ -248,6 +250,7 @@ pub(crate) fn grep_search<'a>(

let mut fallback = grep_search_parsed(
files,
total_files,
&literal_query,
options,
budget,
Expand All @@ -270,6 +273,7 @@ pub(crate) fn grep_search<'a>(
#[allow(clippy::too_many_arguments)]
fn grep_search_parsed<'a>(
files: &'a [FileItem],
total_files: usize,
query: &FFFQuery<'_>,
options: &GrepSearchOptions,
budget: &ContentCacheBudget,
Expand All @@ -280,7 +284,6 @@ fn grep_search_parsed<'a>(
arena: crate::simd_path::ArenaPtr,
overflow_arena: crate::simd_path::ArenaPtr,
) -> GrepResult<'a> {
let total_files = files.live_count();
let constraints_from_query = &query.constraints[..];

let grep_text = extract_grep_text(query);
Expand Down Expand Up @@ -377,7 +380,10 @@ fn grep_search_parsed<'a>(
);

if files_to_search.is_empty() {
return GrepResult::empty(total_files, filtered_file_count);
return GrepResult {
regex_fallback_error,
..GrepResult::empty(total_files, filtered_file_count)
};
}

// `PlainTextMatcher` is used by the grep-searcher engine for line detection.
Expand Down Expand Up @@ -557,6 +563,10 @@ where
};

let search_start = std::time::Instant::now();
#[cfg(unix)]
let base_dir = std::fs::File::open(ctx.base_path).ok();
#[cfg(not(unix))]
let base_dir = None;
let page_limit = options.page_limit;
// Lowest index an abort skipped; everything below it was searched, so it is
// the resume point for the next page. usize::MAX means no abort happened.
Expand All @@ -568,29 +578,22 @@ where
let mut page_filled = false;
let mut aborted = false;

// Each chunk is a rayon barrier. A flat small chunk over 500k files = ~7800
// barriers; x2 growth makes it logarithmic. But a too-aggressive growth
// over-scans: when a page fills mid-chunk, the whole submitted chunk still
// runs.
//
// So only grow when the prefilter is weak (large candidate set);
// when bigram cut the set in half, keep fixed small chunks for cheap page-fill termination.
// Grow empty batches to avoid repeated barriers. A strong prefilter caps
// growth low (bounds over-scan past a filled page) and resets on a match.
let base_chunk = rayon::current_num_threads() * 4;
let prefilter_strong = ctx.total_files > 0 && files_to_search.len() * 2 < ctx.total_files;
let max_chunk = if prefilter_strong {
base_chunk
base_chunk * 8
} else {
(base_chunk * 256).max(8 * 1024)
};
let growth = if prefilter_strong { 1 } else { 2 };
let mut chunk_size = base_chunk;
let mut chunk_start = 0;

while chunk_start < files_to_search.len() {
let chunk_end = (chunk_start + chunk_size).min(files_to_search.len());
let chunk = &files_to_search[chunk_start..chunk_end];
chunk_start = chunk_end;
chunk_size = (chunk_size * growth).min(max_chunk);
let chunk_offset = files_consumed;

// Unless enforced, the budget stays dormant until something matched.
Expand Down Expand Up @@ -624,6 +627,7 @@ where
ctx.arena_for_file(file),
ctx.base_path,
ctx.budget,
base_dir.as_ref(),
)?;

// Fast whole-file memmem check before entering the
Expand All @@ -647,6 +651,12 @@ where
.flatten()
.collect();

chunk_size = if prefilter_strong && !chunk_results.is_empty() {
base_chunk
} else {
(chunk_size * 2).min(max_chunk)
};

// Every file in the chunk was visited unless an abort cut it short.
let resume_at = first_skipped.load(Ordering::Relaxed);
aborted = resume_at != usize::MAX;
Expand Down
103 changes: 103 additions & 0 deletions crates/fff-core/src/grep/grep_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,109 @@ use crate::index::BigramIndexBuilder;
use std::io::Write;
use std::sync::atomic::AtomicBool;

#[test]
fn invalid_regex_error_survives_empty_literal_prefilter() {
let root = tempfile::tempdir().unwrap();
for index in 0..128 {
std::fs::write(
root.path().join(format!("file_{index}.txt")),
"ordinary text",
)
.unwrap();
}
std::fs::write(root.path().join("file_0.txt"), "Qq marker").unwrap();
std::fs::write(root.path().join("file_1.txt"), "Zz marker").unwrap();
let mut picker = FilePicker::new(FilePickerOptions {
base_path: root.path().to_string_lossy().into_owned(),
watch: false,
..Default::default()
})
.unwrap();
picker.collect_files().unwrap();
let query = parse_grep_query("QqZz(");
let options = GrepSearchOptions {
mode: GrepMode::Regex,
..Default::default()
};
let before = picker.grep(&query, &options);
assert!(before.matches.is_empty());
assert!(before.regex_fallback_error.is_some());

let builder = BigramIndexBuilder::new(picker.get_files().len());
let skip = BigramIndexBuilder::new(picker.get_files().len());
for (index, file) in picker.get_files().iter().enumerate() {
let content = std::fs::read(root.path().join(file.relative_path(&picker))).unwrap();
builder.add_file_content(&skip, index, &content);
}
picker.set_bigram_index(builder.compress(None));
let after = picker.grep(&query, &options);
assert!(after.matches.is_empty());
assert_eq!(after.total_files_searched, 0);
assert!(after.regex_fallback_error.is_some());
}

#[test]
fn sparse_matches_after_empty_batches_preserve_pagination() {
let root = tempfile::tempdir().unwrap();
for index in 0..1536 {
let extension = if index < 512 { "rs" } else { "png" };
let path = root.path().join(format!("file_{index}.{extension}"));
let content = if [32, 100, 180, 240].contains(&index) {
"needle\n"
} else {
"ordinary content\n"
};
std::fs::write(&path, content).unwrap();
// `File::open` is read-only and Windows requires write access to set file times.
std::fs::OpenOptions::new()
.write(true)
.open(path)
.unwrap()
.set_times(std::fs::FileTimes::new().set_modified(
std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000 + index),
))
.unwrap();
}
let mut picker = FilePicker::new(FilePickerOptions {
base_path: root.path().to_string_lossy().into_owned(),
watch: false,
..Default::default()
})
.unwrap();
picker.collect_files().unwrap();

for mode in [GrepMode::PlainText, GrepMode::Regex] {
let query = parse_grep_query("needle *.rs");
let mut options = GrepSearchOptions {
mode,
page_limit: usize::MAX,
..Default::default()
};
let result = picker.grep(&query, &options);
let expected: Vec<_> = result
.files
.iter()
.map(|file| file.relative_path(&picker))
.collect();
assert_eq!(expected.len(), 4);
assert_eq!(result.filtered_file_count, 512);

options.page_limit = 1;
let mut actual = Vec::new();
loop {
let page = picker.grep(&query, &options);
actual.extend(page.files.iter().map(|file| file.relative_path(&picker)));
assert_eq!(page.total_files, 1536);
if page.next_file_offset == 0 {
break;
}
assert!(page.next_file_offset > options.file_offset);
options.file_offset = page.next_file_offset;
}
assert_eq!(actual, expected);
}
}

#[test]
fn test_replace_newline_escapes() {
// Single \n → multiline: replaced with a real newline at byte 3
Expand Down
Loading
Loading