]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shared: improve strbuf_reserve_extra performance
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 28 Feb 2025 16:27:12 +0000 (17:27 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 7 Mar 2025 04:57:37 +0000 (22:57 -0600)
If strbuf is used (depmod, modprobe -c) then strbuf_reserve_extra is
performance critical. This reduces amount of instructions for
modprobe -c by around 10 %, the total instruction count for depmod
by 1 % (majority is within reading module files).

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
shared/strbuf.c

index 172558a6275658f79f706900e48a45d60351dff1..ef99eb782bbb6b19d6e0c6780ef3b73f8f9c8b4e 100644 (file)
@@ -36,12 +36,12 @@ static bool buf_realloc(struct strbuf *buf, size_t sz)
 
 static bool strbuf_reserve_extra(struct strbuf *buf, size_t n)
 {
+       if (n < buf->size - buf->used)
+               return true;
+
        if (uaddsz_overflow(buf->used, n, &n) || n >= SIZE_MAX - BUF_STEP)
                return false;
 
-       if (n < buf->size)
-               return true;
-
        if (++n % BUF_STEP)
                n = ((n / BUF_STEP) + 1) * BUF_STEP;