Skip to content

Faster seek and restart#6508

Merged
NinoFloris merged 5 commits intonpgsql:mainfrom
NinoFloris:faster-seek-and-restart
Mar 25, 2026
Merged

Faster seek and restart#6508
NinoFloris merged 5 commits intonpgsql:mainfrom
NinoFloris:faster-seek-and-restart

Conversation

@NinoFloris
Copy link
Copy Markdown
Member

Together with the change to restart behavior in #6490 significantly improves column rereading perf (~10%).

Copilot AI review requested due to automatic review settings March 17, 2026 08:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes column rereading by tightening/streamlining PgReader seek/restart mechanics and adjusting GetBytes to avoid extra work when repositioning within a field, improving reread performance (notably alongside #6490’s restart behavior changes).

Changes:

  • Update NpgsqlDataReader.GetBytes to reuse a cached PgReader local, use the new IsFieldPastOffset check, and clamp reads via CurrentRemaining after seeking.
  • Refactor PgReader seeking/restarting: rename offset-check helper, change Seek to void, and speed up restart rewinds via a lighter-weight rewind path.
  • Introduce a named sentinel constant for DB null field sizes and reuse cached FieldSize during restart.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Npgsql/NpgsqlDataReader.cs Adjusts GetBytes seeking/clamping to use cached reader state and updated PgReader APIs.
src/Npgsql/Internal/PgReader.cs Refactors seek/restart internals for faster restarts and clearer sentinel usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 17, 2026 08:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refines PgReader seeking/restarting behavior to reduce overhead in column rereads, complementing the restart behavior changes from #6490 to improve reread performance.

Changes:

  • Adjust NpgsqlDataReader.GetBytes to seek without relying on a return value and cap reads using PgReader.CurrentRemaining.
  • Update PgReader.Seek to return void and streamline offset handling; add a low-level RewindCore for restart fast-path.
  • Minor internal cleanup: introduce DbNullSentinel constant and rename IsFieldConsumedIsFieldPastOffset.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Npgsql/NpgsqlDataReader.cs Updates GetBytes to use the new PgReader.Seek shape and compute remaining bytes via CurrentRemaining.
src/Npgsql/Internal/PgReader.cs Makes Seek void, optimizes Restart rewind path, and clarifies internal sentinel/naming.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

void RewindCore(int count)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we should add Debug.Assert here, just like Rewind has for CurrentOffset < count, _buffer.ReadPosition < count and StreamActive?

@NinoFloris NinoFloris enabled auto-merge (squash) March 25, 2026 11:46
@NinoFloris NinoFloris merged commit b5a1001 into npgsql:main Mar 25, 2026
12 checks passed
@NinoFloris NinoFloris deleted the faster-seek-and-restart branch March 25, 2026 11:51
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