From 8da7c1e0881c57db634c25669d1f1af87d4c2aaf Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 18 Jun 2024 01:22:06 +0200 Subject: [PATCH] libkmod: improve realloc behavior for zstd outbuffer The allocator in glibc has a particular quirk that successive reallocs on the same pointer are cheap, at the cost of excess memory fragmentation. Other allocators generally do not do this, so excessive reallocs become relatively expensive. Reducing the number of reallocations by using a more agressive strategy for buffer size increase makes performance better on those setups, e.g. musl libc, or generally any other allocator; on my Chimera Linux setup with Scudo allocator (LLVM) it doubles to triples the performance of running e.g. depmod. Signed-off-by: q66 Signed-off-by: Lucas De Marchi --- libkmod/libkmod-file.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c index 8baf12d..52490fb 100644 --- a/libkmod/libkmod-file.c +++ b/libkmod/libkmod-file.c @@ -89,7 +89,11 @@ static int zstd_ensure_outbuffer_space(ZSTD_outBuffer *buffer, size_t min_free) if (buffer->size - buffer->pos >= min_free) return 0; - buffer->size += min_free; + if (buffer->size < min_free) + buffer->size = min_free; + else + buffer->size *= 2; + buffer->dst = realloc(buffer->dst, buffer->size); if (buffer->dst == NULL) { ret = -errno;