Skip to content

Commit 1faf5a5

Browse files
committed
Removed some bound checks for arr[0] indexing to get a reference
1 parent 1920e28 commit 1faf5a5

File tree

10 files changed

+24
-19
lines changed

10 files changed

+24
-19
lines changed

src/ImageSharp/Compression/Zlib/DeflaterEngine.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Buffers;
55
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
67
using SixLabors.ImageSharp.Memory;
78

89
namespace SixLabors.ImageSharp.Compression.Zlib;
@@ -426,8 +427,8 @@ private int InsertString()
426427
private void SlideWindow()
427428
{
428429
Unsafe.CopyBlockUnaligned(
429-
ref this.window.Span[0],
430-
ref this.window.Span[DeflaterConstants.WSIZE],
430+
ref MemoryMarshal.GetReference(this.window.Span),
431+
ref Unsafe.Add(ref MemoryMarshal.GetReference(this.window.Span), DeflaterConstants.WSIZE),
431432
DeflaterConstants.WSIZE);
432433

433434
this.matchStart -= DeflaterConstants.WSIZE;

src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
5353

5454
private ArithmeticDecodingTable[] acDecodingTables;
5555

56-
private readonly byte[] fixedBin = { 113, 0, 0, 0 };
56+
// Use C#'s optimization to refer to assembly's data segment, no allocation occurs.
57+
private ReadOnlySpan<byte> fixedBin => new byte[] { 113, 0, 0, 0 };
5758

5859
private readonly CancellationToken cancellationToken;
5960

@@ -231,7 +232,7 @@ public void InitDecodingTables(List<ArithmeticDecodingTable> arithmeticDecodingT
231232
}
232233
}
233234

234-
private ref byte GetFixedBinReference() => ref this.fixedBin[0];
235+
private ref byte GetFixedBinReference() => ref MemoryMarshal.GetReference(fixedBin);
235236

236237
/// <summary>
237238
/// Decodes the entropy coded data.

src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticStatistics.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Runtime.InteropServices;
5+
46
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
57

68
internal class ArithmeticStatistics
@@ -18,7 +20,7 @@ public ArithmeticStatistics(bool dc, int identifier)
1820

1921
public int Identifier { get; private set; }
2022

21-
public ref byte GetReference() => ref this.statistics[0];
23+
public ref byte GetReference() => ref MemoryMarshal.GetArrayDataReference(this.statistics);
2224

2325
public ref byte GetReference(int offset) => ref this.statistics[offset];
2426

src/ImageSharp/Formats/Png/PngScanlineProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public static void ProcessPaletteScanline<TPixel>(
255255
// If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha
256256
// channel and we should try to read it.
257257
Rgba32 rgba = default;
258-
ref byte paletteAlphaRef = ref paletteAlpha[0];
258+
ref byte paletteAlphaRef = ref MemoryMarshal.GetArrayDataReference(paletteAlpha);
259259

260260
for (int x = 0; x < header.Width; x++)
261261
{
@@ -301,7 +301,7 @@ public static void ProcessInterlacedPaletteScanline<TPixel>(
301301
// If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha
302302
// channel and we should try to read it.
303303
Rgba32 rgba = default;
304-
ref byte paletteAlphaRef = ref paletteAlpha[0];
304+
ref byte paletteAlphaRef = ref MemoryMarshal.GetArrayDataReference(paletteAlpha);
305305
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o++)
306306
{
307307
int index = Unsafe.Add(ref scanlineSpanRef, (uint)o);

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Buffers;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78
using SixLabors.ImageSharp.IO;
89
using SixLabors.ImageSharp.Memory;
910
using SixLabors.ImageSharp.Metadata;
@@ -429,11 +430,11 @@ private void ReadBgra16<TPixel>(BufferedReadStream stream, int width, int height
429430

430431
if (this.fileHeader.ImageType == TgaImageType.BlackAndWhite)
431432
{
432-
color.FromLa16(Unsafe.As<byte, La16>(ref this.scratchBuffer[0]));
433+
color.FromLa16(Unsafe.As<byte, La16>(ref MemoryMarshal.GetArrayDataReference(this.scratchBuffer)));
433434
}
434435
else
435436
{
436-
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref this.scratchBuffer[0]));
437+
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref MemoryMarshal.GetArrayDataReference(this.scratchBuffer)));
437438
}
438439

439440
pixelSpan[x] = color;
@@ -695,7 +696,7 @@ private void ReadBgr24Pixel<TPixel>(BufferedReadStream stream, TPixel color, int
695696
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgr pixel");
696697
}
697698

698-
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref this.scratchBuffer[0]));
699+
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref MemoryMarshal.GetArrayDataReference(this.scratchBuffer)));
699700
pixelSpan[x] = color;
700701
}
701702

src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void Invoke(int y)
133133
// The target buffer is zeroed initially and then it accumulates the results
134134
// of each partial convolution, so we don't have to clear it here as well
135135
ref Vector4 targetBase = ref this.targetValues.GetElementUnsafe(boundsX, y);
136-
ref Complex64 kernelStart = ref this.kernel[0];
136+
ref Complex64 kernelStart = ref MemoryMarshal.GetArrayDataReference(this.kernel);
137137
ref Complex64 kernelEnd = ref Unsafe.Add(ref kernelStart, (uint)kernelSize);
138138

139139
while (Unsafe.IsAddressLessThan(ref kernelStart, ref kernelEnd))

src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void Invoke(int y, Span<Vector4> span)
244244
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(span);
245245
ref ComplexVector4 targetStart = ref MemoryMarshal.GetReference(targetBuffer);
246246
ref ComplexVector4 targetEnd = ref Unsafe.Add(ref targetStart, (uint)span.Length);
247-
ref Complex64 kernelBase = ref this.kernel[0];
247+
ref Complex64 kernelBase = ref MemoryMarshal.GetArrayDataReference(this.kernel);
248248
ref Complex64 kernelEnd = ref Unsafe.Add(ref kernelBase, (uint)kernelSize);
249249
ref int sampleColumnBase = ref MemoryMarshal.GetReference(this.map.GetColumnOffsetSpan());
250250

src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private void Convolve3(int y, Span<Vector4> span)
179179
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(sourceBuffer);
180180
ref Vector4 targetStart = ref MemoryMarshal.GetReference(targetBuffer);
181181
ref Vector4 targetEnd = ref Unsafe.Add(ref targetStart, (uint)sourceBuffer.Length);
182-
ref float kernelBase = ref this.kernel[0];
182+
ref float kernelBase = ref MemoryMarshal.GetArrayDataReference(this.kernel);
183183
ref float kernelEnd = ref Unsafe.Add(ref kernelBase, (uint)kernelSize);
184184
ref int sampleColumnBase = ref MemoryMarshal.GetReference(this.map.GetColumnOffsetSpan());
185185

@@ -243,7 +243,7 @@ private void Convolve4(int y, Span<Vector4> span)
243243
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(sourceBuffer);
244244
ref Vector4 targetStart = ref MemoryMarshal.GetReference(targetBuffer);
245245
ref Vector4 targetEnd = ref Unsafe.Add(ref targetStart, (uint)sourceBuffer.Length);
246-
ref float kernelBase = ref this.kernel[0];
246+
ref float kernelBase = ref MemoryMarshal.GetArrayDataReference(this.kernel);
247247
ref float kernelEnd = ref Unsafe.Add(ref kernelBase, (uint)kernelSize);
248248
ref int sampleColumnBase = ref MemoryMarshal.GetReference(this.map.GetColumnOffsetSpan());
249249

@@ -341,7 +341,7 @@ private void Convolve3(int y, Span<Vector4> span)
341341
targetBuffer.Clear();
342342

343343
ref Vector4 targetBase = ref MemoryMarshal.GetReference(targetBuffer);
344-
ref float kernelStart = ref this.kernel[0];
344+
ref float kernelStart = ref MemoryMarshal.GetArrayDataReference(this.kernel);
345345
ref float kernelEnd = ref Unsafe.Add(ref kernelStart, (uint)kernelSize);
346346

347347
Span<TPixel> sourceRow;
@@ -406,7 +406,7 @@ private void Convolve4(int y, Span<Vector4> span)
406406
targetBuffer.Clear();
407407

408408
ref Vector4 targetBase = ref MemoryMarshal.GetReference(targetBuffer);
409-
ref float kernelStart = ref this.kernel[0];
409+
ref float kernelStart = ref MemoryMarshal.GetArrayDataReference(this.kernel);
410410
ref float kernelEnd = ref Unsafe.Add(ref kernelStart, (uint)kernelSize);
411411

412412
Span<TPixel> sourceRow;

src/ImageSharp/Processing/Processors/Convolution/Parameters/BokehBlurKernelDataProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private static void NormalizeKernels(Complex64[][] kernels, Vector4[] kernelPara
192192
{
193193
ref Complex64[] kernelRef = ref Unsafe.Add(ref baseKernelsRef, (uint)i);
194194
int length = kernelRef.Length;
195-
ref Complex64 valueRef = ref kernelRef[0];
195+
ref Complex64 valueRef = ref MemoryMarshal.GetArrayDataReference(kernelRef);
196196
ref Vector4 paramsRef = ref Unsafe.Add(ref baseParamsRef, (uint)i);
197197

198198
for (int j = 0; j < length; j++)
@@ -214,7 +214,7 @@ private static void NormalizeKernels(Complex64[][] kernels, Vector4[] kernelPara
214214
{
215215
ref Complex64[] kernelsRef = ref Unsafe.Add(ref baseKernelsRef, (uint)i);
216216
int length = kernelsRef.Length;
217-
ref Complex64 valueRef = ref kernelsRef[0];
217+
ref Complex64 valueRef = ref MemoryMarshal.GetArrayDataReference(kernelsRef);
218218

219219
for (int j = 0; j < length; j++)
220220
{

src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ private void BuildCube()
684684
using IMemoryOwner<double> vvOwner = this.Configuration.MemoryAllocator.Allocate<double>(this.maxColors);
685685
Span<double> vv = vvOwner.GetSpan();
686686

687-
ref Box cube = ref this.colorCube[0];
687+
ref Box cube = ref MemoryMarshal.GetArrayDataReference(this.colorCube);
688688
cube.RMin = cube.GMin = cube.BMin = cube.AMin = 0;
689689
cube.RMax = cube.GMax = cube.BMax = IndexCount - 1;
690690
cube.AMax = IndexAlphaCount - 1;

0 commit comments

Comments
 (0)