From 8770cc71e9882414046f5f11b4e57a303381327f Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 22 Mar 2018 17:52:46 -0700 Subject: [PATCH] Update Checksum algorithms to use Spans --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 2 +- src/ImageSharp/Formats/Png/Zlib/Adler32.cs | 25 ++++--------------- src/ImageSharp/Formats/Png/Zlib/Crc32.cs | 23 +++-------------- src/ImageSharp/Formats/Png/Zlib/IChecksum.cs | 22 ++++------------ .../Formats/Png/Zlib/ZlibDeflateStream.cs | 2 +- 5 files changed, 15 insertions(+), 59 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 26a8b48df3..c3750eb09f 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -677,7 +677,7 @@ namespace SixLabors.ImageSharp.Formats.Png if (data != null && length > 0) { - this.crc.Update(data, offset, length); + this.crc.Update(new ReadOnlySpan(data, offset, length)); } WriteInteger(stream, (uint)this.crc.Value); diff --git a/src/ImageSharp/Formats/Png/Zlib/Adler32.cs b/src/ImageSharp/Formats/Png/Zlib/Adler32.cs index 1cce90c0b7..9c4e9e4b99 100644 --- a/src/ImageSharp/Formats/Png/Zlib/Adler32.cs +++ b/src/ImageSharp/Formats/Png/Zlib/Adler32.cs @@ -113,30 +113,15 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Update(byte[] buffer) + public void Update(ReadOnlySpan data) { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } - - this.Update(buffer, 0, buffer.Length); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Update(byte[] buffer, int offset, int count) - { - DebugGuard.NotNull(buffer, nameof(buffer)); - DebugGuard.MustBeGreaterThanOrEqualTo(offset, 0, nameof(offset)); - DebugGuard.MustBeGreaterThanOrEqualTo(count, 0, nameof(count)); - DebugGuard.MustBeLessThan(offset, buffer.Length, nameof(offset)); - DebugGuard.MustBeLessThanOrEqualTo(offset + count, buffer.Length, nameof(count)); - // (By Per Bothner) uint s1 = this.checksum & 0xFFFF; uint s2 = this.checksum >> 16; + int count = data.Length; + int offset = 0; + while (count > 0) { // We can defer the modulo operation: @@ -151,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib count -= n; while (--n >= 0) { - s1 = s1 + (uint)(buffer[offset++] & 0xff); + s1 = s1 + (uint)(data[offset++] & 0xff); s2 = s2 + s1; } diff --git a/src/ImageSharp/Formats/Png/Zlib/Crc32.cs b/src/ImageSharp/Formats/Png/Zlib/Crc32.cs index bd686f2b9f..d1588c384f 100644 --- a/src/ImageSharp/Formats/Png/Zlib/Crc32.cs +++ b/src/ImageSharp/Formats/Png/Zlib/Crc32.cs @@ -137,30 +137,13 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Update(byte[] buffer) + public void Update(ReadOnlySpan data) { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } - - this.Update(buffer, 0, buffer.Length); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Update(byte[] buffer, int offset, int count) - { - DebugGuard.NotNull(buffer, nameof(buffer)); - DebugGuard.MustBeGreaterThanOrEqualTo(count, 0, nameof(count)); - DebugGuard.MustBeGreaterThanOrEqualTo(offset, 0, nameof(offset)); - DebugGuard.MustBeLessThanOrEqualTo(offset + count, buffer.Length, nameof(count)); - this.crc ^= CrcSeed; - while (--count >= 0) + for (int i = 0; i < data.Length; i++) { - this.crc = CrcTable[(this.crc ^ buffer[offset++]) & 0xFF] ^ (this.crc >> 8); + this.crc = CrcTable[(this.crc ^ data[i]) & 0xFF] ^ (this.crc >> 8); } this.crc ^= CrcSeed; diff --git a/src/ImageSharp/Formats/Png/Zlib/IChecksum.cs b/src/ImageSharp/Formats/Png/Zlib/IChecksum.cs index 9d84258cae..a2a57332b1 100644 --- a/src/ImageSharp/Formats/Png/Zlib/IChecksum.cs +++ b/src/ImageSharp/Formats/Png/Zlib/IChecksum.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; + namespace SixLabors.ImageSharp.Formats.Png.Zlib { /// @@ -34,25 +36,11 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib void Update(int value); /// - /// Updates the data checksum with the bytes taken from the array. + /// Updates the data checksum with the bytes taken from the span. /// - /// + /// /// buffer an array of bytes /// - void Update(byte[] buffer); - - /// - /// Adds the byte array to the data checksum. - /// - /// - /// The buffer which contains the data - /// - /// - /// The offset in the buffer where the data starts - /// - /// - /// the number of data bytes to add. - /// - void Update(byte[] buffer, int offset, int count); + void Update(ReadOnlySpan data); } } diff --git a/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs b/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs index dd20886ff7..51e6b4859e 100644 --- a/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs +++ b/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs @@ -163,7 +163,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib public override void Write(byte[] buffer, int offset, int count) { this.deflateStream.Write(buffer, offset, count); - this.adler32.Update(buffer, offset, count); + this.adler32.Update(new ReadOnlySpan(buffer, offset, count)); } ///