Repository navigation
Stream reads on the consumer's thread: no channel, no producer task - #237
Open
electricessence wants to merge 4 commits into
Open
electricessence wants to merge 4 commits into
electricessence wants to merge 4 commits into
Conversation
Stream, and StreamBuffered with a buffer of 0, read each row when it is pulled, on the consumer's own thread, and read nothing ahead. The read is the query's usual one, run as a coroutine of the consumer's loop: a pull wakes it from the pull's own call, it hands one row back and parks, so a row costs no thread hop, no channel and no task. A timer, made when the first row is handed over and only for a finite stall timeout, watches the time of the last hand-over. The consumer's pull and the watchdog's expiry move one state word, so whichever comes first wins and the other does nothing. An expiry cancels the command and wakes the read with the timeout, so it unwinds and closes the reader and the connection; the next pull throws the TimeoutException. A dispose, a break or a throw in the loop cancels the command and unwinds the read the same way. StreamBuffered with a buffer of 1 or more is unchanged. The Channel and Dataflow terminals that hold the connection now ask for StreamBuffered with a buffer of one row, so their documented one-row-ahead behavior and their stall signal stay as they were. The Stream docs say what it now is: nothing read ahead, no producer task, a synchronous read blocks the consumer's thread, a cancel cannot interrupt a read the provider is blocked in, an expiry is found within about a quarter of the timeout. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Stream, StreamBuffered with a channel of one row, StreamBuffered with its default buffer and ToPooledStream, each with a consumer that does nothing and one that awaits Task.Yield() per row. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…er expiry, measured allocation A test reader whose ReadAsync completes later (a yield, or a delay that ends with the token's cancel) covers the pulls that really wait: every row in order, a slow pull is never an expiry, a cancel while a pull waits ends it and cleans up once. A cancel wins over an expiry at the next pull. The allocation test is set from the measured 32.0 bytes per row, the row itself. A tautological assertion is dropped. The docs say which thread reads when AsyncReads() is asked for, that the connection is held until the pull past the last row, how late an expiry can be found, and that a cancel while a row is held is seen at the next pull. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…generic The test reader's ReadAsync throws for a token that is already canceled, as the base reader does, so the existing cancel tests of the reader loops pass again. The stream benchmarks take the V1_ prefix the naming rule allows. nameof of the unbound generic type; the test passes its token. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
electricessence
marked this pull request as ready for review
November 24, 2026 23:25
This branch has not been deployed
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.
Stream(stallTimeout, ct)andStreamBuffered(stallTimeout, 0, ct)now read each row when it is pulled, on the consumer's own thread, and read nothing ahead: no channel and no producer task.StreamBufferedwith a buffer of 1 or more is unchanged. Draft until the owner has read the numbers below.How it works: the query's usual read runs as a coroutine of the consumer's loop. A pull wakes it from the pull's own call; it hands one row back and parks. A pull that finds the next row ready returns a completed task, so a row costs no thread hop. A real asynchronous read (
AsyncReads()) completes the pull from the thread that finished the read.Stall protection: a timer, created at the first row and only for a finite timeout, watches the time of the last hand-over. The consumer's pull and the watchdog's expiry move one state word, so whichever comes first wins and the other does nothing. An expiry cancels the command and unwinds the read, which closes the reader and the connection; the next pull throws the
TimeoutException, never a silent end. A cancel wins over an expiry. A break, a throw in the loop, or a dispose unwinds the read the same way. The timer is stopped on every way out.What changes for a caller: no row is read ahead of the loop (before, one or two were); a synchronous row read blocks the loop's thread unless the query asks for
AsyncReads(); a cancel while the loop holds a row is seen at the next pull (the Channel and Dataflow terminals that hold the connection now ask forStreamBuffered(stall, 1, ct), so their one-row-ahead behavior and stall signal are unchanged). The docs say each of these.Verification: full gate on the head (Release and Debug tests on net11.0 and net10.0, 14,495 pass; benchmarks build; 214 sample checks; format and analyzers 0 findings; DocFX; VS MSBuild 0). The stream tests were looped 25 times with no failure. Tests cover: nothing read ahead, reads on the consumer's thread, lazy and taken once, an expiry on the next pull, the timer's lifetime, dispose after an expiry and twice, a resume racing an expiry, a cancel over an expiry, pulls that really wait (a reader whose
ReadAsynccompletes later) including a cancel during one, and 32.0 bytes per row (the row itself, nothing from the stream). A Haiku swarm with a Sonnet arbiter reviewed it: no blocking finding; its should-fix items (tests and docs) are in.Benchmark (synthetic provider, width 5, net11.0, in process, three rounds on idle cores; the rounds agree within about 5 %). Mean per operation in microseconds; "old Stream" is
StreamBuffered(stall, 1, ct), which is whatStreamwas:await Task.Yield()per rowawait Task.Yield()per rowWhat it supports: for a fast loop
Streamis the fastest of the held-connection reads (4 to 6 times faster than before, and 1.7 times faster thanStreamBufferedat 1,000 rows), and it allocates only the rows (213 KB per 1,000 rows against 318 KB before). With an async step per rowStreamBufferedis about 30 % faster thanStreamandToPooledStreamabout 35 % faster, which matches "slow or async work per row:StreamBuffered".ToPooledStreamis also faster thanStreamfor a fast loop over 100 or more rows; it buffers the whole result, which is the trade the guidance line names. Measured on the synthetic provider, so absolute numbers will differ against a real server; the order of the models is the finding.Not in this PR: release notes and what's-new text. The stream-model cases are added to the synthetic benchmarks in their own commit.
🤖 Generated with Claude Code