Skip to content

perf(hooks): ~60 ms of the 90 ms Bash-group baseline is module import + compiling 188 module-level regexes per dispatch #1283

Description

@devseunggwan

Symptom

A no-op Bash tool call (ls -la) costs 90 ms in the PreToolUse(Bash) dispatch group at 9a75c74. That is acceptable, and far better than the pre-#613 ~35 cold starts — this issue records where the remaining fixed cost sits so it is a known quantity rather than an assumption.

cProfile of hooks/_lib/_dispatch.py PreToolUse Bash claude with a trivial command:

Component Time
python3 -c pass 8 ms
import _hook_utils alone 26 ms
import _hook_utils + _transcript + _git + _fire_ledger + _paths 42 ms
_load_main × 55 members (exec of each impl.py) 84 ms cumulative
of which re.compile × 188 58 ms
hook logic for ls -la remainder, ~10 ms

Python's re cache is per process, and every dispatch is a fresh process, so all 188 module-level patterns across the 55 members are recompiled on every Bash tool call, whether or not the member's cheap discriminator would have returned early.

Options (none urgent)

  1. Lazy compile: wrap the heavy patterns in a functools.lru_cache'd accessor so a member that returns at its cheap gate (tool_name != "Bash", no trigger token in the command) never compiles them. The shared ones in _hook_utils are the biggest single block.
  2. Precompiled cache: not available for re without pickling tricks; not recommended.
  3. Fewer members: the hook-prune audit (docs/hook-prune-audit.md, chore(hooks): evidence-based hook prune audit #713) is the structural lever — every dropped member is one less exec + compile.

Suggest deferring unless the baseline drifts past ~150 ms; the two transcript-scanning members (#1277, #1278) dominate real-world latency by an order of magnitude.

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions