Skip to content

Commit 9007665

Browse files
q66yuwata
authored andcommitted
strbuf: use GREEDY_REALLOC to grow the buffer
This allows us to reserve a bunch of capacity ahead of time, improving the performance of hwdb significantly thanks to not having to reallocate so many times. Before: ``` $ sudo time valgrind --leak-check=full ./systemd-hwdb update ==113297== Memcheck, a memory error detector ==113297== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==113297== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==113297== Command: ./systemd-hwdb update ==113297== ==113297== ==113297== HEAP SUMMARY: ==113297== in use at exit: 0 bytes in 0 blocks ==113297== total heap usage: 1,412,640 allocs, 1,412,640 frees, 117,920,009,195 bytes allocated ==113297== ==113297== All heap blocks were freed -- no leaks are possible ==113297== ==113297== For lists of detected and suppressed errors, rerun with: -s ==113297== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 132.44user 21.15system 2:35.61elapsed 98%CPU (0avgtext+0avgdata 228560maxresident)k 0inputs+25296outputs (0major+6886930minor)pagefaults 0swaps ``` After: ``` $ sudo time valgrind --leak-check=full ./systemd-hwdb update ==112572== Memcheck, a memory error detector ==112572== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==112572== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==112572== Command: ./systemd-hwdb update ==112572== ==112572== ==112572== HEAP SUMMARY: ==112572== in use at exit: 0 bytes in 0 blocks ==112572== total heap usage: 1,320,113 allocs, 1,320,113 frees, 70,614,501 bytes allocated ==112572== ==112572== All heap blocks were freed -- no leaks are possible ==112572== ==112572== For lists of detected and suppressed errors, rerun with: -s ==112572== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 21.94user 0.19system 0:22.23elapsed 99%CPU (0avgtext+0avgdata 229876maxresident)k 0inputs+25264outputs (0major+57275minor)pagefaults 0swaps ``` Co-authored-by: Yu Watanabe <[email protected]> (cherry picked from commit 621b10f) (cherry picked from commit 514ef0f) (cherry picked from commit aa0dd89) (cherry picked from commit 43ee651) (cherry picked from commit 732b645)
1 parent d5d07bd commit 9007665

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

src/basic/strbuf.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ static void bubbleinsert(struct strbuf_node *node,
107107
/* add string, return the index/offset into the buffer */
108108
ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
109109
uint8_t c;
110-
char *buf_new;
111110
struct strbuf_child_entry *child;
112111
struct strbuf_node *node;
113112
ssize_t off;
@@ -147,10 +146,8 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
147146
}
148147

149148
/* add new string */
150-
buf_new = realloc(str->buf, str->len + len+1);
151-
if (!buf_new)
149+
if (!GREEDY_REALLOC(str->buf, str->len + len + 1))
152150
return -ENOMEM;
153-
str->buf = buf_new;
154151
off = str->len;
155152
memcpy(str->buf + off, s, len);
156153
str->len += len;

0 commit comments

Comments
 (0)