Skip to content

perf(runner): test functions accumulate across files, making multi-file runs quadratic — unset after each file (unit suite 64s → 22s) #829

Description

@Chemaclass

Problem

Sequential multi-file runs slow down quadratically with the number of test files. The framework sources every test file into the main shell and never unsets the test functions, so the shell grows as the run progresses — and since every test executes in a $() subshell, every fork copies an ever-fatter process. The cost is fork overhead (sys time), invisible to any per-file profile.

Measured on bashunit's own unit suite (63 files, 1072 tests, macOS, Bash 3.2):

Files run Wall Marginal cost/file
first 10 3.0s 0.30s
first 30 13.9s 0.55s
first 45 26.3s 0.82s
all 63 ~64s ~2s

Smoking gun: the last 18 files take 8.7s standalone but ~38s as the tail of the full run — a 4.4x penalty purely from accumulated shell state. Sys time explodes correspondingly (10 files: 1.3s → 45 files: 8.6s).

Fix (prototype validated)

Unset the file's test functions after the file has been processed, next to the existing hook cleanup (bashunit::runner::clean_set_up_and_tear_down_after_script, which already unsets set_up/tear_down/set_up_before_script/tear_down_after_script — src/runner.sh:2069).

Prototype diff (sequential + parallel path, after run_tear_down_after_script in bashunit::runner::load_test_files, ~src/runner.sh:494):

local _fn
for _fn in $_cached_fns; do
  unset -f "$_fn" 2>/dev/null || true
done

_cached_fns is the output of runner::functions_for_script (the file's test functions). unset -f is a builtin — zero forks, Bash 3.0-safe.

Prototype results (identical pass/skip/snapshot counts before/after: 1065/5/2, assertions 1447/5/2):

Suite Before After
./bashunit tests/unit/ ~64s 21.7s
./bashunit --parallel tests/unit/ ~26s 6.8s

CPU time dropped from ~61s to ~19.5s (sequential). Parallel benefits too: workers fork from the main shell, so they inherit the same bloat today.

Why it's safe

  • Unset happens after call_test_functions returns (sequential) or after the & worker has forked (parallel — the subshell got its copy at fork time), and after run_tear_down_after_script.
  • Data-provider functions and file-local helper functions are not in functions_for_script output and stay defined (same as today).
  • Cross-file behavior today: a later file's same-named test function silently overwrites the earlier one (per-file duplicate check only) — unsetting produces the same observable results.
  • Header count (find_total_tests) sources files in its own subshell, unaffected.

Implementation notes

  • TDD: RED test first — e.g. a runner test asserting a test function from file A is no longer defined when file B is being processed (or a timing-free proxy: after load_test_files over two fixture files, declare -F no longer lists file A's tests). Study tests/unit/runner_test.sh patterns.
  • Word-splitting the newline-separated $_cached_fns with default IFS is intended; function names contain no glob chars, but keep the loop consistent with existing style in load_test_files.
  • Watch the fork-budget acceptance tests (tests/acceptance/bashunit_coldstart_forks_test.sh, bashunit_run_forks_test.sh) — unset -f adds no forks, so they must stay green as-is.
  • Check interactions: --rerun-failed, --retry, --random-order, --shard, providers, --parallel (aggregation), --stop-on-failure.
  • CHANGELOG entry under ## Unreleased### Changed (perf, no behavior change).

Constraints

  • Bash 3.0+ (.claude/rules/bash-style.md); unset -f qualifies
  • Console output byte-identical (snapshot/acceptance suites green)
  • make sa, make lint clean

Acceptance criteria

  • Sequential unit suite counts identical to baseline (1065 passed / 5 skipped / 2 snapshot at time of writing)
  • ./bashunit tests/unit/ wall time drops to ~1/3 of baseline (~64s → ~22s on the reference machine)
  • ./bashunit --parallel tests/unit/ similarly improved (~26s → ~7s)
  • Full suite green: ./bashunit tests/, ./bashunit --parallel tests/, ./bashunit --parallel --simple --strict tests/
  • Fork-budget acceptance tests unchanged and green
  • make sa, make lint pass
  • CHANGELOG.md updated

Verification commands

time ./bashunit tests/unit/
time ./bashunit --parallel tests/unit/
./bashunit tests/ && ./bashunit --parallel tests/ && ./bashunit --parallel --simple --strict tests/
make sa && make lint

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions