commit:     3fa9f4d6aa646553a45cd0118fe46adb64bafd13
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sat Jun 28 06:03:05 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Jun 28 23:50:57 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=3fa9f4d6

isolated-functions.sh: choose an implementation of find0() in advance

Presently, there are two implementations of find0(), one of which serves
to cover situations where the -files0-from option is unavailable,
thereby retaining compatibility with the outdated GitHub CI environment.
Upon first being called, a heuristic determines which implementation
should be used, with the function redeclaring itself accordingly.

However, owing to the way in which find0() is typically used, this
methodology imposes a minor inefficiency. Consider the following.

$ set -x; printf '/var/empty\0' | find0 -mindepth 1
+ printf '/var/empty\0'
+ find0 -mindepth 1
+ printf '/\0'
+ find -files0-from - -maxdepth 0
+ find0 -mindepth 1
+ find -files0-from - -mindepth 1

Note the third and fourth lines of the xtrace output, where the test is
performed to determine whether the -files0-from option is supported,
after which the function is re-declared (though that step is not shown).
If, at this point, the exact same command is issued again then so shall
the trace output be exactly the same. But why?

The answer is that the command is a two-stage pipeline, causing the
find0() function to be called from one of the two ensuing subshells.
While the function does, indeed, redeclare itself, that declaration dies
along with the subshell in which it was issued. Hence, in practice, most
uses of find0() will result in find(1) being executed twice, with the
function being redeclared every single time.

Though a minor issue, address it by having the "isolated-functions.sh"
unit call find0() once at the time that it is sourced. That way, the
function shall be redeclared, with its final definition being propagated
at the point of forking.

See-also: a0210b3c49ea346b1e999f92a7ed89802e8d6849
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/isolated-functions.sh | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
index 9b4090862b..c6ce52928c 100644
--- a/bin/isolated-functions.sh
+++ b/bin/isolated-functions.sh
@@ -744,4 +744,7 @@ find0() {
        find0 "$@"
 }
 
+# Initialise the function now because find0() is normally called after forking.
+find0 < /dev/null
+
 true

Reply via email to