Skip to content

Commit

Permalink
performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
koliva8245 committed Jun 28, 2024
1 parent 24dc5eb commit ef756e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
12 changes: 0 additions & 12 deletions Heroes.MpqTool/BZip2/BZip2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,4 @@ internal static class BZip2
public static readonly char RUNB = (char)1;
public static readonly int NGroups = 6;
public static readonly int GSize = 50;
public static readonly int MaxSelectors = 2 + (900000 / GSize);

internal static T[][] InitRectangularArray<T>(int d1, int d2)
{
var x = new T[d1][];
for (int i = 0; i < d1; i++)
{
x[i] = new T[d2];
}

return x;
}
}
23 changes: 14 additions & 9 deletions Heroes.MpqTool/BZip2/BZip2InputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected override void Dispose(bool disposing)
}
}

private static void HbCreateDecodeTables(int[] limit, List<int> bbase, List<int> perm, char[] length, int minLen, int maxLen, int alphaSize)
private static void HbCreateDecodeTables(int[] limit, List<int> bbase, List<int> perm, List<char> length, int minLen, int maxLen, int alphaSize)
{
for (int i = minLen; i <= maxLen; i++)
{
Expand Down Expand Up @@ -651,21 +651,25 @@ private void RecvDecodingTables()

SelectorMtf(s, pos, nGroups, nSelectors);

char[][] len = s.TempCharArray2d;
s.TempCharArray2dList.Clear();

/* Now the coding tables */
for (int t = 0; t < nGroups; t++)
{
s.TempCharArray2dList.Add(new List<char>());

List<char> len_t = s.TempCharArray2dList[t];

int curr = GetBits(5);
char[] len_t = len[t];

for (int i = 0; i < alphaSize; i++)
{
while (BsGetBit())
{
curr += BsGetBit() ? -1 : 1;
}

len_t[i] = (char)curr;
len_t.Add((char)curr);
}
}

Expand Down Expand Up @@ -716,7 +720,6 @@ private void SelectorMtf(DecompressionState s, byte[] pos, int nGroups, int nSel
private void CreateHuffmanDecodingTables(int alphaSize, int nGroups)
{
DecompressionState s = _data!;
char[][] len = s.TempCharArray2d;

s.GLimitList.Clear();
s.GBaseList.Clear();
Expand All @@ -726,7 +729,9 @@ private void CreateHuffmanDecodingTables(int alphaSize, int nGroups)
{
int minLen = 32;
int maxLen = 0;
char[] len_t = len[t];

List<char> len_t = s.TempCharArray2dList[t];

for (int i = alphaSize; --i >= 0;)
{
char lent = len_t[i];
Expand All @@ -742,7 +747,7 @@ private void CreateHuffmanDecodingTables(int alphaSize, int nGroups)
s.GBaseList.Add(new List<int>());
s.GPermList.Add(new List<int>());

HbCreateDecodeTables(s.GLimitList[t], s.GBaseList[t], s.GPermList[t], len[t], minLen, maxLen, alphaSize);
HbCreateDecodeTables(s.GLimitList[t], s.GBaseList[t], s.GPermList[t], s.TempCharArray2dList[t], minLen, maxLen, alphaSize);
s.GMinlen[t] = minLen;
}
}
Expand Down Expand Up @@ -1247,7 +1252,7 @@ public DecompressionState(int blockSize100k)
GMinlen = new int[BZip2.NGroups]; // 24 byte

GetAndMoveToFrontDecode_yy = new byte[256]; // 512 byte
TempCharArray2d = BZip2.InitRectangularArray<char>(BZip2.NGroups, BZip2.MaxAlphaSize);
TempCharArray2dList = new();
RecvDecodingTables_pos = new byte[BZip2.NGroups]; // 6 byte

Ll8List = new();
Expand All @@ -1273,7 +1278,7 @@ public DecompressionState(int blockSize100k)

public byte[] GetAndMoveToFrontDecode_yy { get; }

public char[][] TempCharArray2d { get; }
public List<List<char>> TempCharArray2dList { get; }

public byte[] RecvDecodingTables_pos { get; }

Expand Down

0 comments on commit ef756e3

Please sign in to comment.