Natively support Memory<byte> #2311
Open
+179
−70
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.
There are many PRs open aiming to extend the API to allow low allocation produce but they all have some caveats:
Span
but given the constraints of that type, it means creating a whole new set of API.IBufferWriter
instead, which unlikebyte[]
orSpan
, supports non-contiguous memory, kind of likeStream
but it enables a lof of ways to reduce allocations. I don't thinkIBufferWriter
is ideal here because in the end, a continuous memory buffer is expected for both the key and value. Also, the PR is introducing several breaking changes.ArraySegment
which does not break the API and fixes the original problem. Though it would prevent the use ofMemory<byte>
which is the preferred type when dealing with contiguous memory whenSpan<byte>
can't be used. This type is for example used in Stream.WriteAsync, Socket.SendAsync, and also in MemoryPool.Rent.Instead of adding a third type of serializer (after
ISerializer
andIAsyncSerializer
), this PR makes a special case forMemory<byte>
(andReadOnlyMemory<byte>
) to by pass the serializers and directly useMessage.Value
orMessage.Key
.