From 48d3963cddcf8d804e55fb57866227f737fa1f41 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 29 Feb 2020 16:52:36 +0100 Subject: [PATCH] cold/hot path microoptimizations based on Resize profile --- .../ArrayPoolMemoryAllocator.Buffer{T}.cs | 9 +++- .../Allocators/ArrayPoolMemoryAllocator.cs | 8 +++- src/ImageSharp/Memory/Buffer2DExtensions.cs | 46 ------------------- src/ImageSharp/Memory/Buffer2D{T}.cs | 46 +++++++++++++++++++ .../Transforms/Resize/ResizeKernelMap.cs | 18 +++++--- 5 files changed, 72 insertions(+), 55 deletions(-) diff --git a/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.Buffer{T}.cs b/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.Buffer{T}.cs index 7a8b4f8bd..131abc5d2 100644 --- a/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.Buffer{T}.cs +++ b/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.Buffer{T}.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory.Internals; @@ -50,7 +51,7 @@ namespace SixLabors.ImageSharp.Memory { if (this.Data == null) { - throw new ObjectDisposedException("ArrayPoolMemoryAllocator.Buffer"); + ThrowObjectDisposedException(); } return MemoryMarshal.Cast(this.Data.AsSpan()).Slice(0, this.length); @@ -74,6 +75,12 @@ namespace SixLabors.ImageSharp.Memory } protected override object GetPinnableObject() => this.Data; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void ThrowObjectDisposedException() + { + throw new ObjectDisposedException("ArrayPoolMemoryAllocator.Buffer"); + } } /// diff --git a/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.cs index 8043c1888..4a04cb5d6 100644 --- a/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.cs +++ b/src/ImageSharp/Memory/Allocators/ArrayPoolMemoryAllocator.cs @@ -133,8 +133,7 @@ namespace SixLabors.ImageSharp.Memory int bufferSizeInBytes = length * itemSizeBytes; if (bufferSizeInBytes < 0 || bufferSizeInBytes > this.BufferCapacityInBytes) { - throw new InvalidMemoryOperationException( - $"Requested allocation: {length} elements of {typeof(T).Name} is over the capacity of the MemoryAllocator."); + ThrowInvalidAllocationException(length); } ArrayPool pool = this.GetArrayPool(bufferSizeInBytes); @@ -171,6 +170,11 @@ namespace SixLabors.ImageSharp.Memory return maxPoolSizeInBytes / 4; } + [MethodImpl(InliningOptions.ColdPath)] + private static void ThrowInvalidAllocationException(int length) => + throw new InvalidMemoryOperationException( + $"Requested allocation: {length} elements of {typeof(T).Name} is over the capacity of the MemoryAllocator."); + private ArrayPool GetArrayPool(int bufferSizeInBytes) { return bufferSizeInBytes <= this.PoolSelectorThresholdInBytes ? this.normalArrayPool : this.largeArrayPool; diff --git a/src/ImageSharp/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs index 8b0f3845e..fea44f52c 100644 --- a/src/ImageSharp/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -27,52 +27,6 @@ namespace SixLabors.ImageSharp.Memory return buffer.FastMemoryGroup.View; } - /// - /// Gets a to the backing data of - /// if the backing group consists of one single contiguous memory buffer. - /// Throws otherwise. - /// - /// The . - /// The value type. - /// The referencing the memory area. - /// - /// Thrown when the backing group is discontiguous. - /// - internal static Span GetSingleSpan(this Buffer2D buffer) - where T : struct - { - Guard.NotNull(buffer, nameof(buffer)); - if (buffer.FastMemoryGroup.Count > 1) - { - throw new InvalidOperationException("GetSingleSpan is only valid for a single-buffer group!"); - } - - return buffer.FastMemoryGroup.Single().Span; - } - - /// - /// Gets a to the backing data of - /// if the backing group consists of one single contiguous memory buffer. - /// Throws otherwise. - /// - /// The . - /// The value type. - /// The . - /// - /// Thrown when the backing group is discontiguous. - /// - internal static Memory GetSingleMemory(this Buffer2D buffer) - where T : struct - { - Guard.NotNull(buffer, nameof(buffer)); - if (buffer.FastMemoryGroup.Count > 1) - { - throw new InvalidOperationException("GetSingleMemory is only valid for a single-buffer group!"); - } - - return buffer.FastMemoryGroup.Single(); - } - /// /// TODO: Does not work with multi-buffer groups, should be specific to Resize. /// Copy columns of inplace, diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index f22b9a875..16b3b9063 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -158,6 +159,36 @@ namespace SixLabors.ImageSharp.Memory return this.FastMemoryGroup.View.GetBoundedSlice(y * this.Width, this.Width); } + /// + /// Gets a to the backing data if the backing group consists of a single contiguous memory buffer. + /// Throws otherwise. + /// + /// The referencing the memory area. + /// + /// Thrown when the backing group is discontiguous. + /// + [MethodImpl(InliningOptions.ShortMethod)] + internal Span GetSingleSpan() + { + // TODO: If we need a public version of this method, we need to cache the non-fast Memory of this.MemoryGroup + return this.cachedMemory.Length != 0 ? this.cachedMemory.Span : ThrowInvalidOperationSingleSpan(); + } + + /// + /// Gets a to the backing data of if the backing group consists of a single contiguous memory buffer. + /// Throws otherwise. + /// + /// The . + /// + /// Thrown when the backing group is discontiguous. + /// + [MethodImpl(InliningOptions.ShortMethod)] + internal Memory GetSingleMemory() + { + // TODO: If we need a public version of this method, we need to cache the non-fast Memory of this.MemoryGroup + return this.cachedMemory.Length != 0 ? this.cachedMemory : ThrowInvalidOperationSingleMemory(); + } + /// /// Swaps the contents of 'destination' with 'source' if the buffers are owned (1), /// copies the contents of 'source' to 'destination' otherwise (2). Buffers should be of same size in case 2! @@ -171,6 +202,9 @@ namespace SixLabors.ImageSharp.Memory [MethodImpl(InliningOptions.ColdPath)] private Memory GetRowMemorySlow(int y) => this.FastMemoryGroup.GetBoundedSlice(y * this.Width, this.Width); + [MethodImpl(InliningOptions.ColdPath)] + private Memory GetSingleMemorySlow() => this.FastMemoryGroup.Single(); + [MethodImpl(InliningOptions.ColdPath)] private ref T GetElementSlow(int x, int y) { @@ -196,5 +230,17 @@ namespace SixLabors.ImageSharp.Memory b.cachedMemory = aCached; } } + + [MethodImpl(InliningOptions.ColdPath)] + private static Memory ThrowInvalidOperationSingleMemory() + { + throw new InvalidOperationException("GetSingleMemory is only valid for a single-buffer group!"); + } + + [MethodImpl(InliningOptions.ColdPath)] + private static Span ThrowInvalidOperationSingleSpan() + { + throw new InvalidOperationException("GetSingleSpan is only valid for a single-buffer group!"); + } } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs index 3e7ccbd0a..5390cbbd1 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; +using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory; @@ -249,12 +250,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms private unsafe ResizeKernel CreateKernel(int dataRowIndex, int left, int right) { int length = right - left + 1; - - if (length > this.data.Width) - { - throw new InvalidOperationException( - $"Error in KernelMap.CreateKernel({dataRowIndex},{left},{right}): left > this.data.Width"); - } + this.ValidateSizesForCreateKernel(length, dataRowIndex, left, right); Span rowSpan = this.data.GetRowSpan(dataRowIndex); @@ -262,5 +258,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms float* rowPtr = (float*)Unsafe.AsPointer(ref rowReference); return new ResizeKernel(left, rowPtr, length); } + + [Conditional("DEBUG")] + private void ValidateSizesForCreateKernel(int length, int dataRowIndex, int left, int right) + { + if (length > this.data.Width) + { + throw new InvalidOperationException( + $"Error in KernelMap.CreateKernel({dataRowIndex},{left},{right}): left > this.data.Width"); + } + } } }