Skip to content

perf(interpreter): dispatch builtins through their trait objects - #2970

Closed
chaliy wants to merge 3 commits into
mainfrom
perf/direct-builtin-dispatch
Closed

chaliy wants to merge 3 commits into
mainfrom
perf/direct-builtin-dispatch

Conversation

@chaliy

@chaliy chaliy commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Builtin execution-plan lookup and execution call the trait object inside the Arc directly. This removes an extra boxed future at each async boundary.

Why

Allocation stack sampling found the Builtin for Arc<dyn Builtin> forwarding adapter on the command path. The interpreter already owns the Arc throughout each await, so it can call the underlying object directly.

Before / After

Instrumented release comparisons asserted exact output, stderr, and exit status in 100-command workloads.

Workload Allocation calls before After Requested bytes removed
100 empty commands 4,900 4,700 40,800
100 printf calls 6,725 6,525 40,800
100 substitution/printf pairs 19,385 18,985 81,600
100 printf/cat pipelines 33,932 33,532 81,600

Five alternating rounds confirmed the counts. Timing changes were approximately 0–1%. The change preserves the existing builtin hooks, resource budgets, execution plans, panic handling, and public trait API.

Risk

Checklist

  • Existing builtin and execution-plan tests selected
  • Backward compatibility considered

Start Proof
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 1d2ddd6 Commit Preview URL

Branch Preview URL
Nov 22 2026, 05:20 PM

@chaliy chaliy closed this Nov 22, 2026
@bashkit-pr-reviewer

Copy link
Copy Markdown

Review summary (PR #2970)

Verdict: Looks good — no findings. Low-risk perf cleanup; safe to merge once the #2969 lockfile overlap is resolved (rebase or merge as-is, your call).

Findings: None (no P0/P1/P2).

What I checked:

  • crates/bashkit/src/interpreter/mod.rs around both changed call sites (~L10191 builtin.as_ref().execution_plan(&plan_ctx) and ~L10311 builtin.as_ref().execute(ctx)): both sit inside the Box::pin(async move …) that owns the Arc<dyn Builtin>, so the borrowed &dyn Builtin future is lifetime-safe and identical in behavior to the old Arc forwarding impl — just one fewer boxed future per async_trait boundary. catch_unwind/AssertUnwindSafe panic handling, hook firing, and budget/context construction are untouched.
  • crates/bashkit/src/builtins/mod.rs (~L1456 trait def, ~L1821 impl Builtin for Arc<dyn Builtin>): the forwarding adapter is retained, so the public trait API is unchanged; sync forwarders (streams_io, llm_hint, reset_session_state) are still used via Arc at mod.rs:2707/8077/10227, which need no de-boxing.
  • knowledge/foundations/builtins.md Status section: knowledge updated in the same change per AGENTS.md, and the note accurately describes the direct-Arc dispatch.
  • AGENTS.md project rules: perf-only change with existing builtin/execution-plan tests cited; no new behavior, no backward-compat concern (impl kept), no sandbox boundary crossed, so no threat-model update needed.
  • PR metadata/files list: Cargo.lock (×5) bumps for url 2.5.8→2.5.9 / form_urlencoded 1.2.2→1.2.3 attributed to the perf: reduce stream and builtin dispatch allocations #2969 patch, disclosed in the description as dropping on rebase — reviewed as disclosed overlap, not a separate concern.

Not reviewed: did not build or run tests (no cargo in this shell, per workflow); relied on the PR's reported allocation-count deltas and exact-output assertions. Full per-hunk diff text was summarized by the diff tool, but the two mod.rs hunks plus the knowledge hunk were verified directly in the checked-out head (19c53da).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant