bzip2: move ->origPtr out of struct EState, make a few members smaller
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 3 Feb 2018 19:50:20 +0000 (20:50 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 3 Feb 2018 19:51:12 +0000 (20:51 +0100)
function                                             old     new   delta
BZ2_compressBlock                                    223     228      +5
BZ2_blockSort                                         85      88      +3
generateMTFValues                                    356     357      +1
handle_compress                                      355     349      -6
compressStream                                       538     531      -7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/2 up/down: 9/-13)              Total: -4 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
archival/libarchive/bz/blocksort.c
archival/libarchive/bz/bzlib_private.h
archival/libarchive/bz/compress.c

index effaa152a152e6c6801b91e7f581e9f18b4b1a7e..7c5b6c552116bf29c41d7df220295db7231a3dbc 100644 (file)
@@ -1022,16 +1022,15 @@ void mainSort(EState* state)
  *     arr1[0 .. nblock-1] holds sorted order
  */
 static NOINLINE
-void BZ2_blockSort(EState* state)
+int32_t BZ2_blockSort(EState* state)
 {
        /* In original bzip2 1.0.4, it's a parameter, but 30
         * (which was the default) should work ok. */
        enum { wfact = 30 };
        unsigned i;
+       int32_t origPtr = origPtr;
 
-       if (state->nblock < 10000) {
-               fallbackSort(state);
-       } else {
+       if (state->nblock >= 10000) {
                /* Calculate the location for quadrant, remembering to get
                 * the alignment right.  Assumes that &(block[0]) is at least
                 * 2-byte aligned -- this should be ok since block is really
@@ -1050,24 +1049,25 @@ void BZ2_blockSort(EState* state)
                 * of whether or not we use the main sort or fallback sort.
                 */
                state->budget = state->nblock * ((wfact-1) / 3);
-
                mainSort(state);
-               if (state->budget < 0) {
-                       fallbackSort(state);
-               }
+               if (state->budget >= 0)
+                       goto good;
        }
+       fallbackSort(state);
+ good:
 
 #if BZ_LIGHT_DEBUG
-       state->origPtr = -1;
+       origPtr = -1;
 #endif
        for (i = 0; i < state->nblock; i++) {
                if (state->ptr[i] == 0) {
-                       state->origPtr = i;
+                       origPtr = i;
                        break;
                }
        }
 
-       AssertH(state->origPtr != -1, 1003);
+       AssertH(origPtr != -1, 1003);
+       return origPtr;
 }
 
 
index fc05d0ebe36eade40e5f8153b856b3807a8b965f..8b8bbe3eb55a654e44e8965144bd79a42600a356 100644 (file)
@@ -120,9 +120,11 @@ typedef struct EState {
 
        /* mode this stream is in, and whether inputting */
        /* or outputting data */
-       int32_t  mode;
-//both smallint?
-       int32_t  state;
+       uint8_t mode;
+       uint8_t state;
+
+       /* misc administratium */
+       uint8_t blockSize100k;
 
        /* remembers avail_in when flush/finish requested */
 /* bbox: not needed, strm->avail_in always has the same value */
@@ -130,12 +132,11 @@ typedef struct EState {
        /* uint32_t avail_in_expect; */
 
        /* for doing the block sorting */
-       int32_t  origPtr;
        uint32_t *arr1;
        uint32_t *arr2;
        uint32_t *ftab;
 
-       uint16_tquadrant;
+       uint16_t *quadrant;
        int32_t  budget;
 
        /* aliases for arr1 and arr2 */
@@ -144,10 +145,6 @@ typedef struct EState {
        uint16_t *mtfv;
        uint8_t  *zbits;
 
-       /* guess what */
-       uint32_t *crc32table;
-//move down
-
        /* run-length-encoding of the input */
        uint32_t state_in_ch;
        int32_t  state_in_len;
@@ -156,21 +153,22 @@ typedef struct EState {
        int32_t  nblock;
        int32_t  nblockMAX;
        //int32_t  numZ; // index into s->zbits[], replaced by pointer:
-       uint8_t *posZ;
-       uint8_t *state_out_pos;
+       uint8_t  *posZ;
+       uint8_t  *state_out_pos;
 
        /* the buffer for bit stream creation */
        uint32_t bsBuff;
        int32_t  bsLive;
 
+       /* guess what */
+       uint32_t *crc32table;
+
        /* block and combined CRCs */
        uint32_t blockCRC;
        uint32_t combinedCRC;
 
        /* misc administratium */
        int32_t  blockNo;
-       int32_t  blockSize100k;
-//smallint?
 
        /* stuff for coding the MTF values */
        int32_t  nMTF;
@@ -206,7 +204,7 @@ typedef struct EState {
 
 /*-- compression. --*/
 
-static void
+static int32_t
 BZ2_blockSort(EState*);
 
 static void
index f1393242d654da84ea347f57e86d9cf3ffc2c8d2..f6507675869b4fc3e04c73f669507c4ce46a6b52 100644 (file)
@@ -665,6 +665,8 @@ void sendMTFValues(EState* s)
 static
 void BZ2_compressBlock(EState* s, int is_last_block)
 {
+       int32_t origPtr = origPtr;
+
        if (s->nblock > 0) {
                BZ_FINALISE_CRC(s->blockCRC);
                s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
@@ -672,7 +674,7 @@ void BZ2_compressBlock(EState* s, int is_last_block)
                if (s->blockNo > 1)
                        s->posZ = s->zbits; // was: s->numZ = 0;
 
-               BZ2_blockSort(s);
+               origPtr = BZ2_blockSort(s);
        }
 
        s->zbits = &((uint8_t*)s->arr2)[s->nblock];
@@ -713,7 +715,7 @@ void BZ2_compressBlock(EState* s, int is_last_block)
                 */
                bsW1_0(s);
 
-               bsW(s, 24, s->origPtr);
+               bsW(s, 24, origPtr);
                generateMTFValues(s);
                sendMTFValues(s);
        }