Skip to content
Myr

searcher: fix performance bug with `-A/--after-context` when searching `stdin`

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

This was a crazy subtle bug where ripgrep could slow down exponentially as increasingly larger values of `-A/--after-context` were used. But, interestingly, this would only occur when searching `stdin` and _not_ when searching the same data as a regular file. This confounded me because ripgrep, pretty early on, erases the difference between searching a single file and `stdin`. So it wasn't like there were different code paths. And I mistakenly assumed that they would otherwise behave the same as they are just treated as streams. But... it turns out that running `read` on a `stdin` versus a regular file seems to behave differently. At least on my Linux system, with `stdin`, `read` never seems to fill the buffer with more than 64K. But with a regular file, `read` pretty reliably fills the caller's buffer with as much space as declared. Of course, it is expected that `read` doesn't *have* to fill up the caller's buffer, and ripgrep is generally fine with that. But when `-A/--after-context` is used with a very large value---big enough that the default buffer capacity is too small---then more heap memory needs to be allocated to correctly handle all cases. This can result in passing buffers bigger than 64K to `read`. While we *correctly* handle `read` calls that don't fill the buffer, it turns out that if we don't fill the buffer, then we get into a pathological case where we aren't processing as many bytes as we could. That is, because of the `-A/--after-context` causing us to keep a lot of bytes around while we roll the buffer and because reading from `stdin` gives us fewer bytes than normal, we weren't amortizing our `read` calls as well as we should have been. Indeed, our buffer capacity increases specifically take this amortization into account, but we weren't taking advantage of it. We fix this by putting `read` into an inner loop that ensures our buffer gets filled up. This fixes the performance bug: ``` $ (time rg ZQZQZQZQZQ bigger.txt --no-mmap -A9999) | wc -l real 1.330 user 0.767 sys 0.559 maxmem 29 MB faults 0 10000 $ cat bigger.txt | (time rg ZQZQZQZQZQ --no-mmap -A9999) | wc -l real 2.355 user 0.860 sys 0.613 maxmem 29 MB faults 0 10000 $ (time rg ZQZQZQZQZQ bigger.txt --no-mmap -A99999) | wc -l real 3.636 user 3.091 sys 0.537 maxmem 29 MB faults 0 100000 $ cat bigger.txt | (time rg ZQZQZQZQZQ --no-mmap -A99999) | wc -l real 4.918 user 3.236 sys 0.710 maxmem 29 MB faults 0 100000 $ (time rg ZQZQZQZQZQ bigger.txt --no-mmap -A999999) | wc -l real 5.430 user 4.666 sys 0.750 maxmem 51 MB faults 0 1000000 $ cat bigger.txt | (time rg ZQZQZQZQZQ --no-mmap -A999999) | wc -l real 6.894 user 4.907 sys 0.850 maxmem 51 MB faults 0 1000000 ``` For comparison, here is GNU grep: ``` $ cat bigger.txt | (time grep ZQZQZQZQZQ -A9999) | wc -l real 1.466 user 0.159 sys 0.839 maxmem 29 MB faults 0 10000 $ cat bigger.txt | (time grep ZQZQZQZQZQ -A99999) | wc -l real 1.663 user 0.166 sys 0.941 maxmem 29 MB faults 0 100000 $ cat bigger.txt | (time grep ZQZQZQZQZQ -A999999) | wc -l real 1.631 user 0.204 sys 0.910 maxmem 29 MB faults 0 1000000 ``` GNU grep is still notably faster. We'll fix that in the next commit. Fixes #3184

authored by Andrew Gallant · committed by Andrew Gallant · parent de2567a · Browse files · 3 files+17 added−10 removed
This commit

State

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

Diff stats

3 files+17 added−10 removed

  • other:+17 added−10 removed

3 files+17 added−10 removed

modified:crates/searcher/src/searcher/glue.rs+2 added-2 removed
735 unmodified lines
736 // The line buffered searcher has slightly different semantics here.737 // Namely, it will *always* detect binary data in the current buffer738 // before searching it. Thus, the total number of bytes searched is739 // smaller than below.740 let exp = "0:a\n\nbyte count:262146\nbinary offset:262153\n";740 let exp = "0:a\n\nbyte count:262142\nbinary offset:262153\n";741 // In contrast, the slice readers (for multi line as well) will only742 // look for binary data in the initial chunk of bytes. After that743 // point, it only looks for binary data in matches. Note though that744 // the binary offset remains the same. (See the binary4 test for a case
25 unmodified lines
770 haystack.push_str("b\x00b\n");771 haystack.push_str("a\x00a\n");772 haystack.push_str("a\n");773774 let exp = "0:a\n\nbyte count:262146\nbinary offset:262149\n";774 let exp = "0:a\n\nbyte count:262142\nbinary offset:262149\n";775 // The binary offset for the Slice readers corresponds to the binary776 // data in `a\x00a\n` since the first line with binary data777 // (`b\x00b\n`) isn't part of a match, and is therefore undetected.778 let exp_slice =
771 unmodified lines
modified:crates/searcher/src/line_buffer.rs+13 added-8 removed
413 unmodified lines
414 self.roll();415 assert_eq!(self.pos, 0);416 loop {417 self.ensure_capacity()?;418 let readlen = rdr.read(self.free_buffer().as_bytes_mut())?;419 if readlen == 0 {420 // We're only done reading for good once the caller has421 // consumed everything.422 self.last_lineterm = self.end;423 return Ok(!self.buffer().is_empty());418 let oldend = self.end;419 while !self.free_buffer().is_empty() {420 let readlen = rdr.read(self.free_buffer())?;421 if readlen == 0 {422 break;423 }424 self.end += readlen;425 }426427 // Get a mutable view into the bytes we've just read. These are428 // the bytes that we do binary detection on, and also the bytes we429 // search to find the last line terminator. We need a mutable slice430 // in the case of binary conversion.430 let oldend = self.end;431 self.end += readlen;431 let newbytes = &mut self.buf[oldend..self.end];432 if newbytes.is_empty() {433 self.last_lineterm = self.end;434 // We're only done reading for good once the caller has435 // consumed everything.436 return Ok(!self.buffer().is_empty());437 }438439 // Binary detection.440 match self.config.binary {441 BinaryDetection::None => {} // nothing to do
529 unmodified lines
modified:CHANGELOG.md+2 added
6 unmodified lines
7* [PERF #2111](https://github.com/BurntSushi/ripgrep/issues/2111):8 Don't resolve helper binaries on Windows when `-z/--search-zip` isn't used.9* [PERF #2865](https://github.com/BurntSushi/ripgrep/pull/2865):10 Avoid using path canonicalization on Windows when emitting hyperlinks.11* [PERF #3184](https://github.com/BurntSushi/ripgrep/pull/3184):12 Improve performance of large values with `-A/--after-context`.1314Bug fixes:1516* [BUG #829](https://github.com/BurntSushi/ripgrep/issues/829),
1777 unmodified lines