perf(helpers): inline normalize+random in generate_id; drop grep fork#673
Merged
Conversation
bashunit::helper::normalize_variable_name now uses a pure-bash case glob to test the first-character identifier rule, eliminating a grep fork per call. All callers benefit (generate_id, snapshot, test_doubles). bashunit::helper::generate_id inlines the normalize and random_str logic, saving two subshell captures per call. generate_id is called once per test and per file load, so on a 800-test suite this drops ~1600 forks. Adds direct tests for generate_id covering sync/parallel suffixes and basename sanitization. Closes #663
eb3d2ea to
acd60ba
Compare
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.
Summary
bashunit::helper::normalize_variable_namenow tests the first-character identifier rule with a pure-bashcaseglob instead of piping intogrep. Drops one fork per call across all callers (snapshot, test_doubles, generate_id).bashunit::helper::generate_idinlines the normalization and random-suffix logic so it no longer subshells intonormalize_variable_nameorrandom_str. Two forks saved per call.generate_idis invoked once per test and once per file load. On the current 800-test suite that's ~1600 forks eliminated.Why not a counter-based ID?
random_str, butgenerate_idis invoked via$(...)at all call sites — counter increments inside a subshell don't persist to the parent. Inliningrandom_str(cheap: 6$RANDOMindexes, no fork) gets the same end result without touching call sites.Test plan
tests/unit/helpers_test.sh:test_generate_id_uses_pid_suffix_when_not_paralleltest_generate_id_appends_random_suffix_when_paralleltest_generate_id_sanitizes_basenametest_normalize_variable_namecovers all edge cases (empty string, leading digit, leading underscore, all-special-chars)./bashunit tests/unit/green (817 passed)./bashunit --parallel tests/green (1052 passed, 46s)make sacleanmake lintcleanCloses #663