rust-lang / libs-team

The home of the library team
Apache License 2.0
115 stars 18 forks source link

Add Seek::seek_relative #281

Closed fintelia closed 10 months ago

fintelia commented 10 months ago

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 https://github.com/rust-lang/rust/issues/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 seeks. 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:

  1. If the user passes an un-buffered File object the read_exact calls will be slow.
  2. 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

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:

m-ou-se commented 10 months ago

Thanks! We briefly discussed this in the meeting just now. This looks fine, please go ahead in adding it as unstable.