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
19 changes: 10 additions & 9 deletions src/file_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,23 @@ pub fn does_file_match_regexp(mut file: &fs::File, re: &Regex) -> bool {
re.is_match(&buf)
}

pub fn does_file_match_query(mut file: &fs::File, query: &str) -> bool {
pub fn does_file_match_query(mut file: &fs::File, finder: &memmem::Finder<'_>) -> bool {
let mut full: Vec<u8> = vec![];
let mut buf = [0u8; 2048];
let finder = memmem::Finder::new(query);
// Keep needle.len()-1 bytes of overlap between chunks so matches that
// span a chunk boundary are not missed.
let overlap = finder.needle().len().saturating_sub(1);
loop {
let bytes = file.read(&mut buf);
if bytes.unwrap_or(0) == 0 {
let n = file.read(&mut buf).unwrap_or(0);
if n == 0 {
break false;
}
if full.contains(&0xA) {
let mut split_full = full.rsplit(|&b| b == b'\n');
full = split_full.next().unwrap_or(&[0u8, 1]).to_vec();
}
full.extend(buf);
full.extend_from_slice(&buf[..n]);
if finder.find(&full).is_some() {
break true;
}
if full.len() > overlap {
full.drain(..full.len() - overlap);
}
}
}
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use clap::Parser;
use colored::Colorize;
use ignore::{WalkBuilder, WalkState};
use memchr::memmem;
use regex::Regex;
use serde::Serialize;
use std::error::Error;
Expand Down Expand Up @@ -237,7 +238,7 @@ impl Config {

let num_threads = match args.threads {
Some(threads) => threads,
None => NonZero::new(5).expect("Default number of threads was invalid"),
None => std::thread::available_parallelism().unwrap_or(NonZero::new(5).unwrap()),
};

let config = Config {
Expand Down Expand Up @@ -620,6 +621,8 @@ impl Searcher {
let config = Arc::clone(&config_walk);
let should_quit = Arc::clone(&should_quit_walk);
let searched_file_count = Arc::clone(&searched_file_count_walk);
// Build the memmem Finder once per thread rather than once per file.
let finder = memmem::Finder::new(config.query.as_bytes()).into_owned();

Box::new(move |entry| {
if should_quit.load(Ordering::Relaxed) {
Expand All @@ -641,7 +644,7 @@ impl Searcher {
}
searched_file_count.fetch_add(1, Ordering::Relaxed);
let tx_file = tx.clone();
search_file(&re, &path_str, &config, move |results| {
search_file(&re, &path_str, &config, &finder, move |results| {
let _ = tx_file.send(results);
});
WalkState::Continue
Expand Down Expand Up @@ -712,7 +715,7 @@ fn debug(config: &Config, output: &str) {
}
}

fn search_file<F>(re: &Regex, file_path: &str, config: &Config, callback: F)
fn search_file<F>(re: &Regex, file_path: &str, config: &Config, finder: &memmem::Finder<'_>, callback: F)
where
F: FnOnce(Vec<SearchResult>) + Send + 'static,
{
Expand All @@ -726,7 +729,7 @@ where
if match config.search_method {
SearchMethod::PrescanRegex => !file_type::does_file_match_regexp(&file, re),
SearchMethod::PrescanMemmem => {
!file_type::does_file_match_query(&file, &config.query)
!file_type::does_file_match_query(&file, finder)
}
SearchMethod::NoPrescan => false,
} {
Expand Down Expand Up @@ -765,7 +768,8 @@ fn search_file_line_by_line(
file: &fs::File,
config: &Config,
) -> Vec<SearchResult> {
let lines = io::BufReader::new(file).lines();
// 64 KB buffer reduces syscalls by up to 8x compared to the default 8 KB.
let lines = io::BufReader::with_capacity(64 * 1024, file).lines();
let mut line_counter = 0;

lines
Expand Down
Loading