Skip to content

Performance penalty of using std.io.Reader #17985

@karlseguin

Description

@karlseguin

Zig Version

0.12.0-dev.1396+f6de3ec96

Steps to Reproduce and Observed Behavior

The implementation of std.io.Reader.streamUntilDelimiter is essentially:

const byte: u8 = try self.readByte();
if (byte == delimiter) return;
try writer.writeByte(byte);

Which is a reasonable default implementation, but depending on the underlying context, can leave a lot of performance on the table. Specifically, I noticed that reading a large file, line by line, was really slow, despite using a BufferedReader. Implementing streamUntilDelimiter directly on BufferedReader gave anywhere from a 2x to a 15x performance improvement (depending on the input). Here's a benchmark: https://gist.github.com/karlseguin/17cc66cc26f167c1242608bfae73fb06

I made std.io.GenericReader call an underlying streamUntilDelimiter if present: master...karlseguin:zig:buffered_reader_delimiter

But I couldn't figure out how to push this down to std.io.Reader, where I think it would do the most good (various std.io.Reader functions call streamUntilDelimiter).

So, unable to provide a fix, I thought I'd open an issue.

Expected Behavior

It would be nice if the the performance benefits of using a BufferedReader to remain in play even when hidden behind an io.GenericReader or .io.Reader.

Metadata

Metadata

Assignees

No one assigned

    Labels

    optimizationstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions