commit:     51ad398621e668920c46916c9a90768e27c2df62
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sat Jul 27 01:11:27 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri May 30 08:14:18 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=51ad3986

phase-helpers.sh: don't declare __unpack_tar() repeatedly in loop

Within the unpack() function, the __unpack_tar() function is re-declared
for every single positional parameter that is iterated over.

I have noticed the tendency for functions to define other functions, and
estimate that this strategy is in place for two reasons. Firstly, so as
not to clutter the function namespace with functions that may otherwise
remain unused. Secondly, to rigorously defend against portage-owned
functions being replaced elsewhere, such as an in ebuild or an eclass.

Be that as it may, declaring functions is not cost-free and doing so
within a loop is simply going too far. Let's hoist the function upwards
so that it is declared just after unpack() validates its arguments.

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

 bin/phase-helpers.sh | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/bin/phase-helpers.sh b/bin/phase-helpers.sh
index 4df7a2010d..444575a7c6 100644
--- a/bin/phase-helpers.sh
+++ b/bin/phase-helpers.sh
@@ -335,6 +335,19 @@ unpack() {
 
        [[ -z "$*" ]] && die "Nothing passed to the 'unpack' command"
 
+       __unpack_tar() {
+               if [[ ${y,,} == tar ]] \
+                               && ! ___eapi_unpack_is_case_sensitive \
+                               || [[ ${y} == tar ]]; then
+                       $1 -c -- "${srcdir}${x}" | tar xof -
+                       __assert_sigpipe_ok "${myfail}"
+               else
+                       local cwd_dest=${x##*/}
+                       cwd_dest=${cwd_dest%.*}
+                       $1 -c -- "${srcdir}${x}" > "${cwd_dest}" || die 
"${myfail}"
+               fi
+       }
+
        for x in "$@"; do
                suffix=${x##*.}
                y=${x%.*}
@@ -388,19 +401,6 @@ unpack() {
                        continue
                fi
 
-               __unpack_tar() {
-                       if [[ ${y,,} == tar ]] \
-                                       && ! ___eapi_unpack_is_case_sensitive \
-                                       || [[ ${y} == tar ]]; then
-                               $1 -c -- "${srcdir}${x}" | tar xof -
-                               __assert_sigpipe_ok "${myfail}"
-                       else
-                               local cwd_dest=${x##*/}
-                               cwd_dest=${cwd_dest%.*}
-                               $1 -c -- "${srcdir}${x}" > "${cwd_dest}" || die 
"${myfail}"
-                       fi
-               }
-
                myfail="unpack: failure unpacking ${x}"
                case ${suffix,,} in
                        tar)

Reply via email to