Skip to content

Commit

Permalink
Use static buffer for rdbcompression
Browse files Browse the repository at this point in the history
Lazily allocate 8KiB buffer to use for compression. This buffer should
be large enough for most cases. When a larger buffer is required,
rdbSaveLzfStringObject falls back to the ad hoc allocation.

This change goes together with the further change that introduces
freeing string objects during BGSAVE. It greatly decreases the number
of COW events. Otherwise compression allocations reuses the memory
freed by string objects. Which leads to COW.

Signed-off-by: Vadym Khoptynets <vadymkh@amazon.com>
  • Loading branch information
poiuj committed Aug 12, 2024
1 parent 5166d48 commit 14a835d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/rdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,18 +388,24 @@ ssize_t rdbSaveLzfBlob(rio *rdb, void *data, size_t compress_len, size_t origina
ssize_t rdbSaveLzfStringObject(rio *rdb, unsigned char *s, size_t len) {
size_t comprlen, outlen;
void *out;
static void *buffer = NULL;

/* We require at least four bytes compression for this to be worth it */
if (len <= 4) return 0;
outlen = len - 4;
if ((out = zmalloc(outlen + 1)) == NULL) return 0;
if (outlen < LZF_STATIC_BUFFER_SIZE) {
if (!buffer) buffer = zmalloc(LZF_STATIC_BUFFER_SIZE);
out = buffer;
} else {
if ((out = zmalloc(outlen + 1)) == NULL) return 0;
}
comprlen = lzf_compress(s, len, out, outlen);
if (comprlen == 0) {
zfree(out);
if (outlen >= LZF_STATIC_BUFFER_SIZE) zfree(out);
return 0;
}
ssize_t nwritten = rdbSaveLzfBlob(rdb, out, comprlen, len);
zfree(out);
if (outlen >= LZF_STATIC_BUFFER_SIZE) zfree(out);
return nwritten;
}

Expand Down
3 changes: 3 additions & 0 deletions src/rdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
#define RDB_LOAD_ERR_EMPTY_KEY 1 /* Error of empty key */
#define RDB_LOAD_ERR_OTHER 2 /* Any other errors */

/* Size of the static buffer used for rdbcompression */
#define LZF_STATIC_BUFFER_SIZE (8 * 1024)

ssize_t rdbWriteRaw(rio *rdb, void *p, size_t len);
int rdbSaveType(rio *rdb, unsigned char type);
int rdbLoadType(rio *rdb);
Expand Down

0 comments on commit 14a835d

Please sign in to comment.