From d11a624a340d34c9b836a268562c5dac53a52563 Mon Sep 17 00:00:00 2001 From: Christian Luksch Date: Mon, 13 May 2024 18:14:13 +0200 Subject: [PATCH] [IO] using spans in all Read/WriteArray functions with net6.0 --- src/Aardvark.Base.IO/StreamCodeReader.cs | 54 ++++++++++++++++++++---- src/Aardvark.Base.IO/StreamCodeWriter.cs | 25 ++++++----- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/Aardvark.Base.IO/StreamCodeReader.cs b/src/Aardvark.Base.IO/StreamCodeReader.cs index 7a37a6de..79e62821 100644 --- a/src/Aardvark.Base.IO/StreamCodeReader.cs +++ b/src/Aardvark.Base.IO/StreamCodeReader.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -178,6 +179,25 @@ public long ReadArray(T[,] array, long count) where T : struct { if (count < 1) return 0; + +#if NET6_0_OR_GREATER + var sizeOfT = Marshal.SizeOf(typeof(T)); + var span = MemoryMarshal.CreateSpan(ref MemoryMarshal.GetArrayDataReference(array), (int)count * sizeOfT); + + var bytesToRead = span.Length; + var offset = 0; + + do + { + int finished = base.Read(span); + if (finished == 0) break; + offset += finished; bytesToRead -= finished; + span = span.Slice(offset, bytesToRead); + } + while (bytesToRead > 0); + + return offset / sizeOfT; +#else unsafe { var sizeOfT = Marshal.SizeOf(typeof(T)); @@ -185,11 +205,9 @@ public long ReadArray(T[,] array, long count) hack.structs = array; var bytesToRead = (int)(sizeOfT * count); - #if __MonoCS__ - var skip = 0; - #else + var skip = 2 * 2 * sizeof(int); - #endif + IntPtr byteLen = (IntPtr)(array.Length * sizeOfT + skip); var offset = skip; @@ -212,12 +230,31 @@ public long ReadArray(T[,] array, long count) } return (long)((offset - skip) / sizeOfT); } +#endif } public long ReadArray(T[, ,] array, long count) where T : struct { if (count < 1) return 0; +#if NET6_0_OR_GREATER + var sizeOfT = Marshal.SizeOf(typeof(T)); + var span = MemoryMarshal.CreateSpan(ref MemoryMarshal.GetArrayDataReference(array), (int)count * sizeOfT); + + var bytesToRead = span.Length; + var offset = 0; + + do + { + int finished = base.Read(span); + if (finished == 0) break; + offset += finished; bytesToRead -= finished; + span = span.Slice(offset, bytesToRead); + } + while (bytesToRead > 0); + + return offset / sizeOfT; +#else unsafe { var sizeOfT = Marshal.SizeOf(typeof(T)); @@ -225,11 +262,9 @@ public long ReadArray(T[, ,] array, long count) hack.structs = array; var bytesToRead = (int)(sizeOfT * count); - #if __MonoCS__ - var skip = 0; - #else + var skip = 3 * 2 * sizeof(int); - #endif + IntPtr byteLen = (IntPtr)(array.Length * sizeOfT + skip); var offset = skip; @@ -252,6 +287,7 @@ public long ReadArray(T[, ,] array, long count) } return (long)((offset - skip) / sizeOfT); } +#endif } public int ReadList(List buffer, int index, int count) @@ -264,7 +300,7 @@ public int ReadList(List buffer, int index, int count) return (int)ReadArray(arrayValue, (long)index, (long)count); } - #endregion +#endregion #region Close diff --git a/src/Aardvark.Base.IO/StreamCodeWriter.cs b/src/Aardvark.Base.IO/StreamCodeWriter.cs index 9cbfe925..f4d07215 100644 --- a/src/Aardvark.Base.IO/StreamCodeWriter.cs +++ b/src/Aardvark.Base.IO/StreamCodeWriter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -165,6 +166,11 @@ public void WriteArray(T[,] array, long count) where T : struct { if (count < 1) return; +#if NET6_0_OR_GREATER + var sizeOfT = Marshal.SizeOf(typeof(T)); + var byteSpan = MemoryMarshal.CreateSpan(ref MemoryMarshal.GetArrayDataReference(array), (int)count * sizeOfT); + base.Write(byteSpan); +#else unsafe { var sizeOfT = Marshal.SizeOf(typeof(T)); @@ -174,11 +180,7 @@ public void WriteArray(T[,] array, long count) fixed (byte* pBytes = hack.bytes) { - #if __MonoCS__ - long offset = 0; - #else long offset = 2 * 2 * sizeof(int); - #endif int itemsPerBlock = c_bufferSize / sizeOfT; do @@ -198,12 +200,18 @@ public void WriteArray(T[,] array, long count) while (count > 0); } } +#endif } public void WriteArray(T[, ,] array, long count) where T : struct { if (count < 1) return; +#if NET6_0_OR_GREATER + var sizeOfT = Marshal.SizeOf(typeof(T)); + var byteSpan = MemoryMarshal.CreateSpan(ref MemoryMarshal.GetArrayDataReference(array), (int)count * sizeOfT); + base.Write(byteSpan); +#else unsafe { var sizeOfT = Marshal.SizeOf(typeof(T)); @@ -213,12 +221,8 @@ public void WriteArray(T[, ,] array, long count) fixed (byte* pBytes = hack.bytes) { - #if __MonoCS__ - long offset = 0; - #else long offset = 3 * 2 * sizeof(int); - #endif - + int itemsPerBlock = c_bufferSize / sizeOfT; do { @@ -237,6 +241,7 @@ public void WriteArray(T[, ,] array, long count) while (count > 0); } } +#endif } public void WriteList(List buffer, int index, int count) @@ -247,7 +252,7 @@ public void WriteList(List buffer, int index, int count) WriteArray(arrayValue, (long)index, (long)count); } - #endregion +#endregion #region Close