Skip to content

Commit

Permalink
[IO] using spans in all Read/WriteArray functions with net6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luithefirst committed May 13, 2024
1 parent 4af328d commit d11a624
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
54 changes: 45 additions & 9 deletions src/Aardvark.Base.IO/StreamCodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -178,18 +179,35 @@ public long ReadArray<T>(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));
var hack = new ByteArrayUnion();
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;

Expand All @@ -212,24 +230,41 @@ public long ReadArray<T>(T[,] array, long count)
}
return (long)((offset - skip) / sizeOfT);
}
#endif
}

public long ReadArray<T>(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));
var hack = new ByteArrayUnion();
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;

Expand All @@ -252,6 +287,7 @@ public long ReadArray<T>(T[, ,] array, long count)
}
return (long)((offset - skip) / sizeOfT);
}
#endif
}

public int ReadList<T>(List<T> buffer, int index, int count)
Expand All @@ -264,7 +300,7 @@ public int ReadList<T>(List<T> buffer, int index, int count)
return (int)ReadArray(arrayValue, (long)index, (long)count);
}

#endregion
#endregion

#region Close

Expand Down
25 changes: 15 additions & 10 deletions src/Aardvark.Base.IO/StreamCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -165,6 +166,11 @@ public void WriteArray<T>(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));
Expand All @@ -174,11 +180,7 @@ public void WriteArray<T>(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
Expand All @@ -198,12 +200,18 @@ public void WriteArray<T>(T[,] array, long count)
while (count > 0);
}
}
#endif
}

public void WriteArray<T>(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));
Expand All @@ -213,12 +221,8 @@ public void WriteArray<T>(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
{
Expand All @@ -237,6 +241,7 @@ public void WriteArray<T>(T[, ,] array, long count)
while (count > 0);
}
}
#endif
}

public void WriteList<T>(List<T> buffer, int index, int count)
Expand All @@ -247,7 +252,7 @@ public void WriteList<T>(List<T> buffer, int index, int count)
WriteArray(arrayValue, (long)index, (long)count);
}

#endregion
#endregion

#region Close

Expand Down

0 comments on commit d11a624

Please sign in to comment.