Skip to content

perf: eliminate hot-path subprocess forks in test runner#731

Merged
Chemaclass merged 14 commits into
mainfrom
perf/optimize-loop
Jun 19, 2026
Merged

perf: eliminate hot-path subprocess forks in test runner#731
Chemaclass merged 14 commits into
mainfrom
perf/optimize-loop

Conversation

@Chemaclass

Copy link
Copy Markdown
Member

🤔 Background

bashunit spawns subprocesses on paths that run once per assertion, per test, and per result line. On large suites these fork+exec calls dominate wall-clock time, especially on macOS/Bash 3.x.

💡 Changes

  • Replace echo|grep, echo|sed, wc, and tr forks with pure-Bash case globs and parameter expansion on per-assertion / per-test / per-result hot paths
  • Short-circuit ANSI rebuild, title interpolation, and file:line parsing when there is nothing to do (the common case)
  • Return interpolated titles via an out-var slot instead of a command-substitution subshell
  • Behaviour-preserving and Bash 3.0+ compatible; full suite green serial, parallel, and --strict

Walks the call stack on every assertion; the camelCase check forked echo+grep per frame. Replace with a pure-bash case glob (test_* | test[A-Z]*). Add a characterization test locking camelCase frame detection.
strip_ansi forked echo+sed twice per assert_equals, even on the success path. Plain text with no backslash or control bytes is unchanged by the sed, so return it directly via a pure-bash case guard (~80x faster on that path). Differential-verified identical output across edge cases; add characterization tests.
…_contains

assert_contains declared the same 'local label_override=""' six times (copy-paste artifact). Reduce to one.
…d_with

Detecting a trailing numeric index forked echo+grep on every call. Use a pure-bash case glob ('' | *[!0-9]*), matching the integer-check style already used in env.sh.
Add cases for glob metacharacters and percent signs (fast path, unchanged) and a backslash escape (slow path, echo -e expands then sed strips). Guards the strip_ansi fast-path optimization against regressions.
parse_file_path_filter forked grep plus two seds to split a 'file:line' filter. Replace with parameter expansion guarded by a digit-only case. Differential-verified identical across edge cases; add a colon-followed-by-non-number test.
Eight acceptance tests defined an identical strip_ansi() helper. Move one copy to tests/bootstrap.sh (loaded by default for all runs) and drop the duplicates.
print_successful_test and print_risky_test wrapped rpad output in 'printf %s\n' inside another command substitution, which immediately strips the added newline. Capture rpad directly, saving a fork per rendered test line.
rpad rebuilt the left text character-by-character to preserve ANSI codes across a possible truncation. When the visible text fits the width (the common case for result lines), that loop just reconstructs the original. Use the original directly in that branch and run the rebuild only when truncating (~30% faster per colored result line). Output verified identical across truncation/ANSI/edge cases.
Replaced two grep forks (a redundant no-'N' check plus a digits-only check) with a single case glob. A pure-digit result already excludes a literal 'N', so the digits-only test is sufficient. Matches the integer-check style used in env.sh.
Replaced echo|wc|tr and grep|wc|tr pipelines with pure-bash newline counting (real newlines plus literal backslash-n escapes). Removes three forks per call and the dependency on echo not interpreting backslashes. Counts verified identical to the old implementation across empty/single/real-newline/literal/mixed inputs.
…o placeholder

Interpolation placeholders are '::N::', so a function name without '::' can never interpolate. Return it immediately in that case, skipping the per-argument escape_single_quotes forks that ran for every data-provider test whose name had no placeholder. Output verified identical; add a no-placeholder-with-args test.
apply_interpolated_title returned via stdout, forcing a per-test $(...) capture, and itself captured interpolate_function_name. Switch it to the documented _BASHUNIT_RUNNER_*_OUT slot pattern and short-circuit non-'::' names, removing two forks per test for the common (non-interpolated) case. State side-effects and output are unchanged.
@Chemaclass Chemaclass added the refactoring Refactoring or cleaning related label Jun 19, 2026
@Chemaclass Chemaclass self-assigned this Jun 19, 2026
@Chemaclass Chemaclass merged commit d445f9b into main Jun 19, 2026
31 checks passed
@Chemaclass Chemaclass deleted the perf/optimize-loop branch June 19, 2026 03:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring Refactoring or cleaning related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant