Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/ICSharpCode.SharpZipLib/Checksum/Adler32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,31 @@ public void Update(byte[] buffer)
/// </param>
public void Update(ArraySegment<byte> segment)
{
foreach (byte b in segment)
//(By Per Bothner)
uint s1 = checkValue & 0xFFFF;
uint s2 = checkValue >> 16;
var count = segment.Count;
var offset = segment.Offset;
while (count > 0)
{
Update(b);
// We can defer the modulo operation:
// s1 maximally grows from 65521 to 65521 + 255 * 3800
// s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
int n = 3800;
if (n > count)
{
n = count;
}
count -= n;
while (--n >= 0)
{
s1 = s1 + (uint)(segment.Array[offset++] & 0xff);
s2 = s2 + s1;
}
s1 %= BASE;
s2 %= BASE;
}
checkValue = (s2 << 16) | s1;
}
}
}
28 changes: 28 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Checksum/ChecksumTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ICSharpCode.SharpZipLib.Checksum;
using NUnit.Framework;
using System;
using System.Diagnostics;

namespace ICSharpCode.SharpZipLib.Tests.Checksum
{
Expand All @@ -27,6 +28,33 @@ public void Adler_32()
exceptionTesting(underTestAdler32);
}

const long BufferSize = 256 * 1024 * 1024;

[Test]
public void Adler_32_Performance()
{
var rand = new Random(1);

var buffer = new byte[BufferSize];
rand.NextBytes(buffer);

var adler = new Adler32();
Assert.AreEqual(0x00000001, adler.Value);

var sw = new Stopwatch();
sw.Start();

adler.Update(buffer);

sw.Stop();
Console.WriteLine($"Adler32 Hashing of 256 MiB: {sw.Elapsed.TotalSeconds:f4} second(s)");

adler.Update(check);
Assert.AreEqual(0xD4897DA3, adler.Value);

exceptionTesting(adler);
}

[Test]
public void CRC_32_BZip2()
{
Expand Down