Open
Description
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 forseek(x, :current)
(withx >= 0
) for every IO. - Optimize
skip
by delegating toseek
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 withIO::Buffered
), so the implementation would need to consider some kind of threshold.