akka.net icon indicating copy to clipboard operation
akka.net copied to clipboard

Add support to `ByteString` for copying to/from `Memory` and `Span`

Open Arkatufus opened this issue 3 years ago • 6 comments

Changes

Add CopyTo and CopyFrom Memory and Span support to ByteString

Arkatufus avatar Jun 27 '22 14:06 Arkatufus

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)

to11mtm avatar Jul 09 '22 19:07 to11mtm

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)

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.

Aaronontheweb avatar Jul 11 '22 12:07 Aaronontheweb

I don't think we will get any improvement switching over to Memory<T>, even microsoft ReadOnlySequence<T> still uses ArraySegment<T> underneath.

Arkatufus avatar Jul 12 '22 13:07 Arkatufus

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)

Aaronontheweb avatar Jul 12 '22 13:07 Aaronontheweb

Hmmm... true, as long as we ditch dotnetty

Arkatufus avatar Jul 12 '22 16:07 Arkatufus

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 ReadOnlyMemory instead of Memory here,
    • We wouldn't need to copy anything
    • Users can then decide whether to do a copy of Memory to ReadOnlyMemory, or an Unsafe cast, but we are providing a more 'guided' API suface
    • Users could use non-array backing buffers without additional allocation

to11mtm avatar Jul 14 '22 20:07 to11mtm

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.

Aaronontheweb avatar Jan 03 '23 17:01 Aaronontheweb