You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Zero-copy binary reading from the reader's buffer #1712
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.
Reacted by Flavien Charlon, Artemov Ivan and Yoh Deadfall
Looking at this again as part of triage with @roji. Some more comments:
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.
Allow NpgsqlParameter to accept a Memory<byte>, which we then write (just as an alternative to byte[]).
Finally, we could optionally:
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...
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?
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.
Activity
roji commentedon Dec 24, 2017
Some notes:
Brar commentedon Apr 18, 2021
Looking at this again as part of triage with @roji. Some more comments:
Memory<byte>, which we then write (just as an alternative tobyte[]).Finally, we could optionally:
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...
NinoFloris commentedon Aug 15, 2023
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.
roji commentedon Aug 15, 2023
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?
NinoFloris commentedon Aug 15, 2023
Right, if we'd expose ROSeq as an alternative to Stream we would be able to satisfy that.
roji commentedon Aug 15, 2023
Yep
[-]Support reading/writing Span to/from bytea[/-][+]Zero-copy binary reading from the reader's buffer[/+]NinoFloris commentedon Nov 20, 2025
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 structon 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.
NinoFloris commentedon Mar 14, 2026
Considering the above and the lack of interest I'm going to close this until it comes up again.