Subject: | Compress::Zlib::uncompress() is slowed down needlessly by parameter validation of arguments stored in Compress::Zlib |
Compress::Zlib uses some very inefficient parameter validation.
When using Compress::Zlib::uncompress() to uncompress 40k small
compressed items I found that 75% of the time was taken up by the
parameter validation.
Since uncompress() does not offer any parameters, the validation is
purely that of the defaults stored internally.
Added to this, it seems that Compress::Zlib does not generate true
constants, which are optimized by Perl, instead each Z_OK for instance
incurs a performance penalty.
I replaced a call to uncompress with the following:
my ($inflater, $err_code, $data)= Compress::Raw::Zlib::_inflateInit(
ZLIB_INFLATE_FLAGS, ZLIB_INFLATE_MAX_WBITS,
ZLIB_INFLATE_BUFFER_SIZE, ZLIB_INFLATE_DICTIONARY);
die "Failed to create inflator: $err_code" if $err_code != _Z_OK;
$err_code= $inflater->inflate(\$_[0],$data);
if ( $err_code != _Z_STREAM_END ) {
TELL "Failed to decompress event packet Zlib error code: %s",
$err_code
if DEBUG;
return undef;
}
All of the ZLIB constants are true constants I create from the non-true
constants generated by Compress::Zlib. The result of using this code is
a significatn performance improvement. On my test set of 40k items it
went from 4 second to 1 second.
I think unrolling Compress::Zlib::compress() to bypass parameter
validation (which is not even needed really) would make big performance
boost to a lot of peoples code.
Thanks,
yves (demerphq)