On 31/10/2025 10:21, Bruno Haible wrote:
Collin Funk wrote:
Can't we change the call to posix_spawnp to posix_spawn to prevent
scanning $PATH twice?

This is not necessary, because in most cases find_in_given_path returns
a filename that contains a slash (see gnulib/lib/findprog.h) and in
this case posix_spawnp() does not even consider PATH [1]:
   "If the file parameter contains a <slash> character, the file parameter
    shall be used as the pathname for the new process image file. Otherwise,
    the path prefix for this file shall be obtained by a search of the
    directories passed as the environment variable PATH ..."

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_spawn.html

But another optimization can be done: to search the compress_program in
$PATH only once. Here's a test case:

   strace -ff -o /tmp/sort-log3 src/sort -S 1M --compress-program=gzip Makefile 
| wc -l

searches for 'gzip' in $PATH 12 times. With the attached patch, it searches
only once.

Pulling that invariant up to global scope is an excellent optimization.

Pushed.

thanks!
Padraig

Reply via email to