From c567762368867873259a37960dadb8a8b335a255 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 12 Mar 2020 23:02:43 +0100 Subject: [PATCH] fix GetSingleSpan() & GetSingleMemory() --- src/ImageSharp/Memory/Buffer2D{T}.cs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index 16b3b90630..bf8630931a 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -171,7 +171,7 @@ namespace SixLabors.ImageSharp.Memory 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(); + return this.cachedMemory.Length != 0 ? this.cachedMemory.Span : this.GetSingleSpanSlow(); } /// @@ -186,7 +186,7 @@ namespace SixLabors.ImageSharp.Memory 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(); + return this.cachedMemory.Length != 0 ? this.cachedMemory : this.GetSingleMemorySlow(); } /// @@ -205,6 +205,9 @@ namespace SixLabors.ImageSharp.Memory [MethodImpl(InliningOptions.ColdPath)] private Memory GetSingleMemorySlow() => this.FastMemoryGroup.Single(); + [MethodImpl(InliningOptions.ColdPath)] + private Span GetSingleSpanSlow() => this.FastMemoryGroup.Single().Span; + [MethodImpl(InliningOptions.ColdPath)] private ref T GetElementSlow(int x, int y) { @@ -230,17 +233,5 @@ 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!"); - } } }