Changed array allocations for copying potentionally big data (like whole images) to be uninitialized allocation

This commit is contained in:
Darth Affe 2024-09-05 22:42:03 +02:00
parent f091aadd92
commit 94f3b8cd16
5 changed files with 10 additions and 9 deletions

View File

@ -124,7 +124,7 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
if (stride < width) throw new ArgumentException("Stride can't be smaller than width.");
if (buffer.Length < (height * stride)) throw new ArgumentException("Not enough data in the buffer.");
byte[] data = new byte[buffer.Length];
byte[] data = GC.AllocateUninitializedArray<byte>(buffer.Length);
buffer.CopyTo(data);
return new Image<T>(data, 0, 0, width, height, stride);
}
@ -152,7 +152,7 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
else
{
byte[] data = ToRawArray();
byte[] target = new byte[Width * Height * targetBpp];
byte[] target = GC.AllocateUninitializedArray<byte>(Width * Height * targetBpp);
MemoryMarshal.Cast<byte, T>(data.AsSpan()).Convert(MemoryMarshal.Cast<byte, TColor>(target));
return new Image<TColor>(target, 0, 0, Width, Height, Width * targetBpp);
}
@ -180,7 +180,7 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
/// <inheritdoc />
public byte[] ToRawArray()
{
byte[] array = new byte[SizeInBytes];
byte[] array = GC.AllocateUninitializedArray<byte>(SizeInBytes);
CopyTo(array);
return array;
}
@ -188,7 +188,7 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
/// <inheritdoc />
public T[] ToArray()
{
T[] colors = new T[Width * Height];
T[] colors = GC.AllocateUninitializedArray<T>(Width * Height);
CopyTo(colors);
return colors;
}

View File

@ -64,7 +64,7 @@ public readonly ref struct ImageColumn<T>
public T[] ToArray()
{
T[] array = new T[Length];
T[] array = GC.AllocateUninitializedArray<T>(Length);
CopyTo(array);
return array;
}

View File

@ -62,10 +62,11 @@ public readonly ref struct ImageRow<T>
public T[] ToArray()
{
T[] array = new T[Length];
T[] array = GC.AllocateUninitializedArray<T>(Length);
CopyTo(array);
return array;
}
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageRowEnumerator GetEnumerator() => new(this);

View File

@ -128,7 +128,7 @@ public readonly ref struct RefImage<T>
/// <returns>The new array containing the data of this <see cref="RefImage{T}"/>.</returns>
public T[] ToArray()
{
T[] array = new T[Width * Height];
T[] array = GC.AllocateUninitializedArray<T>(Width * Height);
CopyTo(array);
return array;
}

View File

@ -38,7 +38,7 @@ public static unsafe partial class PixelHelper
{
if (colors == null) throw new ArgumentNullException(nameof(colors));
TTarget[] buffer = new TTarget[colors.Length];
TTarget[] buffer = GC.AllocateUninitializedArray<TTarget>(colors.Length);
Convert(colors, buffer.AsSpan());
return buffer;
}
@ -49,7 +49,7 @@ public static unsafe partial class PixelHelper
{
if (colors == null) throw new ArgumentNullException(nameof(colors));
TTarget[] buffer = new TTarget[colors.Length];
TTarget[] buffer = GC.AllocateUninitializedArray<TTarget>(colors.Length);
Convert(colors, buffer.AsSpan());
return buffer;
}