perf: Loop over the bash variable directly instead of starting subprocesses.#2333
Merged
marckhouzam merged 1 commit intospf13:mainfrom Dec 6, 2025
Merged
perf: Loop over the bash variable directly instead of starting subprocesses.#2333marckhouzam merged 1 commit intospf13:mainfrom
marckhouzam merged 1 commit intospf13:mainfrom
Conversation
…cesses. Today, the loop is printing the completions one value per line and then reading one line to a variable. We can just skip the whole IO rigamarole by looping over the completions directly.
Collaborator
|
Will this cause problems if some completions have a space in them? |
Contributor
Author
|
It shouldn't. The quotes around the array variable should prevent word splitting like that. $ arr=( "one" "two" "three four" "five")
$ for n in "${arr[@]}"; do
echo "$n"
done
$ for n in ${arr[@]}; do
echo "$n"
done |
Collaborator
|
Ok. Give me a bit of time to revive the completion tests that I haven't looked at in a while and run them. |
Luap99
approved these changes
Nov 27, 2025
Contributor
Luap99
left a comment
There was a problem hiding this comment.
I did a quick spot check with podman and it seems to work correctly even if the values have spaces in them.
Contributor
Author
|
Anecdotally, this appears to shave off something like 10ms: #!/usr/bin/env bash
arr=()
for ((i=0; i<1000; i++)); do
arr+=($i)
done
trials=1
loop_arr_1() {
sum=0
for ((j=0; j<trials; j++)); do
for i in "${arr[@]}"; do
((sum+=i)) || true
done
done
}
loop_arr_2() {
sum=0
for ((j=0; j<trials; j++)); do
while IFS='' read -r i; do
((sum+=i)) || true
done < <(printf '%s\n' "${arr[@]}")
done
}
time loop_arr_1
time loop_arr_2 |
marckhouzam
approved these changes
Dec 6, 2025
Collaborator
marckhouzam
left a comment
There was a problem hiding this comment.
The regressions tests pass.
LGTM
bejaratommy
added a commit
to bejaratommy/cobra
that referenced
this pull request
Apr 11, 2026
…cesses. (spf13#2333) Today, the loop is printing the completions one value per line and then reading one line to a variable. We can just skip the whole IO rigamarole by looping over the completions directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Today, the loop is printing the completions one value per line and then reading one line to a variable. We can just skip the whole IO rigamarole by looping over the completions directly.
I had added this optimization to #2126 but it doesn't look like it got picked up by #1743.