From: Tobias Stoeckmann Date: Fri, 28 Feb 2025 16:27:12 +0000 (+0100) Subject: shared: improve strbuf_reserve_extra performance X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4f1546bd388ca4c97b23af5c8e50a567aeec155;p=thirdparty%2Fkmod.git shared: improve strbuf_reserve_extra performance 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 Link: https://github.com/kmod-project/kmod/pull/296 Signed-off-by: Lucas De Marchi --- diff --git a/shared/strbuf.c b/shared/strbuf.c index 172558a6..ef99eb78 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -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;