Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Seek::seek_relative #281

Closed
fintelia opened this issue Oct 15, 2023 · 1 comment
Closed

Add Seek::seek_relative #281

fintelia opened this issue Oct 15, 2023 · 1 comment
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

Comments

@fintelia
Copy link

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 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

  • Have libraries always wrap their arguments in BufReader. But that would mean the generic code would sometimes make BufReader<Cursor<&[u8]>> and similar.
  • Deprecate BufReader in favor of a BufReader2 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.
@fintelia fintelia added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Oct 15, 2023
@m-ou-se
Copy link
Member

m-ou-se commented Oct 17, 2023

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

@m-ou-se m-ou-se closed this as completed Oct 17, 2023
@dtolnay dtolnay added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants