Skip to content

Commit 0e7e01d

Browse files
committed
Update to latest upstream Adler32 code.
Matched the existing code by inlining SeedValue instead of parameterising the adler seed.
1 parent 108d186 commit 0e7e01d

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/MySqlConnector/Protocol/Serialization/CompressedPayloadHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
168168
return ValueTaskExtensions.FromException<int>(new InvalidOperationException("Expected to read {0:n0} uncompressed bytes but only read {1:n0}".FormatInvariant(uncompressedLength, totalBytesRead)));
169169
m_remainingData = new(uncompressedData, 0, totalBytesRead);
170170

171-
var checksum = Adler32.Calculate(uncompressedData, 0, (uint) totalBytesRead);
171+
var checksum = Adler32.Calculate(uncompressedData.AsSpan(0, totalBytesRead));
172172

173173
var adlerStartOffset = payloadReadBytes.Offset + payloadReadBytes.Count - 4;
174174
if (payloadReadBytes.Array[adlerStartOffset + 0] != ((checksum >> 24) & 0xFF) ||
@@ -218,7 +218,7 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
218218
using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel.Optimal, leaveOpen: true))
219219
deflateStream.Write(remainingUncompressedData.Array!, remainingUncompressedData.Offset, remainingUncompressedBytes);
220220

221-
var checksum = Adler32.Calculate(remainingUncompressedData.Array!, (uint)remainingUncompressedData.Offset, (uint)remainingUncompressedBytes);
221+
var checksum = Adler32.Calculate(remainingUncompressedData.AsSpan(0, remainingUncompressedBytes));
222222
compressedStream.WriteByte((byte) ((checksum >> 24) & 0xFF));
223223
compressedStream.WriteByte((byte) ((checksum >> 16) & 0xFF));
224224
compressedStream.WriteByte((byte) ((checksum >> 8) & 0xFF));

src/MySqlConnector/Utilities/Adler32.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
3-
// https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/Formats/Png/Zlib/Adler32.cs
3+
// https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/Compression/Zlib/Adler32.cs
44

55
#if !NET6_0_OR_GREATER
6+
using System.Runtime.CompilerServices;
67
#if NETCOREAPP3_0_OR_GREATER
78
using System.Runtime.Intrinsics;
89
using System.Runtime.Intrinsics.X86;
@@ -44,36 +45,37 @@ internal static class Adler32
4445
/// Calculates the Adler32 checksum with the bytes taken from the span.
4546
/// </summary>
4647
/// <param name="buffer">The readonly span of bytes.</param>
47-
/// <param name="offset">The offset.</param>
48-
/// <param name="length">The length.</param>
4948
/// <returns>The <see cref="uint"/>.</returns>
50-
public static uint Calculate(ReadOnlySpan<byte> buffer, uint offset, uint length)
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public static uint Calculate(ReadOnlySpan<byte> buffer)
5151
{
52-
if (buffer.Length == 0)
52+
if (buffer.IsEmpty)
5353
{
5454
return SeedValue;
5555
}
5656

5757
#if NETCOREAPP3_0_OR_GREATER
5858
if (Ssse3.IsSupported && buffer.Length >= MinBufferSize)
5959
{
60-
return CalculateSse(buffer, offset, length);
60+
return CalculateSse(buffer);
6161
}
6262
#endif
6363

64-
return CalculateScalar(buffer, offset, length);
64+
return CalculateScalar(buffer);
6565
}
6666

6767
#if NETCOREAPP3_0_OR_GREATER
6868
// Based on https://github.com/chromium/chromium/blob/master/third_party/zlib/adler32_simd.c
69-
private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset, uint length)
69+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70+
private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer)
7071
{
7172
uint s1 = SeedValue & 0xFFFF;
7273
uint s2 = (SeedValue >> 16) & 0xFFFF;
7374

7475
// Process the data in blocks.
7576
const int BLOCK_SIZE = 1 << 5;
7677

78+
uint length = (uint)buffer.Length;
7779
uint blocks = length / BLOCK_SIZE;
7880
length -= blocks * BLOCK_SIZE;
7981

@@ -82,7 +84,7 @@ private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset,
8284
fixed (byte* tapPtr = Tap1Tap2)
8385
{
8486
index += (int)blocks * BLOCK_SIZE;
85-
var localBufferPtr = bufferPtr + offset;
87+
var localBufferPtr = bufferPtr;
8688

8789
// _mm_setr_epi8 on x86
8890
Vector128<sbyte> tap1 = Sse2.LoadVector128((sbyte*)tapPtr);
@@ -192,15 +194,17 @@ private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset,
192194
}
193195
#endif
194196

195-
private static unsafe uint CalculateScalar(ReadOnlySpan<byte> buffer, uint offset, uint length)
197+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
198+
private static unsafe uint CalculateScalar(ReadOnlySpan<byte> buffer)
196199
{
197200
uint s1 = SeedValue & 0xFFFF;
198201
uint s2 = (SeedValue >> 16) & 0xFFFF;
199202
uint k;
200203

201204
fixed (byte* bufferPtr = buffer)
202205
{
203-
var localBufferPtr = bufferPtr + offset;
206+
var localBufferPtr = bufferPtr;
207+
uint length = (uint) buffer.Length;
204208

205209
while (length > 0)
206210
{

0 commit comments

Comments
 (0)