From ac3eb80374eff10447410d0d9402ab0aa5c7124a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 4 Feb 2021 03:42:06 +0000 Subject: [PATCH] Optimize and fix warnings. --- .../Formats/Jpeg/Components/Block8x8F.cs | 17 +++++------------ .../Formats/Jpeg/Components/RowOctet.cs | 11 ++++++----- .../Block8x8F_Scale16X16To8X8.cs | 5 ++++- .../Image/ImageTests.WrapMemory.cs | 2 +- .../TestUtilities/TestEnvironment.cs | 2 +- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index 3a0fce781..ddbac2d07 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -153,10 +153,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// [MethodImpl(InliningOptions.ShortMethod)] public void Clear() - { - // The cheapest way to do this in C#: - this = default; - } + => this = default; // The cheapest way to do this in C#: /// /// Load raw 32bit floating point data from source. @@ -178,9 +175,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// Source [MethodImpl(InliningOptions.ShortMethod)] public static unsafe void LoadFrom(Block8x8F* blockPtr, Span source) - { - blockPtr->LoadFrom(source); - } + => blockPtr->LoadFrom(source); /// /// Load raw 32bit floating point data from source @@ -234,9 +229,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// The destination. [MethodImpl(InliningOptions.ShortMethod)] public static unsafe void ScaledCopyTo(Block8x8F* blockPtr, Span dest) - { - blockPtr->ScaledCopyTo(dest); - } + => blockPtr->ScaledCopyTo(dest); /// /// Copy raw 32bit floating point data to dest @@ -437,7 +430,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// The block pointer. /// The qt pointer. /// Unzig pointer - // [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void DequantizeBlock(Block8x8F* blockPtr, Block8x8F* qtPtr, byte* unzigPtr) { float* b = (float*)blockPtr; @@ -565,7 +557,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components ref Vector256 bBase = ref Unsafe.AsRef(Unsafe.As>(ref b.V0L)); ref Vector256 aEnd = ref Unsafe.Add(ref aBase, 8); - while (Unsafe.IsAddressLessThan(ref aBase, ref aEnd)) + do { Vector256 voff = Avx.Multiply(Avx.Min(Avx.Max(vnegOne, aBase), vone), vadd); Unsafe.Add(ref aBase, 0) = Avx.Add(Avx.Divide(aBase, bBase), voff); @@ -573,6 +565,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components aBase = ref Unsafe.Add(ref aBase, 1); bBase = ref Unsafe.Add(ref bBase, 1); } + while (Unsafe.IsAddressLessThan(ref aBase, ref aEnd)); } else #endif diff --git a/src/ImageSharp/Formats/Jpeg/Components/RowOctet.cs b/src/ImageSharp/Formats/Jpeg/Components/RowOctet.cs index 8234c3974..f35bb4468 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/RowOctet.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/RowOctet.cs @@ -78,14 +78,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(Buffer2D buffer, int startY) { - int y = startY; - int height = buffer.Height; - // We don't actually have to assign values outside of the // frame pixel buffer since they are never requested. - for (int i = 0; i < 8 && y < height; i++) + int y = startY; + int yEnd = Math.Min(y + 8, buffer.Height); + + int i = 0; + while (y < yEnd) { - this[i] = buffer.GetRowSpan(y++); + this[i++] = buffer.GetRowSpan(y++); } } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Scale16X16To8X8.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Scale16X16To8X8.cs index 818829760..ebd3e4013 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Scale16X16To8X8.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Scale16X16To8X8.cs @@ -1,3 +1,6 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + using System; using BenchmarkDotNet.Attributes; using SixLabors.ImageSharp.Formats.Jpeg.Components; @@ -15,7 +18,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Format.Jpeg.Components { var random = new Random(); - float[] f = new float[8*8]; + float[] f = new float[8 * 8]; for (int i = 0; i < f.Length; i++) { f[i] = (float)random.NextDouble(); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index 16d0baff3..17c73cc83 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -375,7 +375,7 @@ namespace SixLabors.ImageSharp.Tests var array = new Rgba32[size]; var memory = new Memory(array); - Image.WrapMemory(memory, height, width); + Image.WrapMemory(memory, height, width); } private class TestMemoryOwner : IMemoryOwner diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index b80a29646..8d1b0f793 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -263,7 +263,7 @@ namespace SixLabors.ImageSharp.Tests private static string GetNetCoreVersion() { Assembly assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly; - string[] assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); + string[] assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App"); if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2) {