system.memory

Proper way to get a mutable struct for Memory<byte> / Span<byte>?

社会主义新天地 提交于 2019-12-30 07:02:10
问题 For a network protocol implementation I want to make use of the new Memory and Span classes to achieve zero-copy of the buffer while accessing the data through a struct . I have the following contrived example: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Data { public int IntValue; public short ShortValue; public byte ByteValue; } static void Prepare() { var buffer = new byte[1024]; var dSpan = MemoryMarshal.Cast<byte, Data>(buffer); ref var d = ref dSpan[0]; d.ByteValue = 1

Span and two dimensional Arrays

删除回忆录丶 提交于 2019-12-13 14:24:04
问题 Is it possible to use the new System.Memory Span struct with two dimensional arrays of data? double[,] testMulti = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 9.5f, 10, 11 }, { 12, 13, 14.3f, 15 } }; double[] testArray = { 1, 2, 3, 4 }; string testString = "Hellow world"; testMulti.AsSpan(); // Compile error testArray.AsSpan(); testString.AsSpan(); Whilst testArray and testString have a AsSpan extension, no such extension exists for testMulti. Is the design of Span limited to working with single

What is the difference between Span<T> and Memory<T> in C# 7.2?

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:36:16
问题 C# 7.2 introduces two new types: Span<T> and Memory<T> that have better performance over earlier C# types like string[] . Question: What is the difference between Span<T> and Memory<T> ? Why would I use one over the other? 回答1: Span<T> is stack-only in nature while Memory<T> can exist on the heap. Span<T> is a new type we are adding to the platform to represent contiguous regions of arbitrary memory, with performance characteristics on par with T[]. Its APIs are similar to the array, but

What is the difference between Span<T> and Memory<T> in C# 7.2?

大兔子大兔子 提交于 2019-12-02 16:06:18
C# 7.2 introduces two new types: Span<T> and Memory<T> that have better performance over earlier C# types like string[] . Question: What is the difference between Span<T> and Memory<T> ? Why would I use one over the other? Hussein Salman Span<T> is stack-only in nature while Memory<T> can exist on the heap. Span<T> is a new type we are adding to the platform to represent contiguous regions of arbitrary memory, with performance characteristics on par with T[]. Its APIs are similar to the array, but unlike arrays, it can point to either managed or native memory, or to memory allocated on the

Proper way to get a mutable struct for Memory<byte> / Span<byte>?

こ雲淡風輕ζ 提交于 2019-11-30 22:19:38
For a network protocol implementation I want to make use of the new Memory and Span classes to achieve zero-copy of the buffer while accessing the data through a struct . I have the following contrived example: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Data { public int IntValue; public short ShortValue; public byte ByteValue; } static void Prepare() { var buffer = new byte[1024]; var dSpan = MemoryMarshal.Cast<byte, Data>(buffer); ref var d = ref dSpan[0]; d.ByteValue = 1; d.ShortValue = (2 << 8) + 3; d.IntValue = (4 << 24) + (5 << 16) + (6 << 8) + 7; } The result is that

Span<T> does not require local variable assignment. Is that a feature?

£可爱£侵袭症+ 提交于 2019-11-30 17:31:06
I notice that the following will compile and execute even though the local variables are not initialized. Is this a feature of Span? void Uninitialized() { Span<char> s1; var l1 = s1.Length; Span<char> s2; UninitializedOut(out s2); var l2 = s2.Length; } void UninitializedOut(out Span<char> s) {} This looks like an issue caused by reference assemblies, required because of the way that Span<T> has framework-specific internals. This means that in the reference assembly : there are no fields (edit: this isn't quite true - see footnote). A struct is considered assigned (for the purposes of

Span<T> does not require local variable assignment. Is that a feature?

谁说胖子不能爱 提交于 2019-11-30 16:41:31
问题 I notice that the following will compile and execute even though the local variables are not initialized. Is this a feature of Span? void Uninitialized() { Span<char> s1; var l1 = s1.Length; Span<char> s2; UninitializedOut(out s2); var l2 = s2.Length; } void UninitializedOut(out Span<char> s) {} 回答1: This looks like an issue caused by reference assemblies, required because of the way that Span<T> has framework-specific internals. This means that in the reference assembly : there are no fields