From 1968d3724fa81ac381145d4acc1dd1d36e89154f Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 5 Feb 2021 18:18:15 +0100 Subject: [PATCH] better JpegEncoder profiling/benchmarks --- .../Formats/Jpeg/Components/Block8x8F.cs | 4 ++-- src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs | 6 +++--- .../Codecs/Jpeg/EncodeJpeg.cs | 13 ++++++++----- .../ImageSharp.Tests.ProfilingSandbox/Program.cs | 9 ++++++++- .../JpegProfilingBenchmarks.cs | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index ddbac2d072..56afae68c7 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// The float value at the specified index public float this[int idx] { - [MethodImpl(InliningOptions.ShortMethod)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] get { GuardBlockIndex(idx); @@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components return Unsafe.Add(ref selfRef, idx); } - [MethodImpl(InliningOptions.ShortMethod)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] set { GuardBlockIndex(idx); diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 422c7bd7ec..d26fbb936d 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -315,7 +315,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// The packed bits. /// The number of bits /// The reference to the emitBuffer. - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private void Emit(uint bits, uint count, ref byte emitBufferBase) { count += this.bitCount; @@ -356,7 +356,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// The index of the Huffman encoder /// The value to encode. /// The reference to the emit buffer. - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private void EmitHuff(HuffIndex index, int value, ref byte emitBufferBase) { uint x = HuffmanLut.TheHuffmanLut[(int)index].Values[value]; @@ -370,7 +370,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// The number of copies to encode. /// The value to encode. /// The reference to the emit buffer. - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private void EmitHuffRLE(HuffIndex index, int runLength, int value, ref byte emitBufferBase) { int a = value; diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpeg.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpeg.cs index 81a5604f1e..5a9ceea946 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpeg.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpeg.cs @@ -16,17 +16,20 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg private Stream bmpStream; private SDImage bmpDrawing; private Image bmpCore; + private MemoryStream destinationStream; [GlobalSetup] public void ReadImages() { if (this.bmpStream == null) { - const string TestImage = TestImages.Bmp.NegHeight; + const string TestImage = TestImages.Jpeg.BenchmarkSuite.Jpeg420Exif_MidSizeYCbCr; this.bmpStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage)); this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore.Metadata.ExifProfile = null; this.bmpStream.Position = 0; this.bmpDrawing = SDImage.FromStream(this.bmpStream); + this.destinationStream = new MemoryStream(); } } @@ -42,15 +45,15 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg [Benchmark(Baseline = true, Description = "System.Drawing Jpeg")] public void JpegSystemDrawing() { - using var stream = new MemoryStream(); - this.bmpDrawing.Save(stream, ImageFormat.Jpeg); + this.bmpDrawing.Save(this.destinationStream, ImageFormat.Jpeg); + this.destinationStream.Seek(0, SeekOrigin.Begin); } [Benchmark(Description = "ImageSharp Jpeg")] public void JpegCore() { - using var stream = new MemoryStream(); - this.bmpCore.SaveAsJpeg(stream); + this.bmpCore.SaveAsJpeg(this.destinationStream); + this.destinationStream.Seek(0, SeekOrigin.Begin); } } } diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index 5504a99784..50a930b6f1 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -31,14 +31,21 @@ namespace SixLabors.ImageSharp.Tests.ProfilingSandbox /// public static void Main(string[] args) { + RunJpegEncoderProfilingTests(); // RunJpegColorProfilingTests(); - RunDecodeJpegProfilingTests(); + // RunDecodeJpegProfilingTests(); // RunToVector4ProfilingTest(); // RunResizeProfilingTest(); Console.ReadLine(); } + private static void RunJpegEncoderProfilingTests() + { + var benchmarks = new JpegProfilingBenchmarks(new ConsoleOutput()); + benchmarks.EncodeJpeg_SingleMidSize(); + } + private static void RunJpegColorProfilingTests() { new JpegColorConverterTests(new ConsoleOutput()).BenchmarkYCbCr(false); diff --git a/tests/ImageSharp.Tests/ProfilingBenchmarks/JpegProfilingBenchmarks.cs b/tests/ImageSharp.Tests/ProfilingBenchmarks/JpegProfilingBenchmarks.cs index 78fb99802e..9de3fc8dfe 100644 --- a/tests/ImageSharp.Tests/ProfilingBenchmarks/JpegProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/ProfilingBenchmarks/JpegProfilingBenchmarks.cs @@ -75,6 +75,21 @@ namespace SixLabors.ImageSharp.Tests.ProfilingBenchmarks #pragma warning restore SA1515 // Single-line comment should be preceded by blank line } + [Fact(Skip = ProfilingSetup.SkipProfilingTests)] + public void EncodeJpeg_SingleMidSize() + { + string path = TestFile.GetInputFileFullPath(TestImages.Jpeg.BenchmarkSuite.Jpeg420Exif_MidSizeYCbCr); + using var image = Image.Load(path); + image.Metadata.ExifProfile = null; + + using var ms = new MemoryStream(); + for (int i = 0; i < 30; i++) + { + image.SaveAsJpeg(ms); + ms.Seek(0, SeekOrigin.Begin); + } + } + // Benchmark, enable manually! [Theory(Skip = ProfilingSetup.SkipProfilingTests)] [InlineData(1, 75, JpegSubsample.Ratio420)]