Add Seek::seek_relative #281
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
Rust encourages users to use
BufReader
to accelerate small I/O operations onRead
implementations that have high per-operation overhead.BufReader
'sSeek
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 aBufReader
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 implementingRead
andSeek
.The decoding process itself will generally involve many small
read_exact
calls, along with someseek
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:
File
object theread_exact
calls will be slow.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 genericimpl Seek
.Solution sketch
Add a
seek_relative
method to theSeek
trait with the same signature as the method onBufReader
.Alternatives
BufReader
. But that would mean the generic code would sometimes makeBufReader<Cursor<&[u8]>>
and similar.BufReader
in favor of aBufReader2
or similar that had reasonable semantics for seeking.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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: