bzip2: code shrink, stop using global data variable

function                                             old     new   delta
compressStream                                       523     538     +15
level                                                  1       -      -1
bzip2_main                                           110      73     -37
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/1 up/down: 15/-38)            Total: -23 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-02-01 01:00:58 +01:00
parent 54a174eb01
commit 1b8ac93a20

View File

@ -83,15 +83,13 @@
/* No point in being shy and having very small buffer here.
* bzip2 internal buffers are much bigger anyway, hundreds of kbytes.
* If iobuf is several pages long, malloc() may use mmap,
* making iobuf is page aligned and thus (maybe) have one memcpy less
* making iobuf page aligned and thus (maybe) have one memcpy less
* if kernel is clever enough.
*/
enum {
IOBUF_SIZE = 8 * 1024
};
static uint8_t level;
/* NB: compressStream() has to return -1 on errors, not die.
* bbunpack() will correctly clean up in this case
* (delete incomplete .bz2 file)
@ -143,6 +141,7 @@ static
IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate UNUSED_PARAM)
{
IF_DESKTOP(long long) int total;
unsigned opt, level;
ssize_t count;
bz_stream bzs; /* it's small */
#define strm (&bzs)
@ -151,6 +150,16 @@ IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate U
#define wbuf (iobuf + IOBUF_SIZE)
iobuf = xmalloc(2 * IOBUF_SIZE);
opt = option_mask32 >> (sizeof("cfkvq" IF_FEATURE_BZIP2_DECOMPRESS("dt") "zs") - 1);
opt |= 0x100; /* if nothing else, assume -9 */
level = 0;
for (;;) {
level++;
if (opt & 1) break;
opt >>= 1;
}
BZ2_bzCompressInit(strm, level);
while (1) {
@ -197,25 +206,18 @@ int bzip2_main(int argc UNUSED_PARAM, char **argv)
opt = getopt32(argv, "^"
/* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
"cfkv" IF_FEATURE_BZIP2_DECOMPRESS("dt") "123456789qzs"
"cfkvq" IF_FEATURE_BZIP2_DECOMPRESS("dt") "zs123456789"
"\0" "s2" /* -s means -2 (compatibility) */
);
#if ENABLE_FEATURE_BZIP2_DECOMPRESS /* bunzip2_main may not be visible... */
if (opt & 0x30) // -d and/or -t
if (opt & (3 << 5)) /* -d and/or -t */
return bunzip2_main(argc, argv);
opt >>= 6;
#else
opt >>= 4;
/* clear "decompress" and "test" bits (or bbunpack() can get confused) */
/* in !BZIP2_DECOMPRESS config, these bits are -zs and are unused */
option_mask32 = opt & ~(3 << 5);
#endif
opt = (uint8_t)opt; /* isolate bits for -1..-8 */
opt |= 0x100; /* if nothing else, assume -9 */
level = 1;
while (!(opt & 1)) {
level++;
opt >>= 1;
}
argv += optind;
option_mask32 &= 0xf; /* ignore all except -cfkv */
return bbunpack(argv, compressStream, append_ext, "bz2");
}