perf: eliminate hot-path subprocess forks in test runner#731
Merged
Conversation
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.
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.
🤔 Background
bashunit spawns subprocesses on paths that run once per assertion, per test, and per result line. On large suites these
fork+execcalls dominate wall-clock time, especially on macOS/Bash 3.x.💡 Changes
echo|grep,echo|sed,wc, andtrforks with pure-Bashcaseglobs and parameter expansion on per-assertion / per-test / per-result hot pathsfile:lineparsing when there is nothing to do (the common case)--strict