perf(assert): fork-free arg join in core string assertions#846
Merged
Conversation
The core string assertions joined their haystack args with a
command-substitution fork ($(printf '%s\n' "${arr[@]}")) on every call.
Introduce bashunit::assert::join_to_slot, a fork-free return-slot helper that
joins with newlines and strips trailing newlines, exactly mirroring the old
subshell output. Wired into assert_contains/not_contains, assert_matches/
not_matches and assert_string_starts_with/ends_with (+negations) — 7 sites.
Join step ~35x faster (10.6s -> 0.3s over 20k iterations) and one fewer fork
per assertion, with identical behaviour. assert_line_count is intentionally
left on the subshell (line-count logic depends on that exact form).
Characterization tests pin the preserved contract: trailing newlines ignored,
internal newlines preserved, multi-arg haystacks joined with newlines.
Closes #844
Claude-Session: https://claude.ai/code/session_01JSeB8UzLCpqst55hAK6dED
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
Related #844
The core string assertions forked a subshell (
$(printf '%s\n' "${arr[@]}")) on every call just to join their haystack arguments. Multiplied across thousands of assertions, that fork is the dominant per-assertion cost.💡 Changes
bashunit::assert::join_to_slot, a fork-free return-slot join that mirrors the old subshell exactly (newline-join + strip trailing newlines).assert_contains/assert_not_contains,assert_matches/assert_not_matches,assert_string_starts_with/assert_string_ends_withand their negations (7 sites). Join step ~35× faster (10.6s → 0.3s / 20k iters), one fewer fork per call, identical behaviour.assert_line_countintentionally keeps the subshell — its line-count logic depends on that exact form (noted in perf(assert): remove per-assertion array-join fork in assert.sh #844).Sibling issue #845 (lowercase forks in
assert_contains_ignore_case) was investigated and closed with benchmark evidence — herestring measured marginally slower and combining operands is unsafe for multiline data.https://claude.ai/code/session_01JSeB8UzLCpqst55hAK6dED