-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal] API for interpretting Span<byte> as struct without copying #26587
Comments
Related proposal for other direction #24656 dotnet/corefx#30614 has multiple examples of how these methods can be used in real-world code. |
A bit like |
Right. |
Could this use the |
The implementation will enforce no-GC references at runtime just like |
This API and scenarios make sense but we shouldn't use static class MemoryMarshal
{
public static ref readonly T AsRef<T>(ReadOnlySpan<byte> span) where T : struct;
public static ref T AsRef(Span<byte> span) where T : struct;
} |
@terrajobst |
@jkotas apparently I fail at reading today, thx |
Contributes to https://github.com/dotnet/corefx/issues/30613 Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Contributes to https://github.com/dotnet/corefx/issues/30613 Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Contributes to https://github.com/dotnet/corefx/issues/30613 Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Contributes to https://github.com/dotnet/corefx/issues/30613 Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
- Add tests - Take advantage of the new API in CoreFX Fixes #30613
- Add tests - Take advantage of the new API in CoreFX Fixes #30613
* Expose MemoryMarshal.AsRef - Add tests - Take advantage of the new API in CoreFX Fixes #30613 * Auto-generated System.Memory ref
Is it possible to use AsRef in .NET Standard 2.1? I was only able to use it targeting netcoreapp3.0 |
@virzak best to ask this in the dotnet/standard repo |
MemoryMarshal contains several APIs that allow reinterpreting
Span<byte>
, but none of them are a good fit for interpreting Span as struct without copying:MemoryMarshal.Read
allows interpretingSpan<byte>
as arbitrary struct. It creates a local copy that is fine and beneficial for small structs, but becomes expensive for large structs.MemoryMarshal.Cast
allows interpretingSpan<byte>
as sequence of arbitrary structs. It does not create a local copy, but comes with extra overhead required to compute the length of the resulting span.MemoryMarshal.GetReference
+Unsafe.As
does not create a local copy, but also skips any bounds checks that is not good for security defense in depth.Proposal
Add methods that allow interpreting
Span<byte>
as arbitrary large struct without copying to MemoryMarshal.Implementation of these methods is going to be faster functional equivalent of
ref MemoryMarshal.Cast<byte, T>(span)[0]
.These APIs come with the same set of portability caveats as
Memory.Cast
. They can be only used when the memory block is sufficiently aligned by other means; or when the target platform does not require strong memory alignment.The text was updated successfully, but these errors were encountered: