From ef756e3ae8e1a5ba8263b58a11dda4f1f42ba1db Mon Sep 17 00:00:00 2001 From: Kevin Oliva Date: Thu, 27 Jun 2024 20:59:17 -0500 Subject: [PATCH] performance optimization --- Heroes.MpqTool/BZip2/BZip2.cs | 12 ------------ Heroes.MpqTool/BZip2/BZip2InputStream.cs | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/Heroes.MpqTool/BZip2/BZip2.cs b/Heroes.MpqTool/BZip2/BZip2.cs index 53219e6..cf43414 100644 --- a/Heroes.MpqTool/BZip2/BZip2.cs +++ b/Heroes.MpqTool/BZip2/BZip2.cs @@ -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(int d1, int d2) - { - var x = new T[d1][]; - for (int i = 0; i < d1; i++) - { - x[i] = new T[d2]; - } - - return x; - } } \ No newline at end of file diff --git a/Heroes.MpqTool/BZip2/BZip2InputStream.cs b/Heroes.MpqTool/BZip2/BZip2InputStream.cs index 54b0114..645875d 100644 --- a/Heroes.MpqTool/BZip2/BZip2InputStream.cs +++ b/Heroes.MpqTool/BZip2/BZip2InputStream.cs @@ -384,7 +384,7 @@ protected override void Dispose(bool disposing) } } - private static void HbCreateDecodeTables(int[] limit, List bbase, List perm, char[] length, int minLen, int maxLen, int alphaSize) + private static void HbCreateDecodeTables(int[] limit, List bbase, List perm, List length, int minLen, int maxLen, int alphaSize) { for (int i = minLen; i <= maxLen; i++) { @@ -651,13 +651,17 @@ 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()); + + List len_t = s.TempCharArray2dList[t]; + int curr = GetBits(5); - char[] len_t = len[t]; + for (int i = 0; i < alphaSize; i++) { while (BsGetBit()) @@ -665,7 +669,7 @@ private void RecvDecodingTables() curr += BsGetBit() ? -1 : 1; } - len_t[i] = (char)curr; + len_t.Add((char)curr); } } @@ -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(); @@ -726,7 +729,9 @@ private void CreateHuffmanDecodingTables(int alphaSize, int nGroups) { int minLen = 32; int maxLen = 0; - char[] len_t = len[t]; + + List len_t = s.TempCharArray2dList[t]; + for (int i = alphaSize; --i >= 0;) { char lent = len_t[i]; @@ -742,7 +747,7 @@ private void CreateHuffmanDecodingTables(int alphaSize, int nGroups) s.GBaseList.Add(new List()); s.GPermList.Add(new List()); - 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; } } @@ -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(BZip2.NGroups, BZip2.MaxAlphaSize); + TempCharArray2dList = new(); RecvDecodingTables_pos = new byte[BZip2.NGroups]; // 6 byte Ll8List = new(); @@ -1273,7 +1278,7 @@ public DecompressionState(int blockSize100k) public byte[] GetAndMoveToFrontDecode_yy { get; } - public char[][] TempCharArray2d { get; } + public List> TempCharArray2dList { get; } public byte[] RecvDecodingTables_pos { get; }