From: q66 Date: Mon, 17 Jun 2024 23:22:06 +0000 (+0200) Subject: libkmod: improve realloc behavior for zstd outbuffer X-Git-Tag: v33~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8da7c1e0881c57db634c25669d1f1af87d4c2aaf;p=thirdparty%2Fkmod.git 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 --- diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c index 8baf12d6..52490fbd 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;