Skip to content

Commit c53259a

Browse files
committed
perf(filedir_xspec): avoid double negation !(!(...)) in extglob
Due to the Bash implementatin of negation matching, nested negations becomes extremely slow for a long filename. "!(!(...))" is interchangeable with "@(...)". The actual overhead caused by "!(!(...))" largely depends on the pattern specified to "...", but it is hard to predict. For example, "+(0)+(?).txt" or "+(+(0)).txt" may cause a large delay. Although those specific patterns are not used in bash-completion, but it is not predictable whether other patterns in bash-completion cause the same problem. It is recommended to avoid "!(!(...))" in general. Examples: $ time bash -O extglob -c 'echo !(!(+(+(0)).txt))' 00000000000000000000.txt real 0m4.537s $ time bash -O extglob -c 'echo !(!(+(0)+(?).txt))' 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.txt real 0m0.991s
1 parent f40a0ca commit c53259a

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

bash_completion

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,14 +3264,14 @@ _comp_compgen_filedir_xspec()
32643264
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
32653265
# news://news.gmane.io/4C940E1C.1010304@case.edu
32663266
eval xspec="${xspec}"
3267-
local matchop=!
32683267
if [[ $xspec == !* ]]; then
32693268
xspec=${xspec#!}
3270-
matchop=@
3269+
xspec="@(|!($xspec|${xspec^^}))"
3270+
else
3271+
xspec="@(|$xspec|${xspec^^})"
32713272
fi
3272-
xspec="$matchop($xspec|${xspec^^})"
32733273
3274-
_comp_compgen -av toks -c "$quoted" -- -f -X "@(|!($xspec))"
3274+
_comp_compgen -av toks -c "$quoted" -- -f -X "$xspec"
32753275
32763276
# Try without filter if it failed to produce anything and configured to
32773277
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-} && ${#toks[@]} -lt 1 ]] &&

0 commit comments

Comments
 (0)