Repository navigation
Read lines until ReadLineAsync returns null in the File polyfills - #709
Merged
Merged
Conversation
This was referenced Oct 12, 2026
This was referenced Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
File.ReadAllLinesAsync(.NET Framework / netstandard2.0) andFile.ReadLinesAsync(below net7) polyfills loop onreader.EndOfStream. When the reader's buffer is empty at that point,EndOfStreamrefills it with a synchronousStream.Read, which on aFileStreamopened for asynchronous I/O blocks the calling thread on the overlapped read.Change
Both loops now read until
ReadLineAsyncreturnsnull:The lines returned are the same. The token is still checked once per line, and, as before, never once the end of the file has been reached, so an already cancelled token still throws for a file with content and still returns nothing for an empty file.
Measurements
net48, x64, Release. A 12 MB file of 200,000 lines, read through the old and the new loop with the
FileStreamreplaced by a subclass that counts calls.Stream.Readcalls per fileStream.ReadAsynccalls per fileEndOfStreamonly has to refill when a line happens to end exactly where the buffer does, so it accounts for about 1% of the reads rather than all of them; the rest already went throughReadLineAsync. The elapsed time is accordingly unchanged within the noise of the machine (best of 15 runs, three rounds):ReadAllLinesAsyncReadLinesAsyncSo this removes the remaining synchronous reads from the two asynchronous methods; it is not a throughput change.
Tests
In a unique temp directory that is deleted afterwards:
ReadAllLinesAsyncandReadLinesAsyncagainstFile.ReadAllLinesfor a set of file contents built around the reader's buffer sizes: empty, only terminators, no final terminator, leading and trailing blank lines, a first line ending 2 bytes before to 2 bytes after the 1024, 2048, 4096 and 8192 byte marks with each of\n,\r\n,\rand no terminator (so terminators are split by a buffer boundary and lines end exactly on one), 5,000 short lines, lines exactly one buffer long, and a 100,000 character lineReadAllLinesAsyncwith UTF-8, UTF-8 with BOM, UTF-16 LE/BE and UTF-32, including a byte order mark overriding the requested encodingReadLinesAsynccancelled while enumerating throws, having returned only a prefix of the lines, and leaves the file closed; breaking out of the enumeration early leaves the file closed