ZlibCompressor: use libdeflate for level 0 compression

this is supported since libdeflate 1.15 and ext-libdeflate 0.2.0.
Everyone should be using these versions by now anyway, and if they aren't, they should update.

libdeflate's level 0 compression is over 20 times faster than zlib, so this is a nice performance improvement.
This commit is contained in:
Dylan K. Taylor
2023-11-09 18:05:07 +00:00
parent c1ed182112
commit e3700cab50
2 changed files with 12 additions and 10 deletions

View File

@ -67,17 +67,12 @@ final class ZlibCompressor implements Compressor{
return $result;
}
private static function zlib_encode(string $data, int $level) : string{
return Utils::assumeNotFalse(zlib_encode($data, ZLIB_ENCODING_RAW, $level), "ZLIB compression failed");
}
public function compress(string $payload) : string{
$compressible = $this->minCompressionSize !== null && strlen($payload) >= $this->minCompressionSize;
if(function_exists('libdeflate_deflate_compress')){
return $compressible ?
libdeflate_deflate_compress($payload, $this->level) :
self::zlib_encode($payload, 0);
}
return self::zlib_encode($payload, $compressible ? $this->level : 0);
$level = $compressible ? $this->level : 0;
return function_exists('libdeflate_deflate_compress') ?
libdeflate_deflate_compress($payload, $level) :
Utils::assumeNotFalse(zlib_encode($payload, ZLIB_ENCODING_RAW, $level), "ZLIB compression failed");
}
}