Re: Performance regression in 5.x: fork-heavy $() loops slow down ~2.5x
From:
Chet Ramey
Subject:
Re: Performance regression in 5.x: fork-heavy $() loops slow down ~2.5x on second run when built with --with-bash-malloc=no
Date:
Fri, 15 May 2026 10:11:58 -0400
User-agent:
Mozilla Thunderbird
On 5/13/26 11:53 PM, debianyu wrote:
In an interactive shell, executing a fork-heavy loop with command substitution
twice consecutively exhibits a ~2.5x slowdown on the second and subsequent runs:
time for i in {1..10000}; do echo $(date) >/dev/null; done # ~16s
time for i in {1..10000}; do echo $(date) >/dev/null; done # ~40s
time for i in {1..10000}; do echo $(date) >/dev/null; done # ~40s
The slowdown persists for the shell's lifetime, including subshells spawned
from it.
Bisecting build configurations and bash versions reveals the slowdown requires
both:
Bash 5.x series (5.0 onward; 4.4 unaffected)
./configure --with-bash-malloc=no (bash uses glibc malloc)
Either condition alone does not trigger the issue:
That's interesting. I haven't seen the glibc malloc trigger such a dramatic
slowdown before.
Trigger Condition: Loop body must involve both fork and execve. Builtins or
fork-only operations (e.g., x=$(:)) do not trigger the issue.