Skip to content

Zero-copy binary reading from the reader's buffer #1712

Closed as not planned
Closed as not planned

Description

@roji

With the new Span introduced in C# 7.2, we should support it for PostgreSQL bytea.

Activity

  1. added this to the milestone on Nov 15, 2017
  2. self-assigned this
    on Nov 15, 2017
  3. roji commented on Dec 24, 2017

    @roji
    MemberAuthor

    Some notes:

    • Wait for 2.1, because right now there are no APIs for reading/writing Spans (e.g. in Stream)
    • Possibly add an optimization for bytea for reading a ReadOnlySpan which points into the Npgsql buffer, in cases where the entire value fits in the buffer. That's zero-copy bytea reading, pretty cool.
    • Unrelated to bytea - remove the tempCharBuffer used for skipping, allocate on the stack instead.
  4. modified the milestones: 4.0, on May 12, 2018
  5. modified the milestones: 4.1, Backlog on Aug 26, 2019
  6. Brar commented on Apr 18, 2021

    @Brar
    Member

    Looking at this again as part of triage with @roji. Some more comments:

    1. Add an NpgsqlDataReader.GetBytes overload just like the existing one, but which accepts a Span instead of byte[]. Think about async though - maybe GetBytesAsync which accepts a Memory. These should be ADO.NET APIs.
    2. Allow NpgsqlParameter to accept a Memory<byte>, which we then write (just as an alternative to byte[]).

    Finally, we could optionally:

    1. Add an API which returns a ReadOnlySpan<byte> directly into the internal buffer. The requirements are:
      a. SequentialAccess isn't used (so that we know the entire buffer is in memory)
      b. When the next row is read, the returned ReadOnlySpan becomes invalidated. It's up to the user to code correctly here...
  7. NinoFloris commented on Aug 15, 2023

    @NinoFloris
    Member

    We have streams filling this need IMO.

    Additionally if we want a more direct translation we'll have to wait until ref struct types can be used as generic arguments.

    @roji let me know what you want to do with this issue.

  8. roji commented on Aug 15, 2023

    @roji
    MemberAuthor

    We have streams filling this need IMO.

    There's still the more extreme version of providing access to Npgsql's internal buffer (zero copy), with all the lifecycle trouble that would entail. Otherwise I agree that this would basically be sugar over the user getting a stream and calling the methods there; and given that NpgsqlDataReader already has a very loaded public API surface, I'd avoid adding non-essential sugar there.

    Maybe keep this open to have the zero-copy variant somewhere on our radar?

  9. NinoFloris commented on Aug 15, 2023

    @NinoFloris
    Member

    Right, if we'd expose ROSeq as an alternative to Stream we would be able to satisfy that.

  10. roji commented on Aug 15, 2023

    @roji
    MemberAuthor

    Yep

  11. changed the title [-]Support reading/writing Span to/from bytea[/-] [+]Zero-copy binary reading from the reader's buffer[/+] on Aug 15, 2023
  12. added theissue type on Jun 14, 2025
  13. NinoFloris commented on Nov 20, 2025

    @NinoFloris
    Member

    So to summarize...

    As we expose the stream for any column, users can read spans and memories today already.

    Exposing a zero-copy interface would actually require us to support something like a PipeReader (as sometimes we won't have the entire column value ready) which requires us to take a dependency on System.IO.Pipelines.

    The other suggestions would only work if the result set isn't being read sequentially, while our current suggestion for high perf consumers is to pass CommandBehavior.SequentialAccess.

    Finally to generically support ref structs we're missing allows ref struct on GetFieldValue, which is not a backwards compatible change for the BCL to make. (For specific types we could always expose whatever we want through new methods on our reader type).

    There may be a place for these but they're not drop in improvements.

  14. NinoFloris commented on Mar 14, 2026

    @NinoFloris
    Member

    Considering the above and the lack of interest I'm going to close this until it comes up again.

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

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions