Skip to content

Stream reads on the consumer's thread: no channel, no producer task - #237

Open
electricessence wants to merge 4 commits into
masterfrom
perf/direct-stream
Open

electricessence wants to merge 4 commits into
masterfrom
perf/direct-stream

Conversation

@electricessence

@electricessence electricessence commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Stream(stallTimeout, ct) and StreamBuffered(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. StreamBuffered with 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 for StreamBuffered(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 ReadAsync completes 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 what Stream was:

Consumer Rows Stream (new) old Stream StreamBuffered (1,024) ToPooledStream
nothing per row 1 1.1 2.1 2.1 2.1
nothing per row 100 11 62 28 9
nothing per row 1,000 105 400 180 57
await Task.Yield() per row 100 45 58 36 33
await Task.Yield() per row 1,000 295 373 210 190

What it supports: for a fast loop Stream is the fastest of the held-connection reads (4 to 6 times faster than before, and 1.7 times faster than StreamBuffered at 1,000 rows), and it allocates only the rows (213 KB per 1,000 rows against 318 KB before). With an async step per row StreamBuffered is about 30 % faster than Stream and ToPooledStream about 35 % faster, which matches "slow or async work per row: StreamBuffered". ToPooledStream is also faster than Stream for 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

electricessence and others added 4 commits November 24, 2026 13:15
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
electricessence marked this pull request as ready for review November 24, 2026 23:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant