Skip to content

Commit a4ad7b0

Browse files
committed
Avoid length-check in pinning spans
Only where it seems profitable. E.g. not when a UnmanagedMemoryStream is constructed of that pointer.
1 parent 9756ae9 commit a4ad7b0

File tree

8 files changed

+19
-20
lines changed

8 files changed

+19
-20
lines changed

src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public static float Compress(float channel)
167167
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168168
private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table)
169169
{
170-
fixed (float* tablePointer = &table[0])
170+
fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table))
171171
{
172172
var scale = Vector256.Create((float)Scale);
173173
Vector256<float> zero = Vector256<float>.Zero;
@@ -199,7 +199,7 @@ private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table)
199199
[MethodImpl(MethodImplOptions.AggressiveInlining)]
200200
private static unsafe void CompandScalar(Span<Vector4> vectors, float[] table)
201201
{
202-
fixed (float* tablePointer = &table[0])
202+
fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table))
203203
{
204204
Vector4 zero = Vector4.Zero;
205205
var scale = new Vector4(Scale);

src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ internal static unsafe void ByteToNormalizedFloat(
644644
ReadOnlySpan<byte> source,
645645
Span<float> dest)
646646
{
647-
fixed (byte* sourceBase = source)
647+
fixed (byte* sourceBase = &MemoryMarshal.GetReference(source))
648648
{
649649
if (Avx2.IsSupported)
650650
{

src/ImageSharp/Compression/Zlib/Adler32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ private static unsafe uint CalculateScalar(uint adler, ReadOnlySpan<byte> buffer
387387
uint s1 = adler & 0xFFFF;
388388
uint s2 = (adler >> 16) & 0xFFFF;
389389

390-
fixed (byte* bufferPtr = buffer)
390+
fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
391391
{
392392
byte* localBufferPtr = bufferPtr;
393393
uint length = (uint)buffer.Length;

src/ImageSharp/Compression/Zlib/Crc32.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static unsafe uint CalculateSse(uint crc, ReadOnlySpan<byte> buffer)
8181
int chunksize = buffer.Length & ~ChunksizeMask;
8282
int length = chunksize;
8383

84-
fixed (byte* bufferPtr = buffer)
84+
fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
8585
{
8686
fixed (ulong* k05PolyPtr = K05Poly)
8787
{
@@ -201,7 +201,7 @@ private static unsafe uint CalculateSse(uint crc, ReadOnlySpan<byte> buffer)
201201
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
202202
private static unsafe uint CalculateArm(uint crc, ReadOnlySpan<byte> buffer)
203203
{
204-
fixed (byte* bufferPtr = buffer)
204+
fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
205205
{
206206
byte* localBufferPtr = bufferPtr;
207207
int len = buffer.Length;
@@ -248,7 +248,7 @@ private static unsafe uint CalculateArm(uint crc, ReadOnlySpan<byte> buffer)
248248
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
249249
private static unsafe uint CalculateArm64(uint crc, ReadOnlySpan<byte> buffer)
250250
{
251-
fixed (byte* bufferPtr = buffer)
251+
fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
252252
{
253253
byte* localBufferPtr = bufferPtr;
254254
int len = buffer.Length;

src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public static void PredictorInverseTransform(
569569
Span<uint> pixelData,
570570
Span<uint> outputSpan)
571571
{
572-
fixed (uint* inputFixed = pixelData)
572+
fixed (uint* inputFixed = &MemoryMarshal.GetReference(pixelData))
573573
{
574574
fixed (uint* outputFixed = outputSpan)
575575
{
@@ -1474,8 +1474,7 @@ private static uint Select(uint a, uint b, uint c, Span<short> scratch)
14741474
{
14751475
if (Sse2.IsSupported)
14761476
{
1477-
Span<short> output = scratch;
1478-
fixed (short* p = output)
1477+
fixed (short* ptr = &MemoryMarshal.GetReference(scratch))
14791478
{
14801479
Vector128<byte> a0 = Sse2.ConvertScalarToVector128UInt32(a).AsByte();
14811480
Vector128<byte> b0 = Sse2.ConvertScalarToVector128UInt32(b).AsByte();
@@ -1489,8 +1488,8 @@ private static uint Select(uint a, uint b, uint c, Span<short> scratch)
14891488
Vector128<byte> pa = Sse2.UnpackLow(ac, Vector128<byte>.Zero); // |a - c|
14901489
Vector128<byte> pb = Sse2.UnpackLow(bc, Vector128<byte>.Zero); // |b - c|
14911490
Vector128<ushort> diff = Sse2.Subtract(pb.AsUInt16(), pa.AsUInt16());
1492-
Sse2.Store((ushort*)p, diff);
1493-
int paMinusPb = output[3] + output[2] + output[1] + output[0];
1491+
Sse2.Store((ushort*)ptr, diff);
1492+
int paMinusPb = ptr[3] + ptr[2] + ptr[1] + ptr[0];
14941493
return (paMinusPb <= 0) ? a : b;
14951494
}
14961495
}

src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ private static void GetResidual(
353353
else
354354
{
355355
#pragma warning disable SA1503 // Braces should not be omitted
356-
fixed (uint* currentRow = currentRowSpan)
357-
fixed (uint* upperRow = upperRowSpan)
356+
fixed (uint* currentRow = &MemoryMarshal.GetReference(currentRowSpan))
357+
fixed (uint* upperRow = &MemoryMarshal.GetReference(upperRowSpan))
358358
{
359359
for (int x = xStart; x < xEnd; x++)
360360
{
@@ -664,9 +664,9 @@ private static void PredictBatch(
664664
Span<short> scratch)
665665
{
666666
#pragma warning disable SA1503 // Braces should not be omitted
667-
fixed (uint* current = currentSpan)
668-
fixed (uint* upper = upperSpan)
669-
fixed (uint* outputFixed = outputSpan)
667+
fixed (uint* current = &MemoryMarshal.GetReference(currentSpan))
668+
fixed (uint* upper = &MemoryMarshal.GetReference(upperSpan))
669+
fixed (uint* outputFixed = &MemoryMarshal.GetReference(outputSpan))
670670
{
671671
uint* output = outputFixed;
672672
if (xStart == 0)

src/ImageSharp/Formats/Webp/WebpCommonUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static unsafe bool CheckNonOpaque(Span<Bgra32> row)
2525
ReadOnlySpan<byte> rowBytes = MemoryMarshal.AsBytes(row);
2626
int i = 0;
2727
int length = (row.Length * 4) - 3;
28-
fixed (byte* src = rowBytes)
28+
fixed (byte* src = &MemoryMarshal.GetReference(rowBytes))
2929
{
3030
var alphaMaskVector256 = Vector256.Create(0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255);
3131
Vector256<byte> all0x80Vector256 = Vector256.Create((byte)0x80).AsByte();
@@ -81,7 +81,7 @@ public static unsafe bool CheckNonOpaque(Span<Bgra32> row)
8181
ReadOnlySpan<byte> rowBytes = MemoryMarshal.AsBytes(row);
8282
int i = 0;
8383
int length = (row.Length * 4) - 3;
84-
fixed (byte* src = rowBytes)
84+
fixed (byte* src = &MemoryMarshal.GetReference(rowBytes))
8585
{
8686
for (; i + 64 <= length; i += 64)
8787
{

src/ImageSharp/Memory/Buffer2DExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal static unsafe void DangerousCopyColumns<T>(
5050

5151
Span<byte> span = MemoryMarshal.AsBytes(buffer.DangerousGetSingleMemory().Span);
5252

53-
fixed (byte* ptr = span)
53+
fixed (byte* ptr = &MemoryMarshal.GetReference(span))
5454
{
5555
byte* basePtr = ptr;
5656
for (int y = 0; y < buffer.Height; y++)

0 commit comments

Comments
 (0)