Skip to content

Commit

Permalink
Re-add the check for small buffers and a new test (see Blosc/python-b…
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Aug 21, 2022
1 parent 3c63098 commit 55e9b06
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions blosc/blosc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2220,6 +2220,10 @@ static int write_compression_header(blosc2_context* context, bool extended_heade
/* Compression level 0 means buffer to be memcpy'ed */
context->header_flags |= (uint8_t)BLOSC_MEMCPYED;
}
if (context->sourcesize < BLOSC_MIN_BUFFERSIZE) {
/* Buffer is too small. Try memcpy'ing. */
context->header_flags |= (uint8_t)BLOSC_MEMCPYED;
}

bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED;
if (extended_header) {
Expand Down
4 changes: 2 additions & 2 deletions include/blosc2.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ enum {
//!< Maximum typesize before considering source buffer as a stream of bytes.
//!< Cannot be larger than 255.
#endif // BLOSC_H
BLOSC_MIN_BUFFERSIZE = 128,
//!< Minimum buffer size to be compressed. Cannot be smaller than 66.
BLOSC_MIN_BUFFERSIZE = 32,
//!< Minimum buffer size to be compressed.
};


Expand Down
35 changes: 35 additions & 0 deletions tests/test_compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,40 @@ static char *test_small_blocksize(void) {
}


/* Check small buffer */
static char *test_small_buffer(void) {
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
cparams.typesize = 1;
blosc2_context *cctx = blosc2_create_cctx(cparams);
blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
blosc2_context *dctx = blosc2_create_dctx(dparams);
size = 2;
uint8_t *src2 = calloc(size, 1);
for (int i = 0; i < size; i++) {
src2[i] = (uint8_t)i;
}

/* Using contexts */
cbytes = blosc2_compress_ctx(cctx, src2, size, dest, size + BLOSC2_MAX_OVERHEAD);
nbytes = blosc2_decompress_ctx(dctx, dest, size + BLOSC2_MAX_OVERHEAD, src, size);
mu_assert("ERROR: nbytes is not correct", nbytes == size);

/* Not using contexts */
cbytes = blosc2_compress(9, 1, cparams.typesize, src2, size, dest, size + BLOSC2_MAX_OVERHEAD);
nbytes = blosc2_decompress(dest, size + BLOSC2_MAX_OVERHEAD, src, size);
mu_assert("ERROR: nbytes is not correct", nbytes == size);

/* Using Blosc1 interface */
cbytes = blosc1_compress(9, 1, cparams.typesize, size, src2, dest, size + BLOSC2_MAX_OVERHEAD);
nbytes = blosc1_decompress(dest, src, size);
mu_assert("ERROR: nbytes is not correct", nbytes == size);

free(src2);
blosc2_free_ctx(cctx);
blosc2_free_ctx(dctx);
return 0;
}


static char *all_tests(void) {
mu_run_test(test_compressor);
Expand All @@ -248,6 +282,7 @@ static char *all_tests(void) {
mu_run_test(test_delta);
mu_run_test(test_typesize);
mu_run_test(test_small_blocksize);
mu_run_test(test_small_buffer);

return 0;
}
Expand Down

0 comments on commit 55e9b06

Please sign in to comment.