]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: improve realloc behavior for zstd outbuffer
authorq66 <q66@chimera-linux.org>
Mon, 17 Jun 2024 23:22:06 +0000 (01:22 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 12 Jul 2024 12:58:13 +0000 (07:58 -0500)
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 <q66@chimera-linux.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-file.c

index 8baf12d6d62a4729e853865a3fa03dc969477f75..52490fbd3c7908fb020f97510c245ee308e7e645 100644 (file)
@@ -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;