Skip to content

Commit

Permalink
Fixing LZ4 compression levels (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
aloneguid authored Aug 3, 2023
1 parent 6011ab2 commit 4728653
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
43 changes: 28 additions & 15 deletions native/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ using namespace std;

bool lzo_initialised{ false };

// taken from https://github.com/lz4/lz4/blob/839edadd09bd653b7e9237a2fbf405d9e8bfc14e/lib/lz4.c#L48C1-L58C35
/*
* LZ4_ACCELERATION_DEFAULT :
* Select "acceleration" for LZ4_compress_fast() when parameter value <= 0
*/
#define LZ4_ACCELERATION_DEFAULT 1
/*
* LZ4_ACCELERATION_MAX :
* Any "acceleration" value higher than this threshold
* get treated as LZ4_ACCELERATION_MAX instead (fix #876)
*/
#define LZ4_ACCELERATION_MAX 65537


bool compress_snappy(
bool compress,
char* input_buffer,
Expand Down Expand Up @@ -52,7 +66,7 @@ bool compress_zstd(
int input_buffer_size,
char* output_buffer,
int* output_buffer_size,
int compression_level) {
compression_level compression_level) {
if(output_buffer == nullptr) {
if(compress) {
*output_buffer_size = ZSTD_compressBound(input_buffer_size);
Expand All @@ -64,9 +78,9 @@ bool compress_zstd(

if(compress) {
auto level = ZSTD_btultra2;
if(compression_level == 1){
if(compression_level == compression_level::fastest){
level = ZSTD_fast;
} else if(compression_level == 2){
} else if(compression_level == compression_level::balanced){
level = ZSTD_btopt;
}

Expand All @@ -84,7 +98,7 @@ bool compress_brotli(
int input_buffer_size,
char* output_buffer,
int* output_buffer_size,
int compression_level) {
compression_level compression_level) {

if(output_buffer == nullptr) {

Expand All @@ -98,9 +112,9 @@ bool compress_brotli(

if(compress) {
auto level = BROTLI_MAX_QUALITY;
if(compression_level == 1){
if(compression_level == compression_level::fastest){
level = BROTLI_MIN_QUALITY;
} else if(compression_level == 2){
} else if(compression_level == compression_level::balanced){
level = BROTLI_MAX_QUALITY/2;
}
size_t compressed_size = *output_buffer_size;
Expand Down Expand Up @@ -197,7 +211,7 @@ bool compress_lz4(
int input_buffer_size,
char* output_buffer,
int* output_buffer_size,
int compression_level) {
compression_level compression_level) {
if(output_buffer == nullptr) {
if(compress) {
*output_buffer_size = LZ4_compressBound(input_buffer_size);
Expand All @@ -209,14 +223,13 @@ bool compress_lz4(
}

if(compress) {
auto level = 65537;
if(compression_level == 1){
level = 1;
} else if(compression_level == 2){
level = 32768;
}
int acceleration = LZ4_ACCELERATION_DEFAULT;
if(compression_level == compression_level::fastest)
acceleration = LZ4_ACCELERATION_MAX;
else if(compression_level == compression_level::balanced)
acceleration = LZ4_ACCELERATION_MAX / 2;

*output_buffer_size = LZ4_compress_fast(input_buffer, output_buffer, input_buffer_size, *output_buffer_size, level);
*output_buffer_size = LZ4_compress_fast(input_buffer, output_buffer, input_buffer_size, *output_buffer_size, acceleration);
return *output_buffer_size != 0;
} else {
*output_buffer_size = LZ4_decompress_safe(input_buffer, output_buffer, input_buffer_size, *output_buffer_size);
Expand All @@ -227,7 +240,7 @@ bool compress_lz4(
}

bool compress(bool compress, int32_t codec, char* input_buffer, int32_t input_buffer_size,
char* output_buffer, int32_t* output_buffer_size, int32_t compression_level) {
char* output_buffer, int32_t* output_buffer_size, compression_level compression_level) {
switch(codec) {
case 1:
return compress_snappy(compress, input_buffer, input_buffer_size, output_buffer, output_buffer_size);
Expand Down
8 changes: 7 additions & 1 deletion native/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
# define EXPORTED
#endif

enum class compression_level : int32_t {
fastest = 1,
balanced = 2,
best = 3
};

extern "C"
{
/**
Expand All @@ -32,7 +38,7 @@ extern "C"
int32_t input_buffer_size,
char* output_buffer,
int32_t* output_buffer_size,
int32_t compression_level);
compression_level compression_level);

/**
* @brief Used to just ping the library to test it's available at all
Expand Down
8 changes: 4 additions & 4 deletions native/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ using namespace std;

bool run(int method, char* buffer, size_t buffer_length) {
int32_t len{0};
bool ok = compress(true, method, buffer, buffer_length, nullptr, &len, 2);
bool ok = compress(true, method, buffer, buffer_length, nullptr, &len, compression_level::best);

vector<char> compressed;
compressed.resize(len);

ok = compress(true, method, buffer, buffer_length,
&compressed[0], &len, 2);
&compressed[0], &len, compression_level::best);

vector<byte> decompressed;
int32_t bl1 = buffer_length;
decompressed.resize(buffer_length);
ok = compress(false, method, &compressed[0], len,
(char*)&decompressed[0], &bl1, 2);
(char*)&decompressed[0], &bl1, compression_level::best);

return ok;
}
Expand All @@ -27,7 +27,7 @@ int main() {

//run(1, bytes, 3);
//run(2, bytes, 3);
run(4, bytes, 3);
run(6, bytes, 3);

return 0;
}

0 comments on commit 4728653

Please sign in to comment.