Skip to content

feat(octane): count engine work in profile builds - #1894

Merged
trueadm merged 5 commits into
mainfrom
feat/profile-counters
Oct 8, 2026
Merged

trueadm merged 5 commits into
mainfrom
feat/profile-counters

Conversation

@trueadm

@trueadm trueadm commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This is phase 1 of #1886, scoped to the review's recommended first cut: a counter core with snapshot/diff, a small counter set covering the motivating examples (wasted renders, rebuilt arms, abandoned work), the strip check, and one benchmark that drives a real interaction end to end. Hook counts, hydration counters, timeline tracks, and <Profiler> come in later PRs.

Profile builds (profile: true, or devtools: true under vite dev) now keep session totals of the engine work around component renders. All probes sit behind the existing __OCTANE_PROFILE_ENABLED__ guard; there is no second flag.

Counter Counts
component.render Component body invocations, including attempts that suspend, throw, or replay
component.renderSuspended / component.renderErrored Attempts ended by a suspension or an error. A throw ends every attempt it unwinds through
component.bailout Memo and implicit bailouts
block.create / block.unmount Render Blocks allocated and torn down
arm.keep / arm.swap @if/@switch evaluations that re-rendered the current arm, or replaced it. A slot's first arm is not a swap
boundary.fallback / boundary.catch Entries into the Suspense fallback or the error catch arm
scheduler.drain Passes over the render queue
commit.root Root transactions accepted at the commit validation point
rollback.root Root transactions rolled back
rollback.journalEntries Journal entries undone, (log.length − checkpoint) / 4, sampled before replay
rollback.capture Speculative captures whose effects, refs and bindings were dropped. Each capture counts once

Counters measure attempted work, so a render a later rollback discards still counts.

Changes in response to the review on #1886:

  • Drains, render attempts, and accepted commits are separate counters, and commit.root counts acceptance rather than entry. The root-shape retry in the benchmark commits with zero drains: it re-renders outside the queue.
  • The journal unit uses the checkpoint, and a parked row is not counted as a rollback.

API

The API lives on octane/profiling and globalThis.__OCTANE_PROFILER__; nothing is added to octane.

  • counters() returns the current totals.
  • snapshot() returns the totals with an envelope: schema, recording generation, build, renderers, recording, and time.
  • diff(before, after) throws when the snapshots cannot be compared:
    • different schema, build, or renderer set;
    • stop() or clear() between them;
    • recording off;
    • arguments in the wrong order.

Each renderer declares the counters it records when its module loads: dom declares all 15, and universal only the render counters. A counter that no installed renderer records is absent from a snapshot, not zero.

Counters are session totals, so evicting buffered events never shrinks them; only clear() resets them. In profile builds a probe is one increment into a preallocated Float64Array and allocates nothing per event.

Benchmark lane

benchmarks/lib/profile-counters.mjs mirrors the precise-coverage collector. Each sample:

  1. loads a fresh context and runs setup;
  2. takes a snapshot, runs the operation (which resolves only once its completion condition holds), and takes a second snapshot;
  3. diffs the snapshots in the page, so the runtime validates the pair;
  4. runs semantic verification.

benchmarks/suspense-recovery/profile-counters.mjs runs the existing root-suspension fixture, its controls and verification as a DEV + PROFILING build. The controls are now shared with root-work.mjs through root-controls.mjs; the serialized adapter is byte-identical, so root-work's recorded adapterHash does not change. The lane gates semantics: every hold rolls back exactly one root render without committing, and every retry commits once without a rollback. The weekly Bench workflow runs it, along with the collector's unit tests.

Profile builds compile the generic program, so this lane complements the production precise-coverage lane and does not replace it.

Validation

  • Targeted tests, one project each:
    • profiling.test.ts, profiling-disabled.test.tsrx, production-profile-bundle.test.ts (octane): 19 passed.
    • profiling-runtime.test.tsrx (octane-profile): 21 passed.
    • universal-native.test.ts -t profil (octane): passed.
  • Mutation check: disabling the arm-swap, root-rollback, boundary, and capture probes in runtime.ts failed the three matching runtime tests; restored afterwards.
  • node --test benchmarks/lib/profile-counters.test.mjs benchmarks/lib/precise-work.test.mjs: 11 passed.
  • node benchmarks/suspense-recovery/profile-counters.mjs, one local run: all 10 shape/operation samples passed their gates. For example, branch/hold gave rollback.root 1, commit.root 0, arm.swap 1, rollback.journalEntries 16, rollback.capture 1.
  • pnpm --filter octane build: dist imports verified. dist/node/profiling.js reads the module-level __octaneDev flag.
  • pnpm typecheck:files on the changed src files: clean. In the tests, the only new error (count as string) is fixed; the others predate this branch, and tests are excluded from packages/octane/tsconfig.json.
  • pnpm sync (no generated changes), pnpm format:files:check.
  • Full pnpm test, pnpm typecheck, pnpm format:check: left to CI.

Strip check. I bundled src/index.ts and src/universal.ts from main and from this branch with esbuild, __OCTANE_PROFILE_ENABLED__: false, and NODE_ENV set to production and then development, using the same tsconfig and source paths of equal length:

Lane Code with identifiers kept Fully minified raw gzip / brotli
prod index.ts identical 511,647 = 511,647 +4 / +29
prod universal.ts identical 368,738 = 368,738 −4 / +9
dev index.ts identical 576,643 = 576,643 0 / −37
dev universal.ts identical 417,423 = 417,423 +3 / +59

The compressed deltas come only from minified-identifier assignment: two module-level WeakMaps swap their short names. That is the review's distinction made concrete:

  • Optimized output contains no added code.
  • Minified identifier choice is not byte-stable once new imports exist.
  • Unminified DEV output keeps the guarded branches as dead if (false) code that never runs.

The first version of this check caught a real leak: top-level new Float64Array(COUNTER_NAMES.length) retained the name table in builds that ignore sideEffects. It is now @__PURE__ with a literal, type-checked length.

CI bench report (comment):

  • All 60 application bundle values (the Vite lanes) are unchanged.
  • All 64 js-framework work counters are unchanged.
  • Only the esbuild public-import lanes moved, by ±1 to 87 bytes of gzip or brotli in both directions and within budget. That is the same identifier-reshuffle effect as above.

perf-review

Perf review: feat/profile-counters (8444db8..worktree)

Scan: node scripts/perf-review-scan.mjs → 0 candidates.

Hot paths touched (each change is a guarded probe; the code is identical when profiling is off):

  • createBlock, unmountBlockInner: per Block.
  • renderBranchSlot: per @if/@switch evaluation.
  • setTryBranch: per boundary transition.
  • drainQueue: per flush.
  • commitRootRenders: per root commit.
  • rollbackRootRender, rollbackTransitionJournal, discardOffscreenCapture: rollback paths.
# Severity Location Dimension Finding Required evidence Provided?
1 note packages/octane/src/runtime.ts (new profiling.js imports) reachability New runtime imports are referenced only inside __OCTANE_PROFILE_ENABLED__ guards Code-identity bundles vs main yes (table above)
2 note packages/octane/src/profiling.ts (counter tables) reachability Module-scope typed arrays are @__PURE__ with a literal length, so builds that ignore sideEffects drop them Code-identity bundles yes
3 needs-evidence profile builds only allocation / CPU Probe overhead inside profile builds is not measured. Each probe is a guarded typed-array increment, next to an existing per-render ProfileEvent allocation that dominates Profile build with vs without counters on js-framework no — see follow-ups

Shapes: no new field on BlockImpl, ScopeImpl, or LiteBlockImpl. Per-capture deduplication uses a module WeakSet, the same pattern as trackedComponents. DOM and scheduling: no change. Compiled output: no compiler change.

Risk / follow-ups

  • Probe overhead in profile builds (perf finding 3) should be measured separately for counters, attribution and timeline output, as the review asks.
  • Per-commit attribution (getCommits(), delayed effects attributed to their originating commit) needs commit identities distinct from DRAIN_ID; the counters keep those identities separate so that work can build on them.
  • Later phases of Profiling mode: count and trace engine work (renders, hooks, Blocks, arms, boundaries, rollbacks) and add <Profiler> #1886:
    • per-kind hook counts (resolveSlot, use(), useContext);
    • hydration counters;
    • the Scheduler track and markers;
    • <Profiler> as its own milestone, including keyed identity when profiling is off;
    • DevTools tabs.
  • profile: 'counters' (counters over unmodified production output) remains an open question on the issue. The precise-coverage lane stays the production oracle.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Instruments core DOM runtime paths (commits, rollbacks, scheduler, boundaries) behind the profile guard; production non-profile bundles are intended unchanged, but counter semantics on rollback/commit are easy to get wrong.

Overview
Profile builds now accumulate session engine-work counters (renders, bailouts, blocks, @if/@switch arms, boundaries, queue drains, root commits/rollbacks, journal replay, discarded captures) alongside the existing event buffer. The profiler gains counters(), snapshot(), and diff(before, after) with schema/build/renderer/generation checks; renderers register which counters they record at load time (dom all 15, universal render-only). Counters are absent when not recorded, survive event-buffer eviction, and reset only on clear().

Runtime wires guarded probes in runtime.ts (and universal render counters in universal-core.ts) behind __OCTANE_PROFILE_ENABLED__, including component render outcomes on __profileEndRender.

Benchmarks & CI: shared root-suspension page controls in root-controls.mjs; collectProfileCounters mirrors precise-work timing; profile-counters.mjs runs the fixture in DEV+profiling and asserts hold = one rollback.root, retry = one commit.root. Weekly Bench workflow runs collector tests and the new lane so counter semantics cannot rot.

Docs/tests: profiling docs and changeset describe the API; unit/runtime/bundle tests cover diff validation, DOM counter behavior, and production strip behavior.

Reviewed by Cursor Bugbot for commit 73b61f9. Bugbot is set up for automated code reviews on this repo. Configure here.

trueadm and others added 2 commits October 8, 2026 13:46
Phase 1 of #1886. Profile builds keep session totals of the work around
component renders: render attempts and outcomes, bailouts, Blocks created
and torn down, @if/@switch arms kept or replaced, Suspense fallbacks and
error catches, render-queue passes, accepted root commits, and work
discarded by rollbacks (root transactions, journal entries, captures).

profiler.snapshot() carries a schema version, build, renderer set and
recording generation, and profiler.diff() rejects pairs it cannot compare.
Each renderer declares the counters it records, so an unsupported counter
is absent rather than zero. Every probe sits behind the existing
__OCTANE_PROFILE_ENABLED__ guard; optimized builds without profiling
contain no added code.

A DEV + PROFILING lane for the root-suspension benchmark fixture records
the counters around a real hold and retry, gated on their semantics, and
runs in the weekly Bench workflow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability labels Oct 8, 2026
@trueadm
trueadm marked this pull request as ready for review October 8, 2026 12:47
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-reachability: 5 value(s) increased within budget

Compares a447b6db6, the merge commit's first parent with a73132515 on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

target operation base ms head ms head/base 95% CI
octane-tsrx select_lots 0.009 0.007 −5.3% −12.8% … −3.4% 🟢 faster
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.233 3.550 ±0.0% −1.4% … +3.5% ⚪ within noise
octane-tsrx replace 7.717 6.508 −3.0% −7.1% … +0.7% ⚪ within noise
octane-tsrx add 2.842 2.958 +0.0% −0.0% … +3.6% ⚪ within noise
octane-tsrx update 0.150 0.154 −0.3% −3.1% … +4.6% ⚪ within noise
octane-tsrx select 0.007 0.007 −5.1% −10.1% … −2.1% ⚪ within noise
octane-tsrx swap 0.044 0.041 −1.5% −8.2% … +4.5% ⚪ within noise
octane-tsrx remove 0.017 0.016 −2.2% −6.6% … +0.0% ⚪ within noise
octane-tsrx runlots 31.058 30.608 −1.8% −4.3% … +0.8% ⚪ within noise
octane-tsrx select_lots 0.009 0.007 −5.3% −12.8% … −3.4% 🟢 faster
octane-tsrx clear 44.783 46.692 +4.7% +1.6% … +9.2% ⚪ within noise
octane-jsx run 7.433 8.658 +1.9% −1.6% … +4.4% ⚪ within noise
octane-jsx replace 12.083 11.033 −3.3% −11.0% … +5.4% ⚪ within noise
octane-jsx add 5.825 5.267 −0.0% −13.9% … +2.6% ⚪ within noise
octane-jsx update 0.372 0.443 +5.1% −8.1% … +34.9% ⚪ within noise
octane-jsx select 0.214 0.235 +1.7% −2.6% … +6.6% ⚪ within noise
octane-jsx swap 0.320 0.328 −1.2% −11.7% … +5.3% ⚪ within noise
octane-jsx remove 0.239 0.234 −0.3% −10.9% … +17.2% ⚪ within noise
octane-jsx runlots 40.558 43.408 +1.1% −1.2% … +6.1% ⚪ within noise
octane-jsx select_lots 2.422 2.207 −0.6% −4.2% … +15.5% ⚪ within noise
octane-jsx clear 46.967 47.525 +1.1% −4.2% … +3.4% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
binding-base-ui-esbuild gzip 69,298 69,299 +1 (+0.0%) 🔴
binding-base-ui-esbuild brotli 60,059 60,125 +66 (+0.1%) 🔴
binding-aria-esbuild brotli 2,982 2,979 −3 (−0.1%) 🟢
binding-radix-esbuild gzip 69,871 69,873 +2 (+0.0%) 🔴
binding-radix-esbuild brotli 60,620 60,634 +14 (+0.0%) 🔴
binding-mantine-hooks-esbuild gzip 30,491 30,490 −1 (−0.0%) 🟢
binding-mantine-hooks-esbuild brotli 27,122 27,180 +58 (+0.2%) 🔴
binding-usehooks-ts-esbuild brotli 27,414 27,390 −24 (−0.1%) 🟢

103 other measured values are unchanged.

Lynx dual-thread render (report only)

All 13 timed operations (13 without a verdict)
target operation base ms head ms head/base 95% CI
octane-lynx empty_startup_ms 0.384 0.422 +10.1% – ⚪ unpaired
octane-lynx create_1k_rows_ms 6.373 8.750 +37.3% – ⚪ unpaired
octane-lynx create_10k_rows_ms 95.229 82.580 −13.3% – ⚪ unpaired
octane-lynx update_1k_rows_ms 1.701 1.929 +13.4% – ⚪ unpaired
octane-lynx update_10k_rows_ms 10.628 9.335 −12.2% – ⚪ unpaired
octane-lynx-reentrant-10k drain_ms 108.282 107.456 −0.8% – ⚪ unpaired
octane-lynx-reentrant-20k drain_ms 214.656 208.554 −2.8% – ⚪ unpaired
octane-lynx-cold create_1k_rows_ms 20.073 23.310 +16.1% – ⚪ unpaired
octane-lynx-cold create_10k_rows_ms 101.304 127.508 +25.9% – ⚪ unpaired
octane-lynx-first-screen create_1k_rows_ms 100.999 114.301 +13.2% – ⚪ unpaired
octane-lynx-first-screen create_10k_rows_ms 763.538 946.795 +24.0% – ⚪ unpaired
octane-lynx-adopt create_1k_rows_ms 147.763 153.569 +3.9% – ⚪ unpaired
octane-lynx-adopt create_10k_rows_ms 1168.885 1406.658 +20.3% – ⚪ unpaired

@cloudflare-workers-and-pages

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 Updated (UTC)
🔵 In progress
View logs
octane-website f45dd1b Oct 08 2026, 12:59 PM

@trueadm
trueadm merged commit 0ce371a into main Oct 8, 2026
30 checks passed
@trueadm
trueadm deleted the feat/profile-counters branch October 8, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant