Skip to content

Commit

Permalink
Improve grep performance
Browse files Browse the repository at this point in the history
hyperfine shows 20%+ perf enhancement.
  • Loading branch information
liuchengxu committed Apr 18, 2024
1 parent d7d058f commit 1f9df1f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
18 changes: 13 additions & 5 deletions crates/cli/src/command/grep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@ pub struct Grep {
#[clap(index = 1)]
grep_query: String,

/// Read input from a cached grep tempfile, only absolute file path is supported.
/// Read input from a cached grep tempfile.
///
/// Only absolute file path is supported.
#[clap(long, value_parser)]
input: Option<PathBuf>,

/// Specify the working directory of CMD
/// Specify the working directory of CMD.
#[clap(long, value_parser)]
cmd_dir: Option<PathBuf>,

/// Recreate the cache, only intended for the test purpose.
/// Recreate the grep cache.
///
/// Only intended for the test purpose.
#[clap(long)]
refresh_cache: bool,

/// Run the filter in parallel.
///
/// Deprecated.
#[clap(long)]
par_run: bool,

/// Use the builtin searching implementation on top of libripgrep instead of the rg executable.
#[clap(long)]
ripgrep: bool,
lib_ripgrep: bool,
}

impl Grep {
Expand All @@ -55,7 +63,7 @@ impl Grep {
return Ok(());
}

if self.ripgrep {
if self.lib_ripgrep {
let dir = match self.cmd_dir {
Some(ref dir) => dir.clone(),
None => std::env::current_dir()?,
Expand Down
20 changes: 10 additions & 10 deletions crates/maple_core/src/searcher/grep/stoppable_searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ impl StoppableSearchImpl {
return WalkState::Quit;
}

let entry = match entry {
Ok(entry) => entry,
Err(_) => return WalkState::Continue,
let Ok(entry) = entry else {
return WalkState::Continue;
};

// TODO: Add search syntax for filtering path
Expand All @@ -109,6 +108,11 @@ impl StoppableSearchImpl {
_ => return WalkState::Continue,
};

let relative_path = entry
.path()
.strip_prefix(&search_root)
.unwrap_or_else(|_| entry.path());

let result = searcher.search_path(
&MatchEverything,
entry.path(),
Expand All @@ -118,14 +122,10 @@ impl StoppableSearchImpl {
return Ok(sender.send(SearcherMessage::ProcessedOne).is_ok());
}

let path = entry
.path()
.strip_prefix(&search_root)
.unwrap_or_else(|_| entry.path());
let line = line.trim();
let maybe_file_result =
matcher
.match_file_result(path, line)
.match_file_result(relative_path, line)
.map(|matched| FileResult {
path: entry.path().to_path_buf(),
line_number,
Expand All @@ -147,7 +147,7 @@ impl StoppableSearchImpl {
);

if let Err(err) = result {
tracing::error!("Global search error: {}, {}", entry.path().display(), err);
tracing::error!(?err, path = ?entry.path(), "Global search error");
}

WalkState::Continue
Expand Down Expand Up @@ -289,7 +289,7 @@ pub async fn search(query: String, matcher: Matcher, search_context: SearchConte
.expect("Max capacity is non-zero; qed");

let new = file_result;
if let std::cmp::Ordering::Greater = new.rank.cmp(&last.rank) {
if new.rank > last.rank {
*last = new;
best_results.sort();
}
Expand Down
3 changes: 3 additions & 0 deletions crates/maple_core/src/stdio_server/provider/impls/blines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ impl BlinesProvider {

let new_control = {
let stop_signal = Arc::new(AtomicBool::new(false));
let vim = ctx.vim.clone();

let join_handle = {
let search_context = ctx.search_context(stop_signal.clone());

tokio::spawn(async move {
let _ = vim.bare_exec("clap#spinner#set_busy");
crate::searcher::file::search(query, source_file, matcher, search_context)
.await;
let _ = vim.bare_exec("clap#spinner#set_idle");
})
};

Expand Down
39 changes: 18 additions & 21 deletions crates/maple_core/src/stdio_server/provider/impls/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,24 @@ impl GrepProvider {
.match_scope(MatchScope::Full) // Force using MatchScope::Full.
.build(Query::from(&query));

let new_control = {
let stop_signal = Arc::new(AtomicBool::new(false));

let vim = ctx.vim.clone();
let mut search_context = ctx.search_context(stop_signal.clone());
// cwd + extra paths
if self.args.base.no_cwd {
search_context.paths = self.args.paths.clone();
} else {
search_context.paths.extend_from_slice(&self.args.paths);
}
let join_handle = tokio::spawn(async move {
let _ = vim.bare_exec("clap#spinner#set_busy");
crate::searcher::grep::search(query, matcher, search_context).await;
let _ = vim.bare_exec("clap#spinner#set_idle");
});

SearcherControl {
stop_signal,
join_handle,
}
let vim = ctx.vim.clone();
let stop_signal = Arc::new(AtomicBool::new(false));
let mut search_context = ctx.search_context(stop_signal.clone());
// cwd + extra paths
if self.args.base.no_cwd {
search_context.paths = self.args.paths.clone();
} else {
search_context.paths.extend_from_slice(&self.args.paths);
}
let join_handle = tokio::spawn(async move {
let _ = vim.bare_exec("clap#spinner#set_busy");
crate::searcher::grep::search(query, matcher, search_context).await;
let _ = vim.bare_exec("clap#spinner#set_idle");
});

let new_control = SearcherControl {
stop_signal,
join_handle,
};

self.searcher_control.replace(new_control);
Expand Down

0 comments on commit 1f9df1f

Please sign in to comment.