Skip to content
Myr

core: don't build decompression reader unless we intend to use it

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Building it can consume resources. In particular, on Windows, the various binaries are eagerly resolved. I think this originally wasn't done. The eager resolution was added later for security purposes. But the "eager" part isn't actually necessary. It would probably be better to change the decompression reader to do lazy resolution only when the binary is needed. But this will at least avoid doing anything when the `-z/--search-zip` flag isn't used. But when it is, ripgrep will still eagerly resolve all possible binaries. Fixes #2111

authored by Andrew Gallant · committed by Andrew Gallant · parent 5c42c8c · Browse files · 1 file+21 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+21 added−18 removed

  • other:+21 added−18 removed

1 file+21 added−18 removed

modified:crates/core/search.rs+21 added-18 removed
39 unmodified lines
40#[derive(Clone, Debug)]41pub(crate) struct SearchWorkerBuilder {42 config: Config,43 command_builder: grep::cli::CommandReaderBuilder,44 decomp_builder: grep::cli::DecompressionReaderBuilder,44}4546impl Default for SearchWorkerBuilder {47 fn default() -> SearchWorkerBuilder {
3 unmodified lines
5152impl SearchWorkerBuilder {53 /// Create a new builder for configuring and constructing a search worker.54 pub(crate) fn new() -> SearchWorkerBuilder {56 let mut cmd_builder = grep::cli::CommandReaderBuilder::new();57 cmd_builder.async_stderr(true);55 let mut command_builder = grep::cli::CommandReaderBuilder::new();56 command_builder.async_stderr(true);5759 let mut decomp_builder = grep::cli::DecompressionReaderBuilder::new();60 decomp_builder.async_stderr(true);6162 SearchWorkerBuilder {63 config: Config::default(),64 command_builder: cmd_builder,65 decomp_builder,66 }58 SearchWorkerBuilder { config: Config::default(), command_builder }59 }6061 /// Create a new search worker using the given searcher, matcher and62 /// printer.
4 unmodified lines
67 printer: Printer<W>,68 ) -> SearchWorker<W> {69 let config = self.config.clone();70 let command_builder = self.command_builder.clone();79 let decomp_builder = self.decomp_builder.clone();71 let decomp_builder = config.search_zip.then(|| {72 let mut decomp_builder =73 grep::cli::DecompressionReaderBuilder::new();74 decomp_builder.async_stderr(true);75 decomp_builder76 });77 SearchWorker {78 config,79 command_builder,80 decomp_builder,
148 unmodified lines
229#[derive(Clone, Debug)]230pub(crate) struct SearchWorker<W> {231 config: Config,232 command_builder: grep::cli::CommandReaderBuilder,236 decomp_builder: grep::cli::DecompressionReaderBuilder,233 /// This is `None` when `search_zip` is not enabled, since in this case it234 /// can never be used. We do this because building the reader can sometimes235 /// do non-trivial work (like resolving the paths of decompression binaries236 /// on Windows).237 decomp_builder: Option<grep::cli::DecompressionReaderBuilder>,238 matcher: PatternMatcher,239 searcher: grep::searcher::Searcher,240 printer: Printer<W>,241}
31 unmodified lines
273274 /// Returns true if and only if the given file path should be275 /// decompressed before searching.276 fn should_decompress(&self, path: &Path) -> bool {276 if !self.config.search_zip {277 return false;278 }279 self.decomp_builder.get_matcher().has_command(path)277 self.decomp_builder.as_ref().is_some_and(|decomp_builder| {278 decomp_builder.get_matcher().has_command(path)279 })280 }281282 /// Returns true if and only if the given file path should be run through283 /// the preprocessor.
42 unmodified lines
326 /// Attempt to decompress the data at the given file path and search the327 /// result. If the given file path isn't recognized as a compressed file,328 /// then search it without doing any decompression.329 fn search_decompress(&mut self, path: &Path) -> io::Result<SearchResult> {330 let Some(ref decomp_builder) = self.decomp_builder else {331 return self.search_path(path);332 };330 let mut rdr = self.decomp_builder.build(path)?;333 let mut rdr = decomp_builder.build(path)?;334 let result = self.search_reader(path, &mut rdr);335 let close_result = rdr.close();336 let search_result = result?;337 close_result?;
112 unmodified lines