[PATCH v2] misc: Optimize getusershell.c

Rocket Ma marocketbd@gmail.com
Thu Apr 23 03:47:20 GMT 2026


> Ah, thanks to you and Rocket for pointing out my mistake. I think I was
> mixing up the one in glibc and the one I wrote for Gnulib to work around
> some issues. Sorry for the extra noise.
>
> The one in Gnulib uses getline, mostly because it was easier. I guess
> the downside to that is that you might read a very long line which would
> be impossible to execute as a shell, instead of limiting things to
> PATH_MAX. It looks like OpenBSD decided to go in that direction as well
> [1].
>
> Collin
>
> [1] https://github.com/openbsd/src/blob/7a2e62a4900cc0c671c264dfa2679f73987d45c6/lib/libc/gen/getusershell.c#L103

This patch aims at reducing memory footprint, since it only allocates
one big buffer, then frees it. The old implementation allocates a
file-size buffer to store string and (file size / 3 * 8) to store
pointers. The openbsd implementation you pointed out uses getline +
strdup with a dynamic array to allocate strings and string pointers,
leading to multiple malloc calls. For glibc, only allocate one big
buffer could help malloc to trim or split it later should improve
performance, rather than leave a lot of free chunks that may never be
used in bins.

Rocket


More information about the Libc-alpha mailing list