Skip to content

Commit ff9d3a6

Browse files
committed
Replace indexed span iteration with foreach (+performance)
1 parent 0981832 commit ff9d3a6

File tree

6 files changed

+19
-26
lines changed

6 files changed

+19
-26
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ private static int CalculateBestCacheSize(
149149
}
150150

151151
// Find the cacheBits giving the lowest entropy.
152-
for (int idx = 0; idx < refs.Count; idx++)
152+
foreach (PixOrCopy v in refs)
153153
{
154-
PixOrCopy v = refs[idx];
155154
if (v.IsLiteral())
156155
{
157156
uint pix = bgra[pos++];
@@ -780,17 +779,16 @@ private static void BackwardRefsWithLocalCache(ReadOnlySpan<uint> bgra, int cach
780779
{
781780
int pixelIndex = 0;
782781
ColorCache colorCache = new(cacheBits);
783-
for (int idx = 0; idx < refs.Count; idx++)
782+
foreach (ref PixOrCopy v in refs)
784783
{
785-
PixOrCopy v = refs[idx];
786784
if (v.IsLiteral())
787785
{
788786
uint bgraLiteral = v.BgraOrDistance;
789787
int ix = colorCache.Contains(bgraLiteral);
790788
if (ix >= 0)
791789
{
792790
// Color cache contains bgraLiteral
793-
refs[idx] = PixOrCopy.CreateCacheIdx(ix);
791+
v = PixOrCopy.CreateCacheIdx(ix);
794792
}
795793
else
796794
{
@@ -812,15 +810,13 @@ private static void BackwardRefsWithLocalCache(ReadOnlySpan<uint> bgra, int cach
812810

813811
private static void BackwardReferences2DLocality(int xSize, Vp8LBackwardRefs refs)
814812
{
815-
for (int idx = 0; idx < refs.Count; idx++)
813+
foreach (ref PixOrCopy v in refs)
816814
{
817-
PixOrCopy v = refs[idx];
818-
819815
if (v.IsCopy())
820816
{
821817
int dist = (int)v.BgraOrDistance;
822818
int transformedDist = DistanceToPlaneCode(xSize, dist);
823-
refs[idx] = PixOrCopy.CreateCopy((uint)transformedDist, v.Len);
819+
v = PixOrCopy.CreateCopy((uint)transformedDist, v.Len);
824820
}
825821
}
826822
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public void Build(int xSize, int cacheBits, Vp8LBackwardRefs backwardRefs)
4040
using OwnedVp8LHistogram histogram = OwnedVp8LHistogram.Create(this.memoryAllocator, cacheBits);
4141

4242
// The following code is similar to HistogramCreate but converts the distance to plane code.
43-
for (int i = 0; i < backwardRefs.Count; i++)
43+
foreach (PixOrCopy v in backwardRefs)
4444
{
45-
histogram.AddSinglePixOrCopy(backwardRefs[i], true, xSize);
45+
histogram.AddSinglePixOrCopy(v, true, xSize);
4646
}
4747

4848
ConvertPopulationCountTableToBitEstimates(histogram.NumCodes(), histogram.Literal, this.Literal);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ private static void HistogramBuild(
110110
int x = 0, y = 0;
111111
int histoXSize = LosslessUtils.SubSampleSize(xSize, histoBits);
112112

113-
for (int i = 0; i < backwardRefs.Count; i++)
113+
foreach (PixOrCopy v in backwardRefs)
114114
{
115-
PixOrCopy v = backwardRefs[i];
116115
int ix = ((y >> histoBits) * histoXSize) + (x >> histoBits);
117116
histograms[ix].AddSinglePixOrCopy(v, false);
118117
x += v.Len;
@@ -217,7 +216,7 @@ private static void HistogramCombineEntropyBin(
217216
clusterMappings[idx] = (ushort)idx;
218217
}
219218

220-
List<int> indicesToRemove = new();
219+
List<int> indicesToRemove = [];
221220
Vp8LStreaks stats = new();
222221
Vp8LBitEntropy bitsEntropy = new();
223222
for (int idx = 0; idx < histograms.Count; idx++)
@@ -345,7 +344,7 @@ private static bool HistogramCombineStochastic(Vp8LHistogramSet histograms, int
345344

346345
// Priority list of histogram pairs. Its size impacts the quality of the compression and the speed:
347346
// the smaller the faster but the worse for the compression.
348-
List<HistogramPair> histoPriorityList = new();
347+
List<HistogramPair> histoPriorityList = [];
349348
const int maxSize = 9;
350349

351350
// Fill the initial mapping.
@@ -480,7 +479,7 @@ private static void HistogramCombineGreedy(Vp8LHistogramSet histograms)
480479
int histoSize = histograms.Count(h => h != null);
481480

482481
// Priority list of histogram pairs.
483-
List<HistogramPair> histoPriorityList = new();
482+
List<HistogramPair> histoPriorityList = [];
484483
int maxSize = histoSize * histoSize;
485484
Vp8LStreaks stats = new();
486485
Vp8LBitEntropy bitsEntropy = new();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless;
99
internal class Vp8LBackwardRefs : IDisposable
1010
{
1111
private readonly IMemoryOwner<PixOrCopy> refs;
12+
private int count;
1213

1314
public Vp8LBackwardRefs(MemoryAllocator memoryAllocator, int pixels)
1415
{
1516
this.refs = memoryAllocator.Allocate<PixOrCopy>(pixels);
16-
this.Count = 0;
17+
this.count = 0;
1718
}
1819

19-
public int Count { get; private set; }
20+
public void Add(PixOrCopy pixOrCopy) => this.refs.Memory.Span[this.count++] = pixOrCopy;
2021

21-
public ref PixOrCopy this[int index] => ref this.refs.Memory.Span[index];
22+
public void Clear() => this.count = 0;
2223

23-
public void Add(PixOrCopy pixOrCopy) => this.refs.Memory.Span[this.Count++] = pixOrCopy;
24-
25-
public void Clear() => this.Count = 0;
24+
public Span<PixOrCopy>.Enumerator GetEnumerator() => this.refs.Slice(0, this.count).GetEnumerator();
2625

2726
/// <inheritdoc/>
2827
public void Dispose() => this.refs.Dispose();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,8 @@ private void StoreImageToBitMask(
10581058
int histogramIx = histogramSymbols[0];
10591059
Span<HuffmanTreeCode> codes = huffmanCodes.AsSpan(5 * histogramIx);
10601060

1061-
for (int i = 0; i < backwardRefs.Count; i++)
1061+
foreach (PixOrCopy v in backwardRefs)
10621062
{
1063-
PixOrCopy v = backwardRefs[i];
10641063
if (tileX != (x & tileMask) || tileY != (y & tileMask))
10651064
{
10661065
tileX = x & tileMask;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ public void Clear()
138138
/// <param name="refs">The backward references.</param>
139139
public void StoreRefs(Vp8LBackwardRefs refs)
140140
{
141-
for (int i = 0; i < refs.Count; i++)
141+
foreach (PixOrCopy v in refs)
142142
{
143-
this.AddSinglePixOrCopy(refs[i], false);
143+
this.AddSinglePixOrCopy(v, false);
144144
}
145145
}
146146

0 commit comments

Comments
 (0)