pgrep: optimize command name copying
strncpy() implementations in glibc and musl use memset() to zero the entire destination string past the source string length. This is hot code path that runs per PID. It makes strncpy() take more cycles than anything else.
Use memccpy() instead since it never touches the destination past the string terminator.
Signed-off-by: Baruch Siach baruch.siach@siklu.com
Merge request reports
Activity
mentioned in commit 98ca9fe3
mentioned in commit 204194cd
Nice, this looks simpler than my
stzncpy
proposal !118@tt.rantala does this and !118 need work still as Jim's commits were for top only?
Let me rephrase it, I believe now this patch should be used instead of !118?
Edited by Craig SmallI'm currently using pgrep from procps v3.3.16 and encountering extreme slowness as described in !118 compared to my previous use of v3.2.8. Is my understanding correct that this patch provides significant improvement by not zeroing 2 MBytes of memory for each process? And that the behaviour before was much better because until v3.3.15 it only zeroed 4 KBytes for each process?
I believe this shows that the
_SC_ARG_MAX
value is 2 MBytes on my system.# xargs --show-limits </dev/null 2>&1 | grep "Maximum length" Maximum length of command we could actually use: 2094500
Is there a reason that this patch should not be applied?