Description
Proposal
Problem statement
Rust encourages users to use BufReader
to accelerate small I/O operations on Read
implementations that have high per-operation overhead. BufReader
's Seek
implementation however has a performance footgun: it always dumps the internal buffer which potentially makes performance even worse than using no buffering at all.
The discussion of this issue in rust-lang/rust#31100 eventually resulted in the addition of BufReader::seek_relative
. That method enables seeking on a BufReader
without dumping the internal buffer.
Unfortunately, it isn't available to generic code parameterized on R: Read + Seek
.
Motivating examples or use cases
A natural approach to writing an image decoding library is to take a R: Read + Seek
which enables the user to provide any type implementing Read
and Seek
.
The decoding process itself will generally involve many small read_exact
calls, along with some seek
s. Depending on the format, seeks may be to absolute positions in the file, or relative seeks past certain sections (or some combination). Nonetheless, by tracking stream position, the absolute seeks can be done as relative seeks instead.
This presents two performance pitfalls:
- If the user passes an un-buffered
File
object theread_exact
calls will be slow. - If they pass a
BufReader<File>
seeking will constantly be dumping the buffer.
Rust users are generally aware about (1), and library documentation can reinforce the proper solution. However, there isn't a clear way to avoid (2) because BufReader::seek_relative
isn't callable given a generic impl Seek
.
Solution sketch
Add a seek_relative
method to the Seek
trait with the same signature as the method on BufReader
.
Alternatives
- Have libraries always wrap their arguments in
BufReader
. But that would mean the generic code would sometimes makeBufReader<Cursor<&[u8]>>
and similar. - Deprecate
BufReader
in favor of aBufReader2
or similar that had reasonable semantics for seeking. - Maybe some sort of wrapper struct could work?
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.