akka.net
akka.net copied to clipboard
Add support to `ByteString` for copying to/from `Memory` and `Span`
Changes
Add CopyTo and CopyFrom Memory and Span support to ByteString
Should we consider going away from ArraySegment for internal storage and use Memory instead? On one hand it would be a bit of a yak shave, on the other hand it should greatly simplify the internal logic (since with Memory we can slice and not have to consider offset/length in other methods)
Should we consider going away from
ArraySegmentfor internal storage and useMemoryinstead? On one hand it would be a bit of a yak shave, on the other hand it should greatly simplify the internal logic (since withMemorywe can slice and not have to consider offset/length in other methods)
This is more what I was thinking too - moving away from ArraySegment and towards System.Memory primitives. I haven't reviewed this PR in any depth at all though.
I don't think we will get any improvement switching over to Memory<T>, even microsoft ReadOnlySequence<T> still uses ArraySegment<T> underneath.
I don't think we will get any improvement switching over to Memory, even microsoft ReadOnlySequence still uses ArraySegment underneath.
being able to work with new native APIs in .NET 6 without copying is a big improvement: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receiveasync?view=net-6.0#system-net-sockets-socket-receiveasync(system-memory((system-byte))-system-net-sockets-socketflags-system-threading-cancellationtoken)
Hmmm... true, as long as we ditch dotnetty
I don't think we will get any improvement switching over to Memory, even microsoft ReadOnlySequence still uses ArraySegment underneath.
being able to work with new native APIs in .NET 6 without copying is a big improvement: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receiveasync?view=net-6.0#system-net-sockets-socket-receiveasync(system-memory((system-byte))-system-net-sockets-socketflags-system-threading-cancellationtoken)
Long term I think it's probably worth the payoff to just move to ReadOnlyMemory in place of ArraySegment.
- https://github.com/akkadotnet/akka.net/pull/6026/files#diff-3392a6574391b386b21fe8dbac936e34d14c9f0ebbdd43602ab9a34b100eae38R94-R105 <-- Good example to consider. If we, in the bold case, -only- took
ReadOnlyMemoryinstead ofMemoryhere,- We wouldn't need to copy anything
- Users can then decide whether to do a copy of
MemorytoReadOnlyMemory, or an Unsafe cast, but we are providing a more 'guided' API suface - Users could use non-array backing buffers without additional allocation
This is small PR and doesn't do much to change the internals of ByteString - we should address the internals of this later, once we are in a better position to support System.Memory throughout the serialization and transport system.