fastgrep 0.1.8

Fast parallel grep with SIMD-accelerated search and trigram indexing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::io::BufWriter;
use std::io::Write;
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;

use clap::Parser;
use fastgrep::cli::Cli;
use fastgrep::output::OutputConfig;
use fastgrep::output::TAB_FIELD_WIDTH_STDIN;
use fastgrep::output::format_result;
use fastgrep::output::write_json_size_limit_warning;
use fastgrep::pattern::CompiledPattern;
use fastgrep::searcher::search_file_streaming;
use fastgrep::searcher::search_file_streaming_reuse;
use fastgrep::searcher::search_reader;
use fastgrep::searcher::search_reader_streaming_labeled;
use fastgrep::threadpool::ThreadPool;
use fastgrep::trigram::TrigramIndex;
use fastgrep::trigram::evict_if_needed;
use fastgrep::walker::SkippedFile;
use fastgrep::walker::walk;
use kanal::bounded;

fn main() -> ExitCode {
    let cli = Cli::parse();

    if cli.version {
        println!(
            "grep (fastgrep) {} [index v{}]",
            concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_SHA"), ")"),
            fastgrep::trigram::INDEX_VERSION
        );
        println!("https://crates.io/crates/fastgrep");
        println!();
        println!("features: simd, trigram-index, parallel, ai-agent-optimized");
        return ExitCode::SUCCESS;
    }

    let config = cli.resolve();

    let pattern = match CompiledPattern::compile(&config) {
        Ok(p) => Arc::new(p),
        Err(e) => {
            eprintln!("grep: {e}");
            return ExitCode::from(2);
        }
    };

    let no_messages = config.no_messages;

    let output_config = OutputConfig {
        mode: config.output_mode,
        color: config.color,
        line_number: config.line_number,
        files_with_matches: config.files_with_matches,
        files_without_match: config.files_without_match,
        count: config.count,
        quiet: config.quiet,
        max_count: config.max_count,
        multi_file: config.multi_file,
        max_line_len: config.max_line_len,
        only_matching: config.only_matching,
        after_context: config.after_context,
        before_context: config.before_context,
        byte_offset: config.byte_offset,
        ignore_binary: config.ignore_binary,
        group_separator: config.group_separator.clone(),
        initial_tab: config.initial_tab,
        null: config.null,
        text: config.text,
    };

    if config.stdin {
        return run_stdin(
            &pattern,
            &output_config,
            config.invert_match,
            no_messages,
            config.label.as_deref(),
        );
    }

    // Check for nonexistent paths (matches GNU grep behavior)
    let mut has_path_error = false;
    for path in &config.paths {
        if !path.exists() {
            has_path_error = true;
            if !no_messages {
                eprintln!("grep: {}: No such file or directory", path.display());
            }
        }
    }

    // Fast path: single file, no recursion — skip thread pool/channel overhead
    if config.paths.len() == 1 && !config.recursive {
        let path = &config.paths[0];
        if path.is_file() {
            return run_single_file(
                path,
                &pattern,
                &output_config,
                config.invert_match,
                no_messages,
            );
        }
        if has_path_error {
            return ExitCode::from(2);
        }
    }

    let result = run_files(config, pattern, output_config, no_messages);
    // If there were path errors, exit code 2 takes precedence over "no match" (1)
    if has_path_error && result != ExitCode::SUCCESS {
        return ExitCode::from(2);
    }
    result
}

fn run_single_file(
    path: &std::path::Path,
    pattern: &CompiledPattern,
    output_config: &OutputConfig,
    invert_match: bool,
    no_messages: bool,
) -> ExitCode {
    let stdout = std::io::stdout().lock();
    let mut writer = BufWriter::new(stdout);

    let count = match search_file_streaming(path, pattern, invert_match, output_config, &mut writer)
    {
        Ok(c) => c,
        Err(e) => {
            if e.kind() != std::io::ErrorKind::BrokenPipe && !no_messages {
                eprintln!("grep: {}: {e}", path.display());
            }
            return ExitCode::from(2);
        }
    };

    let _ = writer.flush();
    if count > 0 { ExitCode::SUCCESS } else { ExitCode::from(1) }
}

fn run_stdin(
    pattern: &CompiledPattern,
    output_config: &OutputConfig,
    invert_match: bool,
    no_messages: bool,
    label: Option<&str>,
) -> ExitCode {
    let mut stdin = std::io::stdin().lock();

    // Default stdin label to "(standard input)" when -H is active (matches GNU grep)
    let effective_label =
        if output_config.multi_file { Some(label.unwrap_or("(standard input)")) } else { label };
    let label_path = effective_label.map(std::path::PathBuf::from);
    let effective_config = output_config;

    let has_context = effective_config.before_context > 0 || effective_config.after_context > 0;

    // Use streaming path for context or only-matching modes
    if has_context || effective_config.only_matching {
        let stdout = std::io::stdout().lock();
        let mut writer = BufWriter::new(stdout);
        let label_bytes = effective_label.map(|l| l.as_bytes());
        let count = match search_reader_streaming_labeled(
            &mut stdin,
            pattern,
            invert_match,
            effective_config,
            &mut writer,
            label_bytes,
        ) {
            Ok(c) => c,
            Err(e) => {
                if e.kind() != std::io::ErrorKind::BrokenPipe && !no_messages {
                    eprintln!("grep: (stdin): {e}");
                }
                return ExitCode::from(2);
            }
        };
        let _ = writer.flush();
        return if count > 0 { ExitCode::SUCCESS } else { ExitCode::from(1) };
    }

    let need_ranges = effective_config.requires_match_ranges()
        && !effective_config.files_with_matches
        && !effective_config.count
        && !effective_config.quiet;
    let count_only =
        effective_config.count || effective_config.files_with_matches || effective_config.quiet;
    let mut result = match search_reader(&mut stdin, pattern, invert_match, need_ranges, count_only)
    {
        Ok(r) => r,
        Err(e) => {
            if !no_messages {
                eprintln!("grep: (stdin): {e}");
            }
            return ExitCode::from(2);
        }
    };

    // Apply label to the result path
    if let Some(ref lp) = label_path {
        result.path = lp.clone();
    }

    let found_match = !result.matches.is_empty();

    if !effective_config.quiet {
        let stdout = std::io::stdout().lock();
        let mut writer = BufWriter::new(stdout);
        if let Err(e) = format_result(&result, effective_config, &mut writer, TAB_FIELD_WIDTH_STDIN)
            && e.kind() != std::io::ErrorKind::BrokenPipe
            && !no_messages
        {
            eprintln!("grep: write error: {e}");
        }
        let _ = writer.flush();
    }

    if found_match { ExitCode::SUCCESS } else { ExitCode::from(1) }
}

fn run_files(
    config: fastgrep::cli::ResolvedConfig,
    pattern: Arc<CompiledPattern>,
    output_config: OutputConfig,
    no_messages: bool,
) -> ExitCode {
    let no_index = config.no_index;
    let invert_match = config.invert_match;
    let threads = config.threads;
    let max_file_size = config.max_file_size;

    // --- Trigram index: load and compute candidate filter ---
    let search_root = config
        .paths
        .first()
        .and_then(|p| if config.recursive { std::fs::canonicalize(p).ok() } else { None });

    let (candidate_filter, index_loaded) = if !no_index && let Some(ref root) = search_root {
        let trigrams = pattern.required_trigrams();
        if let Some(index) = TrigramIndex::load(root) {
            if !trigrams.is_empty() && !index.needs_rebuild() {
                let mut candidates = index.candidate_files(&trigrams);
                let total = index.file_count();
                // Skip filtering when trigrams are too common (>= 90% of files match)
                if total > 0 && candidates.len() * 10 >= total * 9 {
                    (None, true)
                } else {
                    // Include stale files so they get searched normally
                    for stale in index.stale_files() {
                        candidates.insert(stale);
                    }
                    (Some(candidates), true)
                }
            } else {
                (None, true)
            }
        } else {
            (None, false)
        }
    } else {
        (None, false)
    };

    let candidate_filter = candidate_filter.map(Arc::new);
    let should_build_index = !no_index && search_root.is_some() && !index_loaded;

    let (path_tx, path_rx) = bounded::<PathBuf>(256);

    // Channel to collect walked paths for index building on first run
    let (walked_send, walked_recv) = if should_build_index {
        let (s, r) = kanal::unbounded::<PathBuf>();
        (Some(s), Some(r))
    } else {
        (None, None)
    };

    let skipped_files: Arc<Mutex<Vec<SkippedFile>>> = Arc::new(Mutex::new(Vec::new()));
    let skipped_for_walker = Arc::clone(&skipped_files);

    let filter_for_walker = candidate_filter.clone();
    let walker_handle = std::thread::Builder::new()
        .name("fg-walker".into())
        .spawn(move || {
            let (tx_inner, rx_inner) = bounded::<PathBuf>(256);
            std::thread::scope(|s| {
                let config_ref = &config;
                let skipped_ref = &skipped_for_walker;
                s.spawn(|| {
                    let walk_threads = (config_ref.threads / 4).clamp(2, 4);
                    walk(config_ref, tx_inner, walk_threads, skipped_ref);
                });
                for p in rx_inner {
                    if let Some(ref filter) = filter_for_walker
                        && !filter.contains(&p)
                    {
                        continue;
                    }
                    if let Some(ref wtx) = walked_send {
                        let _ = wtx.send(p.clone());
                    }
                    let _ = path_tx.send(p);
                }
            });
            // Drop walked_send to close the channel
            drop(walked_send);
        })
        .expect("failed to spawn walker thread");

    // Shared stdout writer behind a mutex — workers flush per-file buffers here.
    let shared_writer = Arc::new(Mutex::new(BufWriter::new(std::io::stdout())));
    let found_match = Arc::new(AtomicBool::new(false));

    let pool = ThreadPool::new(threads, "fg-search", {
        let pattern = Arc::clone(&pattern);
        let shared_writer = Arc::clone(&shared_writer);
        let found_match = Arc::clone(&found_match);
        let output_config = output_config.clone();
        move || {
            let pattern = Arc::clone(&pattern);
            let shared_writer = Arc::clone(&shared_writer);
            let found_match = Arc::clone(&found_match);
            let output_config = output_config.clone();
            // Per-thread buffers: reusable read buffer + output buffer
            let mut read_buf = Vec::with_capacity(256 * 1024);
            let mut out_buf: Vec<u8> = Vec::with_capacity(64 * 1024);
            while let Ok(path) = path_rx.recv() {
                out_buf.clear();
                match search_file_streaming_reuse(
                    &path,
                    &pattern,
                    invert_match,
                    &output_config,
                    &mut out_buf,
                    &mut read_buf,
                ) {
                    Ok(count) => {
                        if count > 0 {
                            found_match.store(true, Ordering::Relaxed);
                        }
                        // For -c/-L mode, always flush (to show file:0 or non-matching files).
                        // For -q mode, never flush (suppress all output).
                        // For other modes, only flush when there are matches.
                        let should_flush = !output_config.quiet
                            && (count > 0
                                || output_config.count
                                || output_config.files_without_match);
                        if should_flush
                            && let Ok(mut w) = shared_writer.lock()
                            && let Err(e) = w.write_all(&out_buf)
                            && e.kind() == std::io::ErrorKind::BrokenPipe
                        {
                            break;
                        }
                    }
                    Err(e) => {
                        if e.kind() != std::io::ErrorKind::BrokenPipe && !no_messages {
                            eprintln!("grep: {}: {e}", path.display());
                        }
                    }
                }
            }
        }
    });

    pool.join();
    if let Ok(mut w) = shared_writer.lock() {
        let _ = w.flush();
    }

    walker_handle.join().ok();

    // Build trigram index after first run
    if should_build_index
        && let Some(ref root) = search_root
        && let Some(rx) = walked_recv
    {
        let paths: Vec<PathBuf> = std::iter::from_fn(|| rx.try_recv().ok().flatten()).collect();
        if !paths.is_empty() {
            let index = TrigramIndex::build(root, &paths);
            let _ = index.save();
            evict_if_needed();
        }
    }

    // Report skipped files to stderr so AI agents can adapt their search
    if !no_messages
        && let Ok(skipped) = skipped_files.lock()
        && !skipped.is_empty()
    {
        if output_config.is_json() {
            let stderr = std::io::stderr().lock();
            let mut writer = BufWriter::new(stderr);
            for sf in skipped.iter() {
                let _ =
                    write_json_size_limit_warning(&mut writer, &sf.path, sf.size, max_file_size);
            }
            let _ = writer.flush();
        } else {
            eprintln!();
            eprintln!("WARNING: {} file(s) skipped due to size limit:", skipped.len());
            for sf in skipped.iter() {
                let size_mb = sf.size as f64 / (1024.0 * 1024.0);
                eprintln!("  - {} ({:.1} MB)", sf.path.display(), size_mb);
            }
            eprintln!();
            eprintln!("These files may cause grep to hang. To search them anyway, re-run with:");
            eprintln!("  FASTGREP_NO_LIMIT=1 grep ...");
            eprintln!("Or adjust the threshold: --max-file-size=<BYTES> (current: 100 MiB)");
        }
    }

    if found_match.load(Ordering::Relaxed) { ExitCode::SUCCESS } else { ExitCode::from(1) }
}