FU::Util: Add gzip_compress() wrapper for libdeflate/zlib-ng/zlib

And use it for automatic output compression in FU, as (potentially)
faster alternative to Compress::Raw::Zlib.

Was also planning to maybe add support for Zstd or Brotli, but given the
performance of libdeflate, I'm not sure that's really necessary. Brotli
does tend to do a better job at compressing HTML, though.
This commit is contained in:
Yorhel 2025-03-18 16:58:31 +01:00
parent c2e0f158ac
commit bc33fe53f0
5 changed files with 237 additions and 10 deletions

47
t/compress.t Normal file
View file

@ -0,0 +1,47 @@
use v5.36;
use Test::More;
use FU::Util qw/gzip_lib gzip_compress/;
like gzip_lib, qr/^(|libdeflate|zlib-ng|zlib)$/, gzip_lib;
plan skip_all => 'No suitable gzip library found' if !gzip_lib;
plan skip_all => 'Compress::Zlib not found' if !eval { require Compress::Zlib };
my $incompressible = Compress::Zlib::memGzip(join '', map chr(rand 256), 0..93123);
for my $str ('', 'Hello world!', 'x'x4096, $incompressible) {
is Compress::Zlib::memGunzip(gzip_compress(0, $str)), $str;
is Compress::Zlib::memGunzip(gzip_compress(12, $str)), $str;
}
done_testing;
__END__
# Test for leaks:
use Test::LeakTrace;
diag count_sv;
for (0..1000) {
for my $str ('', 'Hello world!', 'x'x4096, $incompressible) {
local $_ = gzip_lib;
$_ = gzip_compress(0, $str);
$_ = gzip_compress(12, $str);
}
}
diag count_sv;
# Compare performance:
use Benchmark 'cmpthese';
open my $F, '<', 'FU.pm';
local $/ = undef;
my $data = <$F>;
cmpthese -3, {
memGzip => 'Compress::Zlib::memGzip($data)',
gzip_compress => 'gzip_compress(6, $data)',
};