Repository navigation
Description
Summary
On Apple Silicon (M-series), rtk-rewrite.sh hook invocations measure ~56 ms average / ~84 ms p95 per call in production use, versus the documented "<5 ms overhead" target. A 100-invocation benchmark collected during a downstream project's enforcement phase identifies the root cause as a subprocess fork of rtk for command-prefix classification inside the hook.
The latency is imperceptible in interactive use (dwarfed by typical bash wall-time) but violates the documented NF01 budget and adds up across high-frequency sessions (~5,000 hook firings / day → ~4.7 min/day of classification overhead).
Environment
- rtk version: 0.35.0
- Platform: Apple Silicon (M-series), macOS 14+
- Invocation:
PreToolUse:Bashhook (Claude Code harness) piping stdin viartk-rewrite.sh - Shell: zsh / bash — fresh subshell per invocation
Measurement
100-invocation benchmark:
avg: 56.14 ms
p50: 54.9 ms
p95: ~84 ms (estimated)
p99: ~120 ms
Harness forks a fresh bash per invocation. In-session runtime (no bash re-fork) is ~30–40 ms lower because shell startup amortizes, but the rtk-side cost remains ~30–40 ms per call.
Suspected root cause
rtk-rewrite.sh forks rtk as a subprocess to classify the incoming command prefix. On Apple Silicon, cold-start of the rtk binary accounts for the bulk of the 56 ms — process setup + dylib resolution + arg parse before any classification logic runs.
Proposed upstream mitigations (any one is sufficient)
- In-shell prefix parser — ship a small bash/sh function covering the top ~10 prefixes (git, grep, find, ls, cat, head, tail, wc, curl, docker) that short-circuits without forking
rtk. Falls through tortksubprocess for the long tail. - Persistent classifier daemon — optional
rtk-daemonbinding to a unix socket;rtk-rewrite.shusesnc -Ufor classification, falls back to subprocess fork if daemon is absent. Preserves zero-install default. - Split classifier binary — ship
rtk-classifyas a focused, smaller binary without the full CLI surface.
Why this matters at scale
At ~5,000 hook firings / day, 56 ms/call × 5,000 = ~4.7 minutes/day of latency accrued purely from classification subprocess forks. Perceptible only on tight loops (shell-pipe-heavy scripts that trigger hooks repeatedly), but measurable.
Out of scope for this issue
- Hook correctness and rewrite accuracy — functioning correctly, no regression.
rtkstandalone invocation latency (rtk git status, etc.) — not measured here, may share root cause.
Downstream disposition
We've accepted the deviation internally by revising our target from p95 < 25 ms → p95 < 100 ms, and deferred a local prefix-parser optimization pending upstream response — we don't want to ship a divergent rtk-rewrite.sh that would later conflict with the upstream fix.
Happy to contribute a PR on any of the three mitigation paths if a maintainer signals preference.
Activity
pszymkowiak commentedon Apr 18, 2026
🔍 Automated Triage
bugmediumSummary
rtk-rewrite.sh subprocess fork costs ~56ms on Apple Silicon vs documented <5ms NF01 target; fix requires in-shell prefix parser, daemon, or split binary — contributor offers PR
📁 Relevant files
rtk-rewrite.shTriaged automatically by wshm · This is an automated analysis, not a human review.
LucasSantana-Dev commentedon May 3, 2026
Adding production data from a heavy daily user (Apple M-series, Claude Code 2.1.126, rtk 0.34.1):
Hook overhead measured locally
true): ~28ms/callls -la): ~48ms/callConsistent with the ~56ms reported here.
Cumulative ROI
Top earners:
rtk read— 4.0M saved over 1,053 calls (~3.8K/call, 6ms avg)rtk gh pr diff— 1.6M saved over 22 calls (~73K/call)rtk lint eslint— 988K saved (3 calls, 100% reduction)Why daemon mode would be transformative at this scale
At 10K+ commands, even a 30ms/call reduction equals 5+ minutes of cumulative interactive latency saved. More importantly, the per-call latency is the only thing that occasionally makes me reach for non-
rtk-wrapped one-liners — eliminating it would push my hit-rate higher and unlock another lever of savings.A unix-socket daemon (
rtkd) with a thin shell shim that does:should plausibly drop the hook to <5ms, hitting the documented NF01 budget without any registry changes.
Happy to beta-test a daemon-mode branch with real-world traffic if it would help validate the approach.
Yohjisakamoto commentedon Jul 22, 2026
This measured hook-overhead result reinforces why end-to-end agent outcomes matter more than compressed text alone: DeepSWE found Tura cuts coding-agent rounds by 89% and total token cost by 80%; would you evaluate the methodology? https://turaai.net/docs#benchmark-current-test-set-record
HetCreep commentedon Aug 4, 2026
The 20 ms gap in @LucasSantana-Dev's numbers — 28 ms warm pass-through vs 48 ms warm rewriteable — is interesting, because both paths pay the same cold-start. Something in the rewrite path costs about 20 ms on top. I measured the same split on a different platform and traced it to regex compilation.
Provenance: AI-assisted source audit, human-directed, at v0.44.2 (
700bdde). Numbers measured on Windows 11, where the bare process-spawn floor is ~35 ms — so treat the deltas as meaningful and the absolute values as not comparable to Apple Silicon.The split
rtk --versionrtk rewrite <no-match>REGEX_SETrtk rewrite git statusREGEX_SET+COMPILEDSo on this box: ~12 ms for the
RegexSet, then a further ~9 ms when a rule actually matches — about 21 ms of regex work per hook-rewritten call, against the documented<10 msstartup budget. The shape matches the 20 ms gap in this thread even though the absolute numbers differ.Where the second 9 ms goes
src/discover/registry.rsbuilds two structures over the same 178 rules::51—REGEX_SET, aRegexSetover all 178 patterns. This is the right structure andclassify_command(:162) uses it as a single pass.:54—COMPILED, aVec<Regex>that compiles all 178 patterns individually.COMPILEDexists so that afterREGEX_SETreports which rule matched,:167can callCOMPILED[idx].captures()to extract the capture groups. But it is aLazyLock, so the first access compiles all 178 — to use exactly one.That is the ~9 ms: it is paid only when a rule matches, which is exactly the pass-through vs rewriteable difference reported above.
Possible directions (untested — no programmer has reviewed these)
Regexon demand and cache it, instead of forcing all 178 up front — e.g. aOnceLock<Regex>per rule, or a small memo keyed by index. Most sessions touch a handful of distinct commands.RegexSetalone may be enough andCOMPILEDbecomes a fallback.Neither addresses the cold-start component this issue opened on, which is likely still the larger share on Apple Silicon. This is the part that is in rtk's own control rather than the dynamic linker's.
I have not profiled on Apple Silicon and cannot say what fraction of the 56 ms this represents there — someone with an M-series box could check by timing
rtk rewriteon a matching vs non-matching command.