From 607571c9eb813feae5e2552fa7e11d562a9b1de2 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 13 Apr 2018 11:27:05 +1000 Subject: [PATCH] Use Unsafe.As per recommendation --- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 24 ++++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 8a67fbecb4..d05d1af487 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -13,6 +13,9 @@ using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Quantization; +// TODO: This is causing more GC collections than I'm happy with. +// This is likely due to the number of short writes to the stream we are doing. +// We should investigate reducing them since we know the length of the byte array we require for multiple parts. namespace SixLabors.ImageSharp.Formats.Gif { /// @@ -80,8 +83,6 @@ namespace SixLabors.ImageSharp.Formats.Gif // Do not use IDisposable pattern here as we want to preserve the stream. var writer = new EndianBinaryWriter(Endianness.LittleEndian, stream); - this.hasFrames = image.Frames.Count > 1; - // Quantize the image returning a palette. QuantizedFrame quantized = this.quantizer.CreateFrameQuantizer().QuantizeFrame(image.Frames.RootFrame); @@ -100,9 +101,9 @@ namespace SixLabors.ImageSharp.Formats.Gif this.WriteComments(image, writer); // Write additional frames. - if (this.hasFrames) + if (image.Frames.Count > 1) { - this.WriteApplicationExtension(writer, image.MetaData.RepeatCount, image.Frames.Count); + this.WriteApplicationExtension(writer, image.MetaData.RepeatCount); } foreach (ImageFrame frame in image.Frames) @@ -134,7 +135,6 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// The . /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] private int GetTransparentIndex(QuantizedFrame quantized) where TPixel : struct, IPixel { @@ -206,11 +206,10 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// The writer to write to the stream with. /// The animated image repeat count. - /// The number of image frames. - private void WriteApplicationExtension(EndianBinaryWriter writer, ushort repeatCount, int frames) + private void WriteApplicationExtension(EndianBinaryWriter writer, ushort repeatCount) { // Application Extension Header - if (repeatCount != 1 && frames > 0) + if (repeatCount != 1) { this.buffer[0] = GifConstants.ExtensionIntroducer; this.buffer[1] = GifConstants.ApplicationExtensionLabel; @@ -341,19 +340,14 @@ namespace SixLabors.ImageSharp.Formats.Gif var rgb = default(Rgb24); using (IManagedByteBuffer colorTable = this.memoryManager.AllocateManagedByteBuffer(colorTableLength)) { - // TODO: Pixel operations? ref TPixel paletteRef = ref MemoryMarshal.GetReference(image.Palette.AsSpan()); - ref byte colorTableRef = ref MemoryMarshal.GetReference(colorTable.Span); + ref Rgb24 rgb24Ref = ref Unsafe.As(ref MemoryMarshal.GetReference(colorTable.Span)); for (int i = 0; i < pixelCount; i++) { - int offset = i * 3; ref TPixel entry = ref Unsafe.Add(ref paletteRef, i); entry.ToRgb24(ref rgb); - - Unsafe.Add(ref colorTableRef, offset) = rgb.R; - Unsafe.Add(ref colorTableRef, offset + 1) = rgb.G; - Unsafe.Add(ref colorTableRef, offset + 2) = rgb.B; + Unsafe.Add(ref rgb24Ref, i); } writer.Write(colorTable.Array, 0, colorTableLength);