Skip to content

Overlap between IO#seek and #skip #15819

Open
@straight-shoota

Description

@straight-shoota

IO has two methods #seek and #skip which share some common behaviour.
Every IO implements #skip, but only some do #seek: FileDescriptor and IO::Memory.

seek directly mutates the file position, whereas skip actually consumes data and discards it. So it only allows moving forward, while seek has more modes.
But with mode Seek::Current and a positive offset, both methods perform the same operation: move the current position forward by x:
skip(x) is equivalent to seek(x, :current) as long as x >= 0.

This could allow two improvements:

  • Use skip as fallback implementation for seek(x, :current) (with x >= 0) for every IO.
  • Optimize skip by delegating to seek when available. This should have a significant effect for large offsets where we can avoid many successive reads just to progress the position. It might not be an improvement for small offsets, though (especially with IO::Buffered), so the implementation would need to consider some kind of threshold.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions