From a40c67d2faec95b3ec9d8768f978f536354061d0 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sun, 5 Jun 2005 18:05:07 +0000 Subject: [PATCH] MFH: Fixed bug #33070 (Improved performance of bzdecompress() by several orders of magnitude). --- NEWS | 2 ++ ext/bz2/bz2.c | 80 ++++++++++++++++++++++----------------------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/NEWS b/NEWS index a22ef11bb2..56345ee8e7 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,8 @@ PHP NEWS and may cause segfault). (Tony) - Fixed bug #33072 (Add a safemode/open_basedir check for runtime "session.save_path" change using session_save_path() function). (Rasmus) +- Fixed bug #33070 (Improved performance of bzdecompress() by several orders + of magnitude). (Ilia) - Fixed bug #33059 (crash when moving xml attribute set in dtd). (Ilia) - Fixed bug #33057 (Don't send extraneous entity-headers on a 304 as per RFC 2616 section 10.3.5) (Rasmus, Choitel) diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index e5e0de51e0..6c8b39f4c8 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -40,9 +40,6 @@ #define PHP_BZ_ERRSTR 1 #define PHP_BZ_ERRBOTH 2 -/* Blocksize of the decompression buffer */ -#define PHP_BZ_DECOMPRESS_SIZE 4096 - function_entry bz2_functions[] = { PHP_FE(bzopen, NULL) PHP_FE(bzread, NULL) @@ -438,56 +435,49 @@ PHP_FUNCTION(bzcompress) Decompresses BZip2 compressed data */ PHP_FUNCTION(bzdecompress) { - zval **source, /* Source data to decompress */ - **zsmall; /* (Optional) user specified small */ - char *dest; /* Destination buffer, initially allocated */ - int error, /* Error container */ - iter = 1, /* Iteration count for the compression loop */ - size, /* Current size to realloc the dest buffer to */ - dest_len = PHP_BZ_DECOMPRESS_SIZE, /* Size of the destination length */ - small = 0, /* The actual small */ - argc = ZEND_NUM_ARGS(); /* Argument count */ - - if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &source, &zsmall) == FAILURE) { - WRONG_PARAM_COUNT; + char *source, *dest; + int source_len, error; + long small = 0; + unsigned int size = 0; + bz_stream bzs; + + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &source, &source_len, &small)) { + RETURN_FALSE; } - convert_to_string_ex(source); - - /* optional small argument handling */ - if (argc > 1) { - convert_to_long_ex(zsmall); - small = Z_LVAL_PP(zsmall); + bzs.bzalloc = NULL; + bzs.bzfree = NULL; + + if (BZ2_bzDecompressInit(&bzs, 0, small) != BZ_OK) { + RETURN_FALSE; } - /* Depending on the size of the source buffer, either allocate - the length of the source buffer or the a default decompression - size */ - dest = emalloc(PHP_BZ_DECOMPRESS_SIZE > Z_STRLEN_PP(source) ? PHP_BZ_DECOMPRESS_SIZE : Z_STRLEN_PP(source)); - - /* (de)Compression Loop */ - do { - /* Handle the (re)allocation of the buffer */ - size = dest_len * iter; - if (iter > 1) { - dest = erealloc(dest, size); - } - ++iter; + bzs.next_in = source; + bzs.avail_in = source_len; - /* Perform the decompression */ - error = BZ2_bzBuffToBuffDecompress(dest, &size, Z_STRVAL_PP(source), Z_STRLEN_PP(source), small, 0); - } while (error == BZ_OUTBUFF_FULL); + /* in most cases bz2 offers at least 2:1 compression, so we use that as our base */ + bzs.avail_out = source_len * 2; + bzs.next_out = dest = emalloc(bzs.avail_out + 1); - if (error != BZ_OK) { - efree(dest); - RETURN_LONG(error); - } else { - /* we might have allocated a little to much, so erealloc the buffer - down to size, before returning it */ + while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) { + /* compression is better then 2:1, need to allocate more memory */ + bzs.avail_out = source_len; + size = (bzs.total_out_hi32 << 32) + bzs.total_out_lo32; + dest = erealloc(dest, size + bzs.avail_out + 1); + bzs.next_out = dest + size; + } + + if (error == BZ_STREAM_END || error == BZ_OK) { + size = (bzs.total_out_hi32 << 32) + bzs.total_out_lo32; dest = erealloc(dest, size + 1); - dest[size] = 0; - RETURN_STRINGL(dest, size, 0); + dest[size] = '\0'; + RETVAL_STRINGL(dest, size, 0); + } else { /* real error */ + efree(dest); + RETVAL_LONG(error); } + + BZ2_bzDecompressEnd(&bzs); } /* }}} */ -- 2.40.0