mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Merge pull request #9 from DarthAffe/PerformanceImprovements
Changed array allocations for copying potentionally big data (like whole images) to be uninitialized allocation
This commit is contained in:
commit
da26ca248f
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user