Skip to content

Optimize NpgsqlDataReader.NextResult async state machine - #6638

Merged
vonzshik merged 1 commit into
mainfrom
optimize-next-result-async-state-machine
Aug 28, 2026
Merged

Optimize NpgsqlDataReader.NextResult async state machine#6638
vonzshik merged 1 commit into
mainfrom
optimize-next-result-async-state-machine

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

By moving output parameter handling into a separate method and changing Consume's return value to ValueTask.

While handling output parameters we have to keep track of _isSequential and Buffer.ReadPosition plus a bunch of other things, which is why they're kept on the async state machine. As for Consume, async state machine also keeps the resulting ConfiguredTaskAwaitable.ConfiguredTaskAwaiter (and for ValueTask it's ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter). As we already have ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter on the state machine from a local function ConsumeResultSet, we might as well just change it to ValueTask and remove ConfiguredTaskAwaitable.ConfiguredTaskAwaiter from the state machine.

All together, we save about 8-10% of allocations per each query (from 600 bytes to about 550).

@vonzshik vonzshik added this to the 11.0.0 milestone Aug 25, 2026
@vonzshik vonzshik self-assigned this Aug 25, 2026
@vonzshik
vonzshik requested a review from roji as a code owner August 25, 2026 12:52
@roji
roji requested a lite review from Copilot August 28, 2026 09:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DataRow handling into ReadMessageWithOutputParameters(...) to keep fewer locals on NextResult’s async state machine.
  • Changed internal Consume(...) from Task to ValueTask to avoid carrying an extra ConfiguredTaskAwaiter on 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

@vonzshik
vonzshik merged commit af6676e into main Aug 28, 2026
18 checks passed
@vonzshik
vonzshik deleted the optimize-next-result-async-state-machine branch August 28, 2026 10:15
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.

3 participants