rust/library/std/src/io
bors 50166d5e5e Auto merge of #98748 - saethlin:optimize-bufreader, r=Mark-Simulacrum
Remove some redundant checks from BufReader

The implementation of BufReader contains a lot of redundant checks. While any one of these checks is not particularly expensive to execute, especially when taken together they dramatically inhibit LLVM's ability to make subsequent optimizations by confusing data flow increasing the code size of anything that uses BufReader.

In particular, these changes have a ~2x increase on the benchmark that this adds a `black_box` to. I'm adding that `black_box` here just in case LLVM gets clever enough to remove the reads entirely. Right now it can't, but these optimizations are really setting it up to do so.

We get this optimization by factoring all the actual buffer management and bounds-checking logic into a new module inside `bufreader` with a new `Buffer` type. This makes it much easier to ensure that we have correctly encapsulated the management of the region of the buffer that we have read bytes into, and it lets us provide a new faster way to do small reads. `Buffer::consume_with` lets a caller do a read from the buffer with a single bounds check, instead of the double-check that's required to use `buffer` + `consume`.

Unfortunately I'm not aware of a lot of open-source usage of `BufReader` in perf-critical environments. Some time ago I tweaked this code because I saw `BufReader` in a profile at work, and I contributed some benchmarks to the `bincode` crate which exercise `BufReader::buffer`. These changes appear to help those benchmarks at little, but all these sorts of benchmarks are kind of fragile so I'm wary of quoting anything specific.
2022-07-27 09:49:06 +00:00
..
buffered Add Buffer::consume_with to enable direct buffer access with one check 2022-07-26 20:16:55 -04:00
cursor attempt to optimise vectored write 2022-06-26 17:15:31 +01:00
error Rename std::io::Error::try_downcast_inner to downcast 2022-07-14 11:24:44 +10:00
impls std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
readbuf implement review suggestions 2021-11-02 22:47:28 -07:00
stdio Make default stdio lock() return 'static handles 2022-02-13 10:23:16 -05:00
util read_buf 2021-11-02 22:47:20 -07:00
copy.rs Warn that platform-specific behavior may change 2022-03-29 19:49:15 -07:00
cursor.rs attempt to optimise vectored write 2022-06-26 17:15:31 +01:00
error.rs Apply suggestions from code review 2022-07-15 13:17:44 -07:00
impls.rs Remove redundant calls to reserve in impl Write for VecDeque 2022-06-09 19:10:09 +02:00
mod.rs Rollup merge of #99716 - sourcelliu:iomut, r=Mark-Simulacrum 2022-07-26 16:57:48 +02:00
prelude.rs Use heading for std::prelude and not io::prelude 2021-01-05 17:52:24 -08:00
readbuf.rs std::io: Modify some ReadBuf method signatures to return &mut Self 2022-05-03 17:52:52 +01:00
stdio.rs Remove mut 2022-07-23 21:22:43 -07:00
tests.rs Rollup merge of #95040 - frank-king:fix/94981, r=Mark-Simulacrum 2022-07-25 18:46:47 +09:00
util.rs add safety comments 2021-11-02 22:47:26 -07:00