bzip2: code shrink

function                                             old     new   delta
BZ2_compressBlock                                    225     230      +5
handle_compress                                      356     355      -1
bsW16                                                 59      56      -3
bsW                                                   64      61      -3
bsFinishWrite                                         37      32      -5
prepare_new_block                                     48      34     -14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/5 up/down: 5/-26)             Total: -21 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-02-03 01:30:12 +01:00
parent 125c3ff4b1
commit e594fb2171
3 changed files with 18 additions and 18 deletions

View File

@ -55,8 +55,9 @@ void prepare_new_block(EState* s)
{ {
int i; int i;
s->nblock = 0; s->nblock = 0;
s->numZ = 0; //indexes inot s->zbits[], initialzation moved to init of s->zbits
s->state_out_pos = 0; //s->posZ = s->zbits; // was: s->numZ = 0;
//s->state_out_pos = s->zbits;
BZ_INITIALISE_CRC(s->blockCRC); BZ_INITIALISE_CRC(s->blockCRC);
/* inlined memset would be nice to have here */ /* inlined memset would be nice to have here */
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
@ -237,11 +238,10 @@ void /*Bool*/ copy_output_until_stop(EState* s)
if (s->strm->avail_out == 0) break; if (s->strm->avail_out == 0) break;
/*-- block done? --*/ /*-- block done? --*/
if (s->state_out_pos >= s->numZ) break; if (s->state_out_pos >= s->posZ) break;
/*progress_out = True;*/ /*progress_out = True;*/
*(s->strm->next_out) = s->zbits[s->state_out_pos]; *(s->strm->next_out) = *s->state_out_pos++;
s->state_out_pos++;
s->strm->avail_out--; s->strm->avail_out--;
s->strm->next_out++; s->strm->next_out++;
s->strm->total_out++; s->strm->total_out++;
@ -261,7 +261,7 @@ void /*Bool*/ handle_compress(bz_stream *strm)
while (1) { while (1) {
if (s->state == BZ_S_OUTPUT) { if (s->state == BZ_S_OUTPUT) {
/*progress_out |=*/ copy_output_until_stop(s); /*progress_out |=*/ copy_output_until_stop(s);
if (s->state_out_pos < s->numZ) break; if (s->state_out_pos < s->posZ) break;
if (s->mode == BZ_M_FINISHING if (s->mode == BZ_M_FINISHING
//# && s->avail_in_expect == 0 //# && s->avail_in_expect == 0
&& s->strm->avail_in == 0 && s->strm->avail_in == 0
@ -336,7 +336,7 @@ int BZ2_bzCompress(bz_stream *strm, int action)
/*if (s->avail_in_expect != s->strm->avail_in) /*if (s->avail_in_expect != s->strm->avail_in)
return BZ_SEQUENCE_ERROR;*/ return BZ_SEQUENCE_ERROR;*/
/*progress =*/ handle_compress(strm); /*progress =*/ handle_compress(strm);
if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ) if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
return BZ_FLUSH_OK; return BZ_FLUSH_OK;
s->mode = BZ_M_RUNNING; s->mode = BZ_M_RUNNING;
return BZ_RUN_OK; return BZ_RUN_OK;
@ -349,9 +349,9 @@ int BZ2_bzCompress(bz_stream *strm, int action)
return BZ_SEQUENCE_ERROR;*/ return BZ_SEQUENCE_ERROR;*/
/*progress =*/ handle_compress(strm); /*progress =*/ handle_compress(strm);
/*if (!progress) return BZ_SEQUENCE_ERROR;*/ /*if (!progress) return BZ_SEQUENCE_ERROR;*/
//#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ) //#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
//# return BZ_FINISH_OK; //# return BZ_FINISH_OK;
if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ) if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
return BZ_FINISH_OK; return BZ_FINISH_OK;
/*s->mode = BZ_M_IDLE;*/ /*s->mode = BZ_M_IDLE;*/
return BZ_STREAM_END; return BZ_STREAM_END;

View File

@ -150,8 +150,9 @@ typedef struct EState {
/* input and output limits and current posns */ /* input and output limits and current posns */
int32_t nblock; int32_t nblock;
int32_t nblockMAX; int32_t nblockMAX;
int32_t numZ; //int32_t numZ; // index into s->zbits[], replaced by pointer:
int32_t state_out_pos; uint8_t *posZ;
uint8_t *state_out_pos;
/* the buffer for bit stream creation */ /* the buffer for bit stream creation */
uint32_t bsBuff; uint32_t bsBuff;

View File

@ -50,8 +50,7 @@ static NOINLINE
void bsFinishWrite(EState* s) void bsFinishWrite(EState* s)
{ {
while (s->bsLive > 0) { while (s->bsLive > 0) {
s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24); *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
s->numZ++;
s->bsBuff <<= 8; s->bsBuff <<= 8;
s->bsLive -= 8; s->bsLive -= 8;
} }
@ -67,8 +66,7 @@ ALWAYS_INLINE
void bsW(EState* s, int32_t n, uint32_t v) void bsW(EState* s, int32_t n, uint32_t v)
{ {
while (s->bsLive >= 8) { while (s->bsLive >= 8) {
s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24); *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
s->numZ++;
s->bsBuff <<= 8; s->bsBuff <<= 8;
s->bsLive -= 8; s->bsLive -= 8;
} }
@ -83,8 +81,7 @@ ALWAYS_INLINE
void bsW16(EState* s, uint32_t v) void bsW16(EState* s, uint32_t v)
{ {
while (s->bsLive >= 8) { while (s->bsLive >= 8) {
s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24); *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
s->numZ++;
s->bsBuff <<= 8; s->bsBuff <<= 8;
s->bsLive -= 8; s->bsLive -= 8;
} }
@ -624,12 +621,14 @@ void BZ2_compressBlock(EState* s, int is_last_block)
s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31); s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
s->combinedCRC ^= s->blockCRC; s->combinedCRC ^= s->blockCRC;
if (s->blockNo > 1) if (s->blockNo > 1)
s->numZ = 0; s->posZ = s->zbits; // was: s->numZ = 0;
BZ2_blockSort(s); BZ2_blockSort(s);
} }
s->zbits = &((uint8_t*)s->arr2)[s->nblock]; s->zbits = &((uint8_t*)s->arr2)[s->nblock];
s->posZ = s->zbits;
s->state_out_pos = s->zbits;
/*-- If this is the first block, create the stream header. --*/ /*-- If this is the first block, create the stream header. --*/
if (s->blockNo == 1) { if (s->blockNo == 1) {