Optimize NpgsqlDataReader.NextResult async state machine - #6638
Merged
Conversation
roji
approved these changes
Aug 28, 2026
There was a problem hiding this comment.
Pull request overview
Refactors NpgsqlDataReader.NextResult internals to reduce async state machine footprint/allocations by extracting output-parameter handling into a dedicated method and switching the internal Consume helper to return ValueTask.
Changes:
- Extracted output-parameter
DataRowhandling intoReadMessageWithOutputParameters(...)to keep fewer locals onNextResult’s async state machine. - Changed internal
Consume(...)fromTasktoValueTaskto avoid carrying an extraConfiguredTaskAwaiteron the async state machine. - Preserved existing behavior for message processing, state restoration, and error-barrier consumption paths.
Show a summary per file
| File | Description |
|---|---|
src/Npgsql/NpgsqlDataReader.cs |
Refactors NextResult output-parameter path into a helper and changes Consume to ValueTask to reduce allocations. |
Review details
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
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.
By moving output parameter handling into a separate method and changing
Consume's return value toValueTask.While handling output parameters we have to keep track of
_isSequentialandBuffer.ReadPositionplus a bunch of other things, which is why they're kept on the async state machine. As forConsume, async state machine also keeps the resultingConfiguredTaskAwaitable.ConfiguredTaskAwaiter(and forValueTaskit'sConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter). As we already haveConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiteron the state machine from a local functionConsumeResultSet, we might as well just change it toValueTaskand removeConfiguredTaskAwaitable.ConfiguredTaskAwaiterfrom the state machine.All together, we save about 8-10% of allocations per each query (from 600 bytes to about 550).