Skip to content

Commit 3a10e93

Browse files
authored
Merge pull request #1828 from turbedi/minor_optimizations
Some minor optimizations
2 parents a39fe4c + c22919d commit 3a10e93

File tree

9 files changed

+28
-18
lines changed

9 files changed

+28
-18
lines changed

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ private void WriteJfifApplicationHeader(ImageMetadata meta)
288288
/// <param name="componentCount">The number of components to write.</param>
289289
private void WriteDefineHuffmanTables(int componentCount)
290290
{
291+
// This uses a C#'s compiler optimization that refers to the static data segment of the assembly,
292+
// and doesn't incur any allocation at all.
291293
// Table identifiers.
292-
ReadOnlySpan<byte> headers = stackalloc byte[]
294+
ReadOnlySpan<byte> headers = new byte[]
293295
{
294296
0x00,
295297
0x10,

src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffLzwEncoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ private void WriteCode(Stream stream, int code)
256256

257257
private void ResetTables()
258258
{
259-
this.children.GetSpan().Fill(0);
260-
this.siblings.GetSpan().Fill(0);
259+
this.children.GetSpan().Clear();
260+
this.siblings.GetSpan().Clear();
261261
this.bitsPerCode = MinBits;
262262
this.maxCode = MaxValue(this.bitsPerCode);
263263
this.nextValidCode = EoiCode + 1;

src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
6464
uint bitsWritten = 0;
6565
for (int y = 0; y < height; y++)
6666
{
67-
scanLine.Fill(0);
67+
scanLine.Clear();
6868
Decode2DScanline(bitReader, this.isWhiteZero, referenceScanLine, scanLine);
6969

7070
bitsWritten = this.WriteScanLine(buffer, scanLine, bitsWritten);
@@ -116,7 +116,15 @@ private static void Decode2DScanline(T6BitReader bitReader, bool whiteIsZero, Cc
116116
{
117117
// If a TIFF reader encounters EOFB before the expected number of lines has been extracted,
118118
// it is appropriate to assume that the missing rows consist entirely of white pixels.
119-
scanline.Fill(whiteIsZero ? (byte)0 : (byte)255);
119+
if (whiteIsZero)
120+
{
121+
scanline.Clear();
122+
}
123+
else
124+
{
125+
scanline.Fill((byte)255);
126+
}
127+
120128
break;
121129
}
122130

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static void ConvertPopulationCountTableToBitEstimates(int numSymbols, ui
8787

8888
if (nonzeros <= 1)
8989
{
90-
output.AsSpan(0, numSymbols).Fill(0);
90+
output.AsSpan(0, numSymbols).Clear();
9191
}
9292
else
9393
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ private static void OptimizeHistogramSymbols(ushort[] clusterMappings, int numCl
287287

288288
// Create a mapping from a cluster id to its minimal version.
289289
int clusterMax = 0;
290-
clusterMappingsTmp.AsSpan().Fill(0);
290+
clusterMappingsTmp.AsSpan().Clear();
291291

292292
// Re-map the ids.
293293
for (int i = 0; i < symbols.Length; i++)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static class HuffmanUtils
2828
public static void CreateHuffmanTree(uint[] histogram, int treeDepthLimit, bool[] bufRle, HuffmanTree[] huffTree, HuffmanTreeCode huffCode)
2929
{
3030
int numSymbols = huffCode.NumSymbols;
31-
bufRle.AsSpan().Fill(false);
31+
bufRle.AsSpan().Clear();
3232
OptimizeHuffmanForRle(numSymbols, bufRle, histogram);
3333
GenerateOptimalTree(huffTree, histogram, numSymbols, treeDepthLimit, huffCode.CodeLengths);
3434

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ private void AddLiteral(Vp8LHistogram b, Vp8LHistogram output, int literalSize)
320320
}
321321
else
322322
{
323-
output.Literal.AsSpan(0, literalSize).Fill(0);
323+
output.Literal.AsSpan(0, literalSize).Clear();
324324
}
325325
}
326326

@@ -343,7 +343,7 @@ private void AddRed(Vp8LHistogram b, Vp8LHistogram output, int size)
343343
}
344344
else
345345
{
346-
output.Red.AsSpan(0, size).Fill(0);
346+
output.Red.AsSpan(0, size).Clear();
347347
}
348348
}
349349

@@ -366,7 +366,7 @@ private void AddBlue(Vp8LHistogram b, Vp8LHistogram output, int size)
366366
}
367367
else
368368
{
369-
output.Blue.AsSpan(0, size).Fill(0);
369+
output.Blue.AsSpan(0, size).Clear();
370370
}
371371
}
372372

@@ -389,7 +389,7 @@ private void AddAlpha(Vp8LHistogram b, Vp8LHistogram output, int size)
389389
}
390390
else
391391
{
392-
output.Alpha.AsSpan(0, size).Fill(0);
392+
output.Alpha.AsSpan(0, size).Clear();
393393
}
394394
}
395395

@@ -412,7 +412,7 @@ private void AddDistance(Vp8LHistogram b, Vp8LHistogram output, int size)
412412
}
413413
else
414414
{
415-
output.Distance.AsSpan(0, size).Fill(0);
415+
output.Distance.AsSpan(0, size).Clear();
416416
}
417417
}
418418

src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,22 +911,22 @@ private void InitLeft()
911911

912912
this.LeftNz[8] = 0;
913913

914-
this.LeftDerr.AsSpan().Fill(0);
914+
this.LeftDerr.AsSpan().Clear();
915915
}
916916

917917
private void InitTop()
918918
{
919919
int topSize = this.mbw * 16;
920920
this.YTop.AsSpan(0, topSize).Fill(127);
921921
this.UvTop.AsSpan().Fill(127);
922-
this.Nz.AsSpan().Fill(0);
922+
this.Nz.AsSpan().Clear();
923923

924924
int predsW = (4 * this.mbw) + 1;
925925
int predsH = (4 * this.mbh) + 1;
926926
int predsSize = predsW * predsH;
927-
this.Preds.AsSpan(predsSize + this.predsWidth, this.mbw).Fill(0);
927+
this.Preds.AsSpan(predsSize + this.predsWidth, this.mbw).Clear();
928928

929-
this.TopDerr.AsSpan().Fill(0);
929+
this.TopDerr.AsSpan().Clear();
930930
}
931931

932932
private int Bit(uint nz, int n) => (nz & (1 << n)) != 0 ? 1 : 0;

src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ private void ResetBoundaryPredictions()
546546
int predsW = (4 * this.Mbw) + 1;
547547
int predsH = (4 * this.Mbh) + 1;
548548
int predsSize = predsW * predsH;
549-
this.Preds.AsSpan(predsSize + this.PredsWidth - 4, 4).Fill(0);
549+
this.Preds.AsSpan(predsSize + this.PredsWidth - 4, 4).Clear();
550550

551551
this.Nz[0] = 0; // constant
552552
}

0 commit comments

Comments
 (0)