summary refs log tree commit diff
path: root/library/std/src/io/buffered/bufreader.rs
AgeCommit message (Collapse)AuthorLines
2023-11-19Rollup merge of #116750 - fintelia:seek_seek_relative, r=Mark-SimulacrumTakayuki Maeda-0/+10
Add Seek::seek_relative The `BufReader` struct has a `seek_relative` method because its `Seek::seek` implementation involved dumping the internal buffer (https://github.com/rust-lang/rust/issues/31100). Unfortunately, there isn't really a good way to take advantage of that method in generic code. This PR adds the same method to the main `Seek` trait with the straightforward default method, and an override for `BufReader` that calls its implementation. _Also discussed in [this](https://internals.rust-lang.org/t/add-seek-seek-relative/19546) internals.rust-lang.org thread._
2023-10-20Specialize `Bytes<R>::next` when `R` is a `BufReader`.Nicholas Nethercote-5/+20
This reduces the runtime for a simple program using `Bytes::next` to iterate through a file from 220ms to 70ms on my Linux box.
2023-10-14Add Seek::seek_relativeJonathan Behrens-0/+10
2023-07-06Rollup merge of #112008 - intruder-kat:master, r=NilstriebMichael Goulet-1/+1
Fix incorrect documented default bufsize in bufreader/writer
2023-06-17Extend io::copy buffer reuse to BufReader tooThe 8472-1/+1
previously it was only able to use BufWriter. This was due to a limitation in the BufReader generics that prevented specialization. This change works around the issue by using `where Self: Read` instead of `where I: Read`. This limits our options, e.g. we can't access BufRead methods, but it happens to work out if we rely on some implementation details.
2023-05-26Fix incorrect documented default bufsize in bufreader/writerKathryn R-1/+1
2023-05-01Relax implicit `R: Sized` bound on `BufReader<R>`Maybe Waffle-12/+15
2022-10-06Avoid defensive re-initialization of the BufReader bufferBen Kimock-0/+8
2022-08-18Address reviewer commentsNick Cameron-2/+2
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-05non-linux platformsNick Cameron-3/+2
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-04std::io: migrate ReadBuf to BorrowBuf/BorrowCursorNick Cameron-7/+8
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-07-26Add Buffer::consume_with to enable direct buffer access with one checkBen Kimock-3/+1
2022-07-24Rename and document the new BufReader internalsBen Kimock-7/+7
2022-07-24Remove some redundant checks from BufReaderBen Kimock-53/+30
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.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-02-04Hide Repr details from io::Error, and rework `io::Error::new_const`.Thom Chiovoloni-2/+2
2021-11-02implement review suggestionsDrMeepster-1/+1
2021-11-02consolidate 2 unsafe blocks into 1DrMeepster-2/+2
2021-11-02read_bufDrMeepster-16/+43
2021-10-09Apply clippy suggestionsClemens Wasser-6/+5
2021-10-07Optimize File::read_to_end and read_to_stringJohn Kugelman-0/+45
Reading a file into an empty vector or string buffer can incur unnecessary `read` syscalls and memory re-allocations as the buffer "warms up" and grows to its final size. This is perhaps a necessary evil with generic readers, but files can be read in smarter by checking the file size and reserving that much capacity. `std::fs::read` and `read_to_string` already perform this optimization: they open the file, reads its metadata, and call `with_capacity` with the file size. This ensures that the buffer does not need to be resized and an initial string of small `read` syscalls. However, if a user opens the `File` themselves and calls `file.read_to_end` or `file.read_to_string` they do not get this optimization. ```rust let mut buf = Vec::new(); file.read_to_end(&mut buf)?; ``` I searched through this project's codebase and even here are a *lot* of examples of this. They're found all over in unit tests, which isn't a big deal, but there are also several real instances in the compiler and in Cargo. I've documented the ones I found in a comment here: https://github.com/rust-lang/rust/issues/89516#issuecomment-934423999 Most telling, the `Read` trait and the `read_to_end` method both show this exact pattern as examples of how to use readers. What this says to me is that this shouldn't be solved by simply fixing the instances of it in this codebase. If it's here it's certain to be prevalent in the wider Rust ecosystem. To that end, this commit adds specializations of `read_to_end` and `read_to_string` directly on `File`. This way it's no longer a minor footgun to start with an empty buffer when reading a file in. A nice side effect of this change is that code that accesses a `File` as a bare `Read` constraint or via a `dyn Read` trait object will benefit. For example, this code from `compiler/rustc_serialize/src/json.rs`: ```rust pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> { let mut contents = Vec::new(); match rdr.read_to_end(&mut contents) { ``` Related changes: - I also added specializations to `BufReader` to delegate to `self.inner`'s methods. That way it can call `File`'s optimized implementations if the inner reader is a file. - The private `std::io::append_to_string` function is now marked `unsafe`. - `File::read_to_string` being more efficient means that the performance note for `io::read_to_string` can be softened. I've added @camelid's suggested wording from: https://github.com/rust-lang/rust/issues/80218#issuecomment-936806502
2021-09-25Apply 16 commits (squashed)Frank Steffahn-4/+4
---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::fmt ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::{rc, sync} ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::string ---------- Fix spacing for links inside code blocks in alloc::vec ---------- Fix spacing for links inside code blocks in core::option ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in core::result ---------- Fix spacing for links inside code blocks in core::{iter::{self, iterator}, stream::stream, poll} ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{fs, path} ---------- Fix spacing for links inside code blocks in std::{collections, time} ---------- Fix spacing for links inside code blocks in and make formatting of `&str`-like types consistent in std::ffi::{c_str, os_str} ---------- Fix spacing for links inside code blocks, and improve link tooltips in std::ffi ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{io::{self, buffered::{bufreader, bufwriter}, cursor, util}, net::{self, addr}} ---------- Fix typo in link to `into` for `OsString` docs ---------- Remove tooltips that will probably become redundant in the future ---------- Apply suggestions from code review Replacing `…std/primitive.reference.html` paths with just `reference` Co-authored-by: Joshua Nelson <github@jyn.dev> ---------- Also replace `…std/primitive.reference.html` paths with just `reference` in `core::pin`
2021-06-10Specialize `io::Bytes::size_hint` for more typesBenoît du Garreau-1/+7
2021-03-21Bump stable version of bufreader_seek_relative.Mara Bos-1/+1
2021-03-10Stabilize `bufreader_seek_relative`philippeitis-1/+1
2021-03-05Rollup merge of #82728 - calebsander:refactor/bufreader-buf, r=m-ou-seMara-4/+3
Avoid unnecessary Vec construction in BufReader As mentioned in #80460, creating a `Vec` and calling `Vec::into_boxed_slice()` emits unnecessary calls to `realloc()` and `free()`. Updated the code to use `Box::new_uninit_slice()` to create a boxed slice directly. I think this also makes it more explicit that the initial contents of the buffer are uninitialized. r? ``@m-ou-se``
2021-03-03Avoid unnecessary Vec construction in BufReaderCaleb Sander-4/+3
2021-01-31Remove trailing newlineXavientois-2/+3
2021-01-31Implement SizeHint trait for BufReader, Emtpy, and ChainXavientois-1/+8
2021-01-24Stabilize `Seek::stream_position` & change feature of `Seek::stream_len`Lukas Kalbertodt-1/+0
2021-01-17Add benchmark and fast path for BufReader::read_exactBen Kimock-0/+14
2020-11-05document HACKsPeter Jaszkowiak-0/+1
2020-11-05Intra-doc links for std::io::bufferedPeter Jaszkowiak-1/+1
2020-09-10Refactor io/buffered.rs into submodulesNathan West-0/+423