Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize InflaterHuffmanTree #844

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private IEnumerable<bool> CreateStateMachine()
while (!input.TryGetBits(3, ref codeLengths, MetaCodeLengthIndex[i])) yield return false;
}

var metaCodeTree = new InflaterHuffmanTree(codeLengths);
var metaCodeTree = new InflaterHuffmanTree(codeLengths.AsSpan());

// Decompress the meta tree symbols into the data table code lengths
int index = 0;
Expand Down Expand Up @@ -113,8 +113,8 @@ private IEnumerable<bool> CreateStateMachine()
if (codeLengths[256] == 0)
throw new StreamDecodingException("Inflater dynamic header end-of-block code missing");

litLenTree = new InflaterHuffmanTree(new ArraySegment<byte>(codeLengths, 0, litLenCodeCount));
distTree = new InflaterHuffmanTree(new ArraySegment<byte>(codeLengths, litLenCodeCount, distanceCodeCount));
litLenTree = new InflaterHuffmanTree(codeLengths.AsSpan(0, litLenCodeCount));
distTree = new InflaterHuffmanTree(codeLengths.AsSpan(litLenCodeCount, distanceCodeCount));

yield return true;
}
Expand Down
87 changes: 42 additions & 45 deletions src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace ICSharpCode.SharpZipLib.Zip.Compression
{
Expand All @@ -13,6 +15,11 @@ public class InflaterHuffmanTree

private const int MAX_BITLEN = 15;

// see InflaterHuffmanTreeTest.GenerateTrees how to generate the sequence
// stored in DLL's static data section so no allocation occurs
private static ReadOnlySpan<byte> defLitLenTreeBytes => new byte[] { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8 };
private static ReadOnlySpan<byte> defDistTreeBytes => new byte[] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

#endregion Constants

#region Instance Fields
Expand All @@ -24,74 +31,62 @@ public class InflaterHuffmanTree
/// <summary>
/// Literal length tree
/// </summary>
public static InflaterHuffmanTree defLitLenTree;
public static readonly InflaterHuffmanTree defLitLenTree = new InflaterHuffmanTree(defLitLenTreeBytes);

/// <summary>
/// Distance tree
/// </summary>
public static InflaterHuffmanTree defDistTree;
public static readonly InflaterHuffmanTree defDistTree = new InflaterHuffmanTree(defDistTreeBytes);

#region Constructors

static InflaterHuffmanTree()
/// <summary>
/// Constructs a Huffman tree from the array of code lengths.
/// </summary>
/// <param name = "codeLengths">
/// the array of code lengths
/// </param>
public InflaterHuffmanTree(IList<byte> codeLengths) : this(ListToSpan(codeLengths))
{
try
{
byte[] codeLengths = new byte[288];
int i = 0;
while (i < 144)
{
codeLengths[i++] = 8;
}
while (i < 256)
{
codeLengths[i++] = 9;
}
while (i < 280)
{
codeLengths[i++] = 7;
}
while (i < 288)
{
codeLengths[i++] = 8;
}
defLitLenTree = new InflaterHuffmanTree(codeLengths);
}

codeLengths = new byte[32];
i = 0;
while (i < 32)
{
codeLengths[i++] = 5;
}
defDistTree = new InflaterHuffmanTree(codeLengths);
private static ReadOnlySpan<byte> ListToSpan(IList<byte> codeLengths)
{
#if NET6_0_OR_GREATER
if (codeLengths is List<byte> list)
{
return CollectionsMarshal.AsSpan(list);
}
catch (Exception)
#endif
if (codeLengths is byte[] array)
{
throw new SharpZipBaseException("InflaterHuffmanTree: static tree length illegal");
return array;
}
}

#region Constructors
// slow path
return codeLengths.ToArray();
}

/// <summary>
/// Constructs a Huffman tree from the array of code lengths.
/// </summary>
/// <param name = "codeLengths">
/// the array of code lengths
/// </param>
public InflaterHuffmanTree(IList<byte> codeLengths)
internal InflaterHuffmanTree(ReadOnlySpan<byte> codeLengths)
{
BuildTree(codeLengths);
}

#endregion Constructors

private void BuildTree(IList<byte> codeLengths)
private void BuildTree(ReadOnlySpan<byte> codeLengths)
{
int[] blCount = new int[MAX_BITLEN + 1];
int[] nextCode = new int[MAX_BITLEN + 1];
Span<int> blCount = stackalloc int[MAX_BITLEN + 1];
Span<int> nextCode = stackalloc int[MAX_BITLEN + 1];

for (int i = 0; i < codeLengths.Count; i++)
foreach (var bits in codeLengths)
{
int bits = codeLengths[i];
if (bits > 0)
{
blCount[bits]++;
Expand Down Expand Up @@ -136,20 +131,21 @@ private void BuildTree(IList<byte> codeLengths)
}
}

for (int i = 0; i < codeLengths.Count; i++)
for (var i = 0; i < codeLengths.Length; i++)
{
int bits = codeLengths[i];
var bits = codeLengths[i];
if (bits == 0)
{
continue;
}

code = nextCode[bits];
int revcode = DeflaterHuffman.BitReverse(code);
if (bits <= 9)
{
do
{
tree[revcode] = (short)((i << 4) | bits);
tree[revcode] = (short) ((i << 4) | bits);
revcode += 1 << bits;
} while (revcode < 512);
}
Expand All @@ -160,10 +156,11 @@ private void BuildTree(IList<byte> codeLengths)
subTree = -(subTree >> 4);
do
{
tree[subTree | (revcode >> 9)] = (short)((i << 4) | bits);
tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
revcode += 1 << bits;
} while (revcode < treeLen);
}

nextCode[bits] = code + (1 << (16 - bits));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
{
Expand Down Expand Up @@ -36,12 +37,18 @@ public void Write(int value)
{
if (windowFilled++ == WindowSize)
{
throw new InvalidOperationException("Window full");
ThrowWindowFull();
}
window[windowEnd++] = (byte)value;
windowEnd &= WindowMask;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowWindowFull()
{
throw new InvalidOperationException("Window full");
}

private void SlowRepeat(int repStart, int length, int distance)
{
while (length-- > 0)
Expand Down
51 changes: 51 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/InflaterHuffmanTreeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using NUnit.Framework;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
public class InflaterHuffmanTreeTest
{
/// <summary>
/// Generates code based on optimization described in https://github.com/dotnet/csharplang/issues/5295#issue-1028421234
/// </summary>
[Test]
[Explicit]
public void GenerateTrees()
{
// generates the byte arrays needed by InflaterHuffmanTree
var defLitLenTreeBytes = new byte[288];
int i = 0;
while (i < 144)
{
defLitLenTreeBytes[i++] = 8;
}

while (i < 256)
{
defLitLenTreeBytes[i++] = 9;
}

while (i < 280)
{
defLitLenTreeBytes[i++] = 7;
}

while (i < 288)
{
defLitLenTreeBytes[i++] = 8;
}

Console.WriteLine($"private static ReadOnlySpan<byte> defLitLenTreeBytes => new byte[] {{ { string.Join(", ", defLitLenTreeBytes) } }};");


var defDistTreeBytes = new byte[32];
i = 0;
while (i < 32)
{
defDistTreeBytes[i++] = 5;
}

Console.WriteLine($"private static ReadOnlySpan<byte> defDistTreeBytes => new byte[] {{ { string.Join(", ", defDistTreeBytes) } }};");
}
}
}
Loading