Skip to content
Myr

searcher: fix regression with `--line-buffered` flag

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

In my fix for #3184, I actually had two fixes. One was a tweak to how we read data and the other was a tweak to how we determined how much of the buffer we needed to keep around. It turns out that fixing #3184 only required the latter fix, found in commit d4b77a8d8967ce1bf701ec65ceb9a75e85e5f2e0. The former fix also helped the specific case of #3184, but it ended up regressing `--line-buffered`. Specifically, previous to 8c6595c215d1e24bed5b7b86e2b18f3c871439ef (the first fix), we would do one `read` syscall. This call might not fill our caller provided buffer. And in particular, `stdin` seemed to fill fewer bytes than reading from a file. So the "fix" was to put `read` in a loop and keep calling it until the caller provided buffer was full or until the stream was exhausted. This helped alleviate #3184 by amortizing `read` syscalls better. But of course, in retrospect, this change is clearly contrary to how `--line-buffered` works. We specifically do _not_ want to wait around until the buffer is full. We want to read what we can, search it and move on. So this reverts the first fix but leaves the second, which still keeps #3184 fixed and also fixes #3194 (the regression). This reverts commit 8c6595c215d1e24bed5b7b86e2b18f3c871439ef. Fixes #3194

authored by Andrew Gallant · committed by Andrew Gallant · parent 38d6302 · Browse files · 3 files+17 added−19 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−19 removed

  • other:+17 added−19 removed

3 files+17 added−19 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:262142\nbinary offset:262153\n";740 let exp = "0:a\n\nbyte count:262146\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:262142\nbinary offset:262149\n";774 let exp = "0:a\n\nbyte count:262146\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+8 added-13 removed
413 unmodified lines
414 self.roll();415 assert_eq!(self.pos, 0);416 loop {417 self.ensure_capacity()?;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;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());424 }425426 // Get a mutable view into the bytes we've just read. These are427 // the bytes that we do binary detection on, and also the bytes we428 // search to find the last line terminator. We need a mutable slice429 // in the case of binary conversion.430 let oldend = self.end;431 self.end += readlen;432 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 }433434 // Binary detection.435 match self.config.binary {436 BinaryDetection::None => {} // nothing to do
529 unmodified lines
modified:CHANGELOG.md+7 added-4 removed
1TBD2===115.0.12======3Unreleased changes. Release notes have not yet been written.45Bug fixes:67* [BUG #3194](https://github.com/BurntSushi/ripgrep/issues/3194):8 Fix a regression with `--line-buffered` introduced in ripgrep 15.0.0.9101115.0.0 (2025-10-15)12===================13ripgrep 15 is a new major version release of ripgrep that mostly has bug fixes,14some minor performance improvements and minor new features. Here are some
26 unmodified lines
41* [PERF #2111](https://github.com/BurntSushi/ripgrep/issues/2111):42 Don't resolve helper binaries on Windows when `-z/--search-zip` isn't used.43* [PERF #2865](https://github.com/BurntSushi/ripgrep/pull/2865):44 Avoid using path canonicalization on Windows when emitting hyperlinks.40* [PERF #3184](https://github.com/BurntSushi/ripgrep/pull/3184):41 Improve performance of large values with `-A/--after-context`.4546Bug fixes:4748* [BUG #829](https://github.com/BurntSushi/ripgrep/issues/829),
1779 unmodified lines