commit:     86896ebdf18e8be5453a83f83b805126223b0441
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Wed Feb  4 05:42:11 2026 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Feb  6 01:45:15 2026 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=86896ebd

Micro-optimise the bash quote_args() routine

Rather than assemble an array of the requoted elements produced by
${param@Q} expansion before proceeding to update elements in place,
directly iterate over the words produced by the expansion while
incrementally assembling an array. Micro-benchmarking shows the latter
method to be 74% faster for my x86_64 workstation.

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

 functions.sh | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/functions.sh b/functions.sh
index fca4b51..467f746 100644
--- a/functions.sh
+++ b/functions.sh
@@ -715,16 +715,17 @@ _collect_entropy() {
 if [ "${BASH_VERSINFO-0}" -ge 5 ]; then
        eval '
                _quote_args_bash() {
-                       local IFS=" " args i
+                       local IFS=" " args word
 
                        (( $# > 0 )) || return 0
-                       args=("${@@Q}")
-                       for i in "${!args[@]}"; do
-                               if [[ ${args[i]} == \$* ]]; then
-                                       args[i]=${args[i]//\\E/\\e}
+                       for word in "${@@Q}"; do
+                               if [[ ${word} == \$* ]]; then
+                                       args+=("${word//\\E/\\e}")
+                               else
+                                       args+=("${word}")
                                fi
                        done
-                       printf "%s\\n" "${args[*]}"
+                       printf %s\\n "${args[*]}"
                }
        '
 fi

Reply via email to