AoC PR: lib: performance and API improvements for stdin and reader #2343
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are three changes in this patch:
Replaced
while (mutate.flat_map_sequence u8 x->x).count < n
byn_read < n
. This was by far the biggest performance issue when reading bytes since it required time in O(n²).Replaced
drop
inreader.discard
byslice
which is O(1) and does not cause deep recursion.Add a version of io.buffered.reader.read with an argument max_n that gives the desired number of bytes to be read.
io.stdin.read_provider.read no longer restrics the number of bytes to just one, but tries to read count bytes. This change currently breaks the C backend since the C intrinsic apparently attempts to read all bytes until count or end-of-file is reached. The C backend should instead check the number of bytes that are available to read without blocking and read only those unless there are none available. Only then it should block until data is available and then read all available data up to the given limit of bytes.
I will create an issue for this C backend problem to be fixed later.
Added two convenience features
io.buffered.read_fully
andio.buffered.read_lines
to read all bytes into a large array or read all lines of a file into an array of Strings, respectively.