summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-07 12:31:51 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-07 12:31:51 +0700
commit86c42dc16533f74e2f82fd5e800f3b9b56ab7c64 (patch)
tree6ba7d0a348c04599ee0ef98007993b2d8f2bddd8
parentFurther bzip2 improvements (diff)
downloadBouncyCastle.NET-ed25519-86c42dc16533f74e2f82fd5e800f3b9b56ab7c64.tar.xz
bzip2 perf. opts.
-rw-r--r--crypto/bzip2/src/CBZip2InputStream.cs12
-rw-r--r--crypto/bzip2/src/CBZip2OutputStream.cs39
2 files changed, 21 insertions, 30 deletions
diff --git a/crypto/bzip2/src/CBZip2InputStream.cs b/crypto/bzip2/src/CBZip2InputStream.cs
index 111b1b530..c3125a4e7 100644
--- a/crypto/bzip2/src/CBZip2InputStream.cs
+++ b/crypto/bzip2/src/CBZip2InputStream.cs
@@ -465,7 +465,6 @@ namespace Org.BouncyCastle.Apache.Bzip2
 
         private void GetAndMoveToFrontDecode()
         {
-            byte[] yy = new byte[256];
             int i, j, nextSym;
 
             int limitLast = BZip2Constants.baseBlockSize * blockSize100k;
@@ -487,9 +486,10 @@ namespace Org.BouncyCastle.Apache.Bzip2
             */
             Array.Clear(unzftab, 0, unzftab.Length);
 
-            for (i = 0; i <= 255; i++)
+            byte[] yy = new byte[nInUse];
+            for (i = 0; i < nInUse; ++i)
             {
-                yy[i] = (byte)i;
+                yy[i] = seqToUnseq[i];
             }
 
             last = -1;
@@ -567,7 +567,7 @@ namespace Org.BouncyCastle.Apache.Bzip2
                     //while (nextSym == BZip2Constants.RUNA || nextSym == BZip2Constants.RUNB);
                     while (nextSym <= BZip2Constants.RUNB);
 
-                    byte ch = seqToUnseq[yy[0]];
+                    byte ch = yy[0];
                     unzftab[ch] += s;
 
                     if (last >= limitLast - s)
@@ -586,8 +586,8 @@ namespace Org.BouncyCastle.Apache.Bzip2
                         throw new InvalidOperationException("Block overrun");
 
                     byte tmp = yy[nextSym - 1];
-                    unzftab[seqToUnseq[tmp]]++;
-                    ll8[last] = seqToUnseq[tmp];
+                    unzftab[tmp]++;
+                    ll8[last] = tmp;
 
                     /*
                      * This loop is hammered during decompression, hence avoid
diff --git a/crypto/bzip2/src/CBZip2OutputStream.cs b/crypto/bzip2/src/CBZip2OutputStream.cs
index ee3eeaed0..262a52f84 100644
--- a/crypto/bzip2/src/CBZip2OutputStream.cs
+++ b/crypto/bzip2/src/CBZip2OutputStream.cs
@@ -78,6 +78,13 @@ namespace Org.BouncyCastle.Apache.Bzip2
             780, 773, 635, 389, 707, 100, 626, 958, 165, 504, 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, 645, 990,
             626, 197, 510, 357, 358, 850, 858, 364, 936, 638 };
 
+        /*
+         * Knuth's increments seem to work better than Incerpi-Sedgewick here, possibly because the number of elements
+         * to sort is usually small, typically <= 20.
+         */
+        private static readonly int[] Incs = { 1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161,
+            2391484 };
+
         private bool finished;
 
         protected static void HbMakeCodeLengths(byte[] len, int[] freq, int alphaSize, int maxLen)
@@ -247,7 +254,7 @@ namespace Org.BouncyCastle.Apache.Bzip2
         private readonly int allowableBlockSize;
 
         bool blockRandomised;
-        IList blocksortStack = Platform.CreateArrayList();
+        private readonly IList blocksortStack = Platform.CreateArrayList();
 
         int bsBuff;
         int bsLivePos;
@@ -256,8 +263,6 @@ namespace Org.BouncyCastle.Apache.Bzip2
         private bool[] inUse = new bool[256];
         private int nInUse;
 
-        private byte[] unseqToSeq = new byte[256];
-
         private byte[] m_selectors = new byte[BZip2Constants.MAX_SELECTORS];
 
         private byte[] blockBytes;
@@ -952,7 +957,7 @@ namespace Org.BouncyCastle.Apache.Bzip2
                 return;
 
             int hp = 0;
-            while (incs[hp] < bigN)
+            while (Incs[hp] < bigN)
             {
                 hp++;
             }
@@ -960,7 +965,7 @@ namespace Org.BouncyCastle.Apache.Bzip2
 
             for (; hp >= 0; hp--)
             {
-                h = incs[hp];
+                h = Incs[hp];
 
                 i = lo + h;
                 while (i <= hi)
@@ -1518,27 +1523,18 @@ namespace Org.BouncyCastle.Apache.Bzip2
             return false;
         }
 
-        /*
-        Knuth's increments seem to work better
-        than Incerpi-Sedgewick here.  Possibly
-        because the number of elems to sort is
-        usually small, typically <= 20.
-        */
-        private static readonly int[] incs = { 1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161,
-            2391484 };
-
         private void GenerateMtfValues()
         {
-            byte[] yy = new byte[256];
             int i;
 
             nInUse = 0;
 
+            byte[] yy = new byte[256];
             for (i = 0; i < 256; i++)
             {
                 if (inUse[i])
                 {
-                    unseqToSeq[i] = (byte)nInUse++;
+                    yy[nInUse++] = (byte)i;
                 }
             }
 
@@ -1549,18 +1545,13 @@ namespace Org.BouncyCastle.Apache.Bzip2
                 mtfFreq[i] = 0;
             }
 
-            for (i = 0; i < nInUse; i++)
-            {
-                yy[i] = (byte)i;
-            }
-
             int j, wr = 0, zPend = 0;
             for (i = 0; i < count; i++)
             {
-                byte ll_i = unseqToSeq[blockBytes[zptr[i]]];
+                byte blockByte = blockBytes[zptr[i]];
 
                 byte tmp = yy[0];
-                if (ll_i == tmp)
+                if (blockByte == tmp)
                 {
                     zPend++;
                     continue;
@@ -1573,7 +1564,7 @@ namespace Org.BouncyCastle.Apache.Bzip2
                     tmp = yy[sym];
                     yy[sym++] = tmp2;
                 }
-                while (ll_i != tmp);
+                while (blockByte != tmp);
                 yy[0] = tmp;
 
                 while (zPend > 0)