Skip to content

perf(helpers): inline normalize+random in generate_id; drop grep fork#673

Merged
Chemaclass merged 2 commits into
mainfrom
perf/663-cache-generate-id
May 12, 2026
Merged

perf(helpers): inline normalize+random in generate_id; drop grep fork#673
Chemaclass merged 2 commits into
mainfrom
perf/663-cache-generate-id

Conversation

@Chemaclass
Copy link
Copy Markdown
Member

Summary

  • bashunit::helper::normalize_variable_name now tests the first-character identifier rule with a pure-bash case glob instead of piping into grep. Drops one fork per call across all callers (snapshot, test_doubles, generate_id).
  • bashunit::helper::generate_id inlines the normalization and random-suffix logic so it no longer subshells into normalize_variable_name or random_str. Two forks saved per call.
  • generate_id is 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?

  • The issue proposed a monotonic counter to replace random_str, but generate_id is invoked via $(...) at all call sites — counter increments inside a subshell don't persist to the parent. Inlining random_str (cheap: 6 $RANDOM indexes, no fork) gets the same end result without touching call sites.

Test plan

  • New tests in tests/unit/helpers_test.sh:
    • test_generate_id_uses_pid_suffix_when_not_parallel
    • test_generate_id_appends_random_suffix_when_parallel
    • test_generate_id_sanitizes_basename
  • Existing test_normalize_variable_name covers 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 sa clean
  • make lint clean
  • Bash 3.0+ compat preserved

Closes #663

@Chemaclass Chemaclass added enhancement New feature or request refactoring Refactoring or cleaning related labels May 12, 2026
@Chemaclass Chemaclass self-assigned this May 12, 2026
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
@Chemaclass Chemaclass force-pushed the perf/663-cache-generate-id branch from eb3d2ea to acd60ba Compare May 12, 2026 08:55
@Chemaclass Chemaclass merged commit 1400383 into main May 12, 2026
24 checks passed
@Chemaclass Chemaclass deleted the perf/663-cache-generate-id branch May 12, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request refactoring Refactoring or cleaning related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(helpers): cache or cheapen generate_id per test

1 participant