From 564757f2ec9a728e40a362231d2ed4d64a03d0f0 Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Wed, 23 Aug 2023 14:25:14 -0400 Subject: [PATCH] Improve performance of hashing and reduce memory --- Damselfly.Core.ImageProcessing/ImageSharpProcessor.cs | 11 +++++++---- Damselfly.Core.ImageProcessing/SkiaSharpProcessor.cs | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Damselfly.Core.ImageProcessing/ImageSharpProcessor.cs b/Damselfly.Core.ImageProcessing/ImageSharpProcessor.cs index f60e64c4..454dab3c 100644 --- a/Damselfly.Core.ImageProcessing/ImageSharpProcessor.cs +++ b/Damselfly.Core.ImageProcessing/ImageSharpProcessor.cs @@ -1,4 +1,4 @@ -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using System.Security.Cryptography; using CoenM.ImageHash; using CoenM.ImageHash.HashAlgorithms; @@ -238,7 +238,7 @@ public static string GetHash(Image image) try { var hashWatch = new Stopwatch("CalcImageHash"); - var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); + using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); image.ProcessPixelRows(pixelAccessor => { @@ -246,12 +246,15 @@ public static string GetHash(Image image) { var pixelRowSpan = pixelAccessor.GetRowSpan(y); - var rgbaBytes = MemoryMarshal.AsBytes(pixelRowSpan).ToArray(); + var rgbaBytes = MemoryMarshal.AsBytes(pixelRowSpan); hash.AppendData(rgbaBytes); } }); - result = hash.GetHashAndReset().ToHex(true); + Span buffer = stackalloc byte[SHA1.HashSizeInBytes]; + hash.GetHashAndReset(buffer); + result = Convert.ToHexString(buffer); + hashWatch.Stop(); Logging.LogVerbose($"Hashed image ({result}) in {hashWatch.HumanElapsedTime}"); } diff --git a/Damselfly.Core.ImageProcessing/SkiaSharpProcessor.cs b/Damselfly.Core.ImageProcessing/SkiaSharpProcessor.cs index a8edc9f5..fd0d0c1a 100644 --- a/Damselfly.Core.ImageProcessing/SkiaSharpProcessor.cs +++ b/Damselfly.Core.ImageProcessing/SkiaSharpProcessor.cs @@ -1,4 +1,4 @@ -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using System.Security.Cryptography; using Damselfly.Core.Constants; using Damselfly.Core.DbModels.Images; @@ -187,17 +187,20 @@ public async Task TransformDownloadImage(string input, Stream output, IExportSet var pixels = sourceBitmap.GetPixelSpan(); - var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); + using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); for ( var row = 0; row < sourceBitmap.Height; row++ ) { var rowPixels = pixels.Slice(row * sourceBitmap.RowBytes, sourceBitmap.RowBytes); - var rgbaBytes = MemoryMarshal.AsBytes(rowPixels).ToArray(); + var rgbaBytes = MemoryMarshal.AsBytes(rowPixels); hash.AppendData(rgbaBytes); } - result = hash.GetHashAndReset().ToHex(true); + Span buffer = stackalloc byte[SHA1.HashSizeInBytes]; + hash.GetHashAndReset(buffer); + result = Convert.ToHexString(buffer); + hashWatch.Stop(); Logging.LogVerbose($"Hashed image ({result}) in {hashWatch.HumanElapsedTime}"); }