Bump to dfbbf7f.

readstdin: reduce memory-usage by duplicating the line from getline()

Improves upon commit 32db2b125190d366be472ccb7cad833248696144

The getline() implementation often uses a more greedy way of allocating memory.
Using this buffer directly and forcing an allocation (by setting it to NULL)
would waste a bit of extra space, depending on the implementation of course.

Tested on musl libc and glibc.
The current glibc version allocates a minimum of 120 bytes per line.
For smaller lines musl libc seems less wasteful but still wastes a few bytes
per line.

On a dmenu_path listing on my system the memory usage was about 350kb (old) vs
30kb (new) on Void Linux glibc.

Side-note that getline() also reads NUL bytes in lines, while strdup() would
read until the NUL byte. Since dmenu reads text lines either is probably
fine(tm). Also rename junk to linesiz.

Ref.
https://git.suckless.org/dmenu/commit/dfbbf7f6e1b22ccf9e5a45d77ee10995577fb4fc.html
This commit is contained in:
bakkeby 2023-04-07 14:17:25 +02:00
parent 1279886b9c
commit a725f9f6b9

View File

@ -1428,7 +1428,7 @@ readstdin(void)
char *buf, *p;
#endif // SEPARATOR_PATCH | TSV_PATCH
size_t i, junk, itemsiz = 0;
size_t i, linesiz, itemsiz = 0;
ssize_t len;
#if PASSWORD_PATCH
@ -1439,7 +1439,7 @@ readstdin(void)
#endif // PASSWORD_PATCH
/* read each line from stdin and add it to the item list */
for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++) {
for (i = 0; (len = getline(&line, &linesiz, stdin)) != -1; i++) {
if (i + 1 >= itemsiz) {
itemsiz += 256;
if (!(items = realloc(items, itemsiz * sizeof(*items))))
@ -1448,7 +1448,8 @@ readstdin(void)
if (line[len - 1] == '\n')
line[len - 1] = '\0';
items[i].text = line;
if (!(items[i].text = strdup(line)))
die("strdup:");
#if SEPARATOR_PATCH
if (separator && (p = separator_greedy ?
strrchr(items[i].text, separator) : strchr(items[i].text, separator))) {
@ -1483,7 +1484,6 @@ readstdin(void)
#if HIGHPRIORITY_PATCH
items[i].hp = arrayhas(hpitems, hplength, items[i].text);
#endif // HIGHPRIORITY_PATCH
line = NULL;
}
free(line);
if (items)