commit eaec4e73717e00874c692925bf8857efb4ead086
parent 6ed4f0c790fa295897ff53ec7ae684e84602d373
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 13 Jan 2026 15:55:21 +0100
Do not use zlib if the compression level is 0.
This level means that the data is not compressed. Using zlib then only
adds computational and storage overhead.
Diffstat:
2 files changed, 101 insertions(+), 53 deletions(-)
diff --git a/src/shtr.h b/src/shtr.h
@@ -19,9 +19,11 @@
#ifndef SHTR_H
#define SHTR_H
-#include <float.h>
#include <rsys/rsys.h>
+#include <float.h>
+#include <limits.h>
+
/* Library symbol management */
#if defined(SHTR_SHARED_BUILD) /* Build shared library */
#define SHTR_API extern EXPORT_SYM
@@ -42,7 +44,7 @@
#define SHTR_MAX_MOLECULES_COUNT 100
#define SHTR_MAX_ISOTOPES_COUNT 10
-#define SHTR_DEFAULT_COMPRESSION (~0)
+#define SHTR_DEFAULT_COMPRESSION INT_MAX
struct shtr_isotope {
double abundance; /* in ]0, 1] */
diff --git a/src/shtr_line_list.c b/src/shtr_line_list.c
@@ -90,9 +90,6 @@ zctx_init(struct zctx* zctx, struct shtr* shtr, const int level)
(zctx->shtr->allocator, NLINES_PER_CHUNK, sizeof(*zctx->lines));
if(!zctx->lines) { res = RES_MEM_ERR; goto error; }
- /* Allocate memory of compressed data */
- zctx->zlines = MEM_ALLOC(zctx->shtr->allocator, ZCHUNK_MAX_SIZE);
- if(!zctx->zlines) { res = RES_MEM_ERR; goto error; }
/* Define the zlib compression level */
if(level == SHTR_DEFAULT_COMPRESSION) {
@@ -101,13 +98,19 @@ zctx_init(struct zctx* zctx, struct shtr* shtr, const int level)
z_level = CLAMP(level, 0, 9); /* zlib compression level in [0,9] */
}
- /* Initialize zlib */
- zctx->stream.zalloc = zalloc_func;
- zctx->stream.zfree = zfree_func;
- zctx->stream.opaque = zctx->shtr->allocator;
- ret = deflateInit(&zctx->stream, z_level);
- if(ret != Z_OK) { res = RES_UNKNOWN_ERR; goto error; }
- zctx->zlib_is_init = 1;
+ if(z_level != 0) {
+ /* Allocate memory of compressed data */
+ zctx->zlines = MEM_ALLOC(zctx->shtr->allocator, ZCHUNK_MAX_SIZE);
+ if(!zctx->zlines) { res = RES_MEM_ERR; goto error; }
+
+ /* Initialize zlib */
+ zctx->stream.zalloc = zalloc_func;
+ zctx->stream.zfree = zfree_func;
+ zctx->stream.opaque = zctx->shtr->allocator;
+ ret = deflateInit(&zctx->stream, z_level);
+ if(ret != Z_OK) { res = RES_UNKNOWN_ERR; goto error; }
+ zctx->zlib_is_init = 1;
+ }
exit:
return res;
@@ -131,20 +134,25 @@ zctx_deflate(struct zctx* zctx, struct shtr_line_list* list)
if(!zctx->nlines) goto exit; /* Nothing to do */
- /* Setup input/output for zlib */
- zctx->stream.next_in = (unsigned char*)zctx->lines;
- zctx->stream.avail_in = (uInt)(zctx->nlines * sizeof(*zctx->lines));
- zctx->stream.next_out = (unsigned char*)zctx->zlines;
- zctx->stream.avail_out = ZCHUNK_MAX_SIZE;
+ if(!zctx->zlib_is_init) { /* Compression is disabled */
+ zchunk.size = (uint32_t)(zctx->nlines * sizeof(*zctx->lines));
- /* Compress */
- ret = deflate(&zctx->stream, Z_FINISH);
- if(ret != Z_STREAM_END) { res = RES_UNKNOWN_ERR; goto error; }
+ } else {
+ /* Setup input/output for zlib */
+ zctx->stream.next_in = (unsigned char*)zctx->lines;
+ zctx->stream.avail_in = (uInt)(zctx->nlines * sizeof(*zctx->lines));
+ zctx->stream.next_out = (unsigned char*)zctx->zlines;
+ zctx->stream.avail_out = ZCHUNK_MAX_SIZE;
- CHK(deflateReset(&zctx->stream) == Z_OK);
+ /* Compress */
+ ret = deflate(&zctx->stream, Z_FINISH);
+ if(ret != Z_STREAM_END) { res = RES_UNKNOWN_ERR; goto error; }
- /* Calculate the size after compression */
- zchunk.size = ZCHUNK_MAX_SIZE - zctx->stream.avail_out;
+ CHK(deflateReset(&zctx->stream) == Z_OK);
+
+ /* Calculate the size after compression */
+ zchunk.size = ZCHUNK_MAX_SIZE - zctx->stream.avail_out;
+ }
/* Calculate the total size already allocated for compressed lines */
nblocks = darray_charp_size_get(&list->blocks);
@@ -179,8 +187,13 @@ zctx_deflate(struct zctx* zctx, struct shtr_line_list* list)
res = darray_zchunk_push_back(&list->zchunks, &zchunk);
if(res != RES_OK) goto error;
- /* Save compressed chunk data */
- memcpy(block + zchunk.offset % BLOCK_SIZE, zctx->zlines, zchunk.size);
+ if(zctx->zlib_is_init) {
+ /* Save compressed chunk data */
+ memcpy(block + zchunk.offset % BLOCK_SIZE, zctx->zlines, zchunk.size);
+ } else {
+ /* Save un-compressed chunk data */
+ memcpy(block + zchunk.offset % BLOCK_SIZE, zctx->lines, zchunk.size);
+ }
/* Update the number of fully recorded lines,
* i.e., compressed and stored in the list */
@@ -212,10 +225,11 @@ check_shtr_line_list_load_args(const struct shtr_line_list_load_args* args)
}
static res_T
-create_line_list(struct shtr* shtr, struct shtr_line_list** out_list)
+create_line_list
+ (struct shtr* shtr,
+ struct shtr_line_list** out_list)
{
struct shtr_line_list* list = NULL;
- int ret = Z_OK; /* zlib */
res_T res = RES_OK;
ASSERT(shtr && out_list);
@@ -235,26 +249,37 @@ create_line_list(struct shtr* shtr, struct shtr_line_list** out_list)
res = cache_create(shtr, &list->cache);
if(res != RES_OK) goto error;
- /* Initialize zlib */
+exit:
+ *out_list = list;
+ return res;
+error:
+ if(list) {
+ SHTR(line_list_ref_put(list));
+ list = NULL;
+ }
+ goto exit;
+}
+
+static res_T
+setup_zlib(struct shtr_line_list* list)
+{
+ int ret = Z_OK; /* zlib */
+ res_T res = RES_OK;
+ ASSERT(list);
+
list->z_stream.zalloc = zalloc_func;
list->z_stream.zfree = zfree_func;
list->z_stream.opaque = list->shtr->allocator;
ret = inflateInit(&list->z_stream);
- if(ret != Z_OK) {
- res = RES_UNKNOWN_ERR;
- ERROR(shtr, "Error intializing line decompressor -- %s\n", res_to_cstr(res));
- goto error;
- }
+ if(ret != Z_OK) { res = RES_UNKNOWN_ERR; goto error; }
+
list->zlib_is_init = 1;
exit:
- *out_list = list;
return res;
error:
- if(list) {
- SHTR(line_list_ref_put(list));
- list = NULL;
- }
+ ERROR(list->shtr,
+ "Error intializing line decompressor -- %s\n", res_to_cstr(res));
goto exit;
}
@@ -517,8 +542,14 @@ load_stream
name = args->filename;
}
- res = create_line_list(shtr, &list);
+ res = create_line_list(shtr, &list);
if(res != RES_OK) goto error;
+
+ if(args->compression_level > 0) {
+ res = setup_zlib(list);
+ if(res != RES_OK) goto error;
+ }
+
res = zctx_init(&zctx, shtr, args->compression_level);
if(res != RES_OK) goto error;
@@ -530,7 +561,6 @@ load_stream
goto error;
}
-
for(;;) {
struct shtr_line ln = SHTR_LINE_NULL;
@@ -588,20 +618,26 @@ decompress_zchunk
block = darray_charp_cdata_get(&list->blocks)[block_id];
- list->z_stream.next_in = (unsigned char*)(block + block_offset);
- list->z_stream.avail_in = (uInt)zchunk->size;
- list->z_stream.next_out = (unsigned char*)lines;
- list->z_stream.avail_out = (uInt)(sizeof(struct line)*NLINES_PER_CHUNK);
- ret = inflate(&list->z_stream, Z_FINISH);
- if(ret != Z_STREAM_END) {
- ASSERT(list->z_stream.msg);
- ERROR(list->shtr, "Error decompressing the chunk of lines -- %s\n",
- list->z_stream.msg);
- res = RES_UNKNOWN_ERR;
- goto error;
- }
+ if(!list->zlib_is_init) {
+ /* Data are not compressed */
+ memcpy(lines, block+block_offset, zchunk->size);
- CHK(inflateReset(&list->z_stream) == Z_OK);
+ } else {
+ list->z_stream.next_in = (unsigned char*)(block + block_offset);
+ list->z_stream.avail_in = (uInt)zchunk->size;
+ list->z_stream.next_out = (unsigned char*)lines;
+ list->z_stream.avail_out = (uInt)(sizeof(struct line)*NLINES_PER_CHUNK);
+ ret = inflate(&list->z_stream, Z_FINISH);
+ if(ret != Z_STREAM_END) {
+ ASSERT(list->z_stream.msg);
+ ERROR(list->shtr, "Error decompressing the chunk of lines -- %s\n",
+ list->z_stream.msg);
+ res = RES_UNKNOWN_ERR;
+ goto error;
+ }
+
+ CHK(inflateReset(&list->z_stream) == Z_OK);
+ }
exit:
return res;
@@ -681,6 +717,7 @@ shtr_line_list_create_from_stream
struct shtr_line_list* list = NULL;
char** blocks = NULL;
size_t i=0, n=0;
+ int is_compression_enabled = 0;
int version = 0;
res_T res = RES_OK;
@@ -718,6 +755,7 @@ shtr_line_list_create_from_stream
}
READ(&list->nlines, 1);
+ READ(&is_compression_enabled, 1);
/* Memory descriptor of compressed chunks */
READ(&n, 1);
@@ -739,6 +777,11 @@ shtr_line_list_create_from_stream
#undef READ
+ if(is_compression_enabled) {
+ res = setup_zlib(list);
+ if(res != RES_OK) goto error;
+ }
+
exit:
if(out_list) *out_list = list;
return res;
@@ -831,6 +874,9 @@ shtr_line_list_write
/* Number of lines in the list */
WRITE(&list->nlines, 1);
+ /* Is decompression enabled */
+ WRITE(&list->zlib_is_init, 1);
+
/* Memory descriptor of compressed chunks */
n = darray_zchunk_size_get(&list->zchunks);
WRITE(&n, 1);