From 8fc424a38af9b0cea84cb81c8dbc4930033322a8 Mon Sep 17 00:00:00 2001 From: woutware <35376607+woutware@users.noreply.github.com> Date: Sat, 28 Apr 2018 11:19:59 +0200 Subject: [PATCH] Minor performance refactorings. Moved the division out of the loop, division is expensive. --- .../Drawing/Processors/FillRegionProcessor.cs | 10 +++++----- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs b/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs index aa0b2e9b23..95ac3fe298 100644 --- a/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs +++ b/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs @@ -100,6 +100,8 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors using (BasicArrayBuffer scanline = source.MemoryManager.AllocateFake(scanlineWidth)) { bool scanlineDirty = true; + float subpixelFraction = 1f / subpixelCount; + float subpixelFractionPoint = subpixelFraction / subpixelCount; for (int y = minY; y < maxY; y++) { if (scanlineDirty) @@ -113,9 +115,8 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors scanlineDirty = false; } - float subpixelFraction = 1f / subpixelCount; - float subpixelFractionPoint = subpixelFraction / subpixelCount; - for (float subPixel = (float)y; subPixel < y + 1; subPixel += subpixelFraction) + float yPlusOne = y + 1; + for (float subPixel = (float)y; subPixel < yPlusOne; subPixel += subpixelFraction) { int pointsFound = region.Scan(subPixel + offset, buffer.Array, 0); if (pointsFound == 0) @@ -197,8 +198,7 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors private static void QuickSort(Span data) { - int hi = Math.Min(data.Length - 1, data.Length - 1); - QuickSort(data, 0, hi); + QuickSort(data, 0, data.Length - 1); } private static void QuickSort(Span data, int lo, int hi) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 777ee1f543..b95e102c76 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -321,9 +321,6 @@ namespace SixLabors.ImageSharp.Formats.Png /// The private IManagedByteBuffer GetOptimalFilteredScanline() { - Span scanSpan = this.rawScanline.Span; - Span prevSpan = this.previousScanline.Span; - // Palette images don't compress well with adaptive filtering. if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) { @@ -331,6 +328,9 @@ namespace SixLabors.ImageSharp.Formats.Png return this.result; } + Span scanSpan = this.rawScanline.Span; + Span prevSpan = this.previousScanline.Span; + // This order, while different to the enumerated order is more likely to produce a smaller sum // early on which shaves a couple of milliseconds off the processing time. UpFilter.Encode(scanSpan, prevSpan, this.up.Span, out int currentSum);