From acf9d85e8ca8a5ebd4894e85e1d6fe82d2e097b2 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 14 May 2021 00:36:40 +0300 Subject: [PATCH] Moved dispose control logic to base Image class internal call EnsureNotDisposed is no longer virtual -> micro speedup gain in pixel index accessor Image[x, y]. --- src/ImageSharp/Image.cs | 22 ++++++++++++++++++++-- src/ImageSharp/Image{TPixel}.cs | 18 ------------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index c07c7fe83..3aa30063d 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -19,6 +19,8 @@ namespace SixLabors.ImageSharp /// public abstract partial class Image : IImage, IConfigurationProvider { + private bool isDisposed; + private Size size; private readonly Configuration configuration; @@ -78,7 +80,17 @@ namespace SixLabors.ImageSharp Configuration IConfigurationProvider.Configuration => this.configuration; /// - public void Dispose() => this.Dispose(true); + public void Dispose() + { + if (this.isDisposed) + { + return; + } + + this.Dispose(true); + + this.isDisposed = true; + } /// /// Saves the image to the given stream using the given image encoder. @@ -144,7 +156,13 @@ namespace SixLabors.ImageSharp /// /// Throws if the image is disposed. /// - internal abstract void EnsureNotDisposed(); + internal void EnsureNotDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException("Trying to execute an operation on a disposed image."); + } + } /// /// Accepts a . diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 9c32a2c31..c43643052 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -23,8 +23,6 @@ namespace SixLabors.ImageSharp public sealed class Image : Image where TPixel : unmanaged, IPixel { - private bool isDisposed; - private ImageFrameCollection frames; /// @@ -272,26 +270,10 @@ namespace SixLabors.ImageSharp /// protected override void Dispose(bool disposing) { - if (this.isDisposed) - { - return; - } - if (disposing) { this.frames.Dispose(); } - - this.isDisposed = true; - } - - /// - internal override void EnsureNotDisposed() - { - if (this.isDisposed) - { - throw new ObjectDisposedException("Trying to execute an operation on a disposed image."); - } } ///