You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It simply reads into a buffer, and discards it. GenericReader.skipBytes just calls this function.
Many readers can implement skipping efficiently. For file readers, skipping can be done by simply incrementing the file pointer, which may be done without actually calling into the operating system. For the BufferedReader, skipping can by done by simply incrementing the index into the buffer. The current implementation of skipBytes, therefore, leads to unnecessary memory copies and system calls.
I propose we add a user-provided skip function to the Reader interface. We can also provide a default implementation, similar to the current one:
pubfnGenericReader(
comptimeContext: type,
comptimeReadError: type,
/// Returns the number of bytes read. It may be less than buffer.len./// If the number of bytes read is 0, it means end of stream./// End of stream is not an error condition.comptimereadFn: fn (context: Context, buffer: []u8) ReadError!usize,
comptimeSkipError: type,
/// Returns the number of bytes skipped. It may be less than amount./// If the number of bytes skipped is 0, it means end of stream./// End of stream is not an error condition.comptimeskipFn: fn(context: Context, amount: usize) SkipError!usize,
) type {
returnstruct{
pubfnskip(self: @This(), amount: usize) SkipError!usize{
returnskipFn(self.ctx, amount);
}
// Add the similar conveniences to read: skipAll, skipNoEof.
};
}
pubfnGenericReaderDefaultSkip(
comptimeContext: type,
comptimeReadError: type,
/// Returns the number of bytes read. It may be less than buffer.len./// If the number of bytes read is 0, it means end of stream./// End of stream is not an error condition.comptimereadFn: fn (context: Context, buffer: []u8) ReadError!usize,
) type {
constSkip=struct{
pubfnskipBytes(context: Context, num_bytes: u64) ReadError!usize{
varbuf: [512]u8=undefined;
constamt=@min(num_bytes, buf.len);
returnreadFn(context, buf[0..amt]);
}
};
returnGenericReader(Context, ReadError, readFn, ReadError, Skip.skipBytes);
}
The problem is that many readers are not fully seekable, but can still skip efficiently. If we were to require a SeekableStream in order to get an efficient skip, we would end up with more complexity, because we would have to pass around one more object, and we would prevent the use of readers that don't have a matching SeekableStream.
This is current implementation of
AnyReader.skipBytes
.It simply reads into a buffer, and discards it.
GenericReader.skipBytes
just calls this function.Many readers can implement skipping efficiently. For file readers, skipping can be done by simply incrementing the file pointer, which may be done without actually calling into the operating system. For the
BufferedReader
, skipping can by done by simply incrementing the index into the buffer. The current implementation ofskipBytes
, therefore, leads to unnecessary memory copies and system calls.I propose we add a user-provided skip function to the
Reader
interface. We can also provide a default implementation, similar to the current one:AnyReader
then would get an extra field:The text was updated successfully, but these errors were encountered: