Skip to content

Perf/stack zone#193

Open
agustincico wants to merge 22 commits into
codefrau:mainfrom
agustincico:perf/stack-zone
Open

Perf/stack zone#193
agustincico wants to merge 22 commits into
codefrau:mainfrom
agustincico:perf/stack-zone

Conversation

@agustincico

Copy link
Copy Markdown

No description provided.

agustincico and others added 22 commits July 5, 2026 19:46
Design for Cog-style stack zone (flat frames + lazy context reification)
written against SqueakJS semantics (send/doReturn/transferTo/closures).
Synthetic fib-by-sends benchmark isolates heap contexts vs flat frames
and JS vs WASM: flat frames alone give 1.38x in pure JS (incremental
path inside jit.js); frames + per-method WASM compilation give 2.7x
(phase 2 target). A WASM bytecode interpreter alone is break-even.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Runs ws/client/cuis.image headless with a fully virtualized environment
(virtual Date/Date.now/performance.now, seeded Math.random, inert
WebSocket) and accumulates an FNV-1a hash of the execution trace
(sendCount/pc/sp/method oop per slice). Two runs of the same VM produce
identical hashes (validated 6/6 over the full 3.6M-send Cuis startup),
so any semantic divergence introduced by VM changes is caught by
comparing against a recorded golden. --bench mode measures wall time
on the same workload with the real clock: baseline 2.87M sends/s.

golden.json is machine-specific (timezone offset enters the trace),
so it is gitignored; each machine records its own with --golden.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The image creates its .changes file and debug logs next to itself,
so a fresh clone's first run saw different filesystem state than
subsequent runs. Delete those artifacts before every run (and
gitignore them) so all runs start from the canonical state.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
sp is an index into context.pointers today but a zone index with the
stack zone, so it (and scheduling-level slices/virtualMs) must not be
part of the cross-representation oracle. Hash sendCount/method/pc only.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Mechanical refactor, groundwork for the stack zone: vm.stack is THE
operand array (today activeContext.pointers, kept in sync at every
context switch; a zone page once flat frames land). All stack helpers,
arg copies, perform's stack slides, and jit-generated code now index
vm.stack instead of reaching through activeContext. become refreshes
vm.stack in case the active context's pointers array was swapped.

No behavior change: differential trace hash is identical to the
pre-refactor golden; bench unchanged (2.90M vs 2.87M sends/s, noise).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…on-write)

Mapping vm.primitives.js showed unwind prims 195-199 return false, so all
exception handling walks contexts from Smalltalk code via sends — which
means every image-level access to context fields goes through either a
send with the context as receiver or a reflective primitive. That bounds
the read-through surface to two chokepoints and allows a proxy-free v1.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Second mechanical groundwork refactor: interpretOne and jit-generated
code now access temps as temps[tempOffset+n] instead of reaching
through homeContext.pointers[6+n]. In context mode tempOffset stays 6
and vm.temps === homeContext.pointers (synced at the same points as
vm.stack), so generated code is equivalent; in stack-zone mode temps
will live in the frame at a per-activation base. Trace hash identical
to golden; bench unchanged (2.80M sends/s, noise).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…validated

New vm.stackzone.js implements the design in perf/stack-zone-design.md:
sends push flat frames into growable zone pages; heap Contexts exist
only as married snapshots (created on thisContext/closure creation/
process switch), synced when a married context is a send receiver or
reflective-primitive target, widowed on frame return, and flushed
wholesale on writes/become/snapshot. makeBaseFrame inflates dormant
contexts; the interpreter never executes on heap contexts. Enabled per
instance via options.stackZone; context mode is untouched (golden
trace identical).

Supporting changes:
- interpretOne receiver inst-var stores routed through storeInstVar
  (incl. the doubleExtended 0x84 cases Cuis uses for Context>>sender/
  terminate — missing those made unwind chain surgery invisible to
  live frames)
- blockReturn bytecodes routed through doBlockReturn
- context identity hashes drawn from a separate stream so ordinary
  objects get allocation-order-independent hashes across representations
- tryPrimitive activation-change check uses page/fp in frames mode
  (an instance-level activeContext accessor was a 10x+ slowdown)
- harness: --frames/--nojit flags, cadence-independent oracle sampling
  at fixed sendCount checkpoints, per-send fine logging, debug hooks

Validation: full Cuis startup (3,398,634 sends, boot through WS timeout
to quit) produces the identical trace hash as context mode without jit,
zero divergent checkpoints. Bench (20M sends): frames-nojit 2.62M
sends/s vs contexts-nojit 2.46M (+6.5%) vs contexts+jit 2.84M — the
jit frames templates are the next milestone.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Generated code in stack-zone mode detects activation changes by
(fp, zonePage) instead of context identity, routes receiver inst-var
stores through the married-context write-through (with a fresh vm.pc
and a resume label, since a flush rebuilds the frame), and handles the
at:put: quick prim's flush hazard by re-executing the bytecode
idempotently. blockReturn and closure creation now use the mode-neutral
doBlockReturn/activeContextObj in both modes (identical semantics in
context mode). Frames mode compiles methods again (executeNewMethodZ
and full-closure activation call compileIfPossible).

Validation: frames+jit produces the IDENTICAL trace to the context-mode
jit golden over the full Cuis startup (3.6M sends to quit) — the jit's
trace-visible at:-cache fast path behaves the same in both modes.
Context mode codegen is byte-identical to before (golden still passes).

Bench (20M sends): frames+jit 2.99-3.06M sends/s vs contexts+jit
2.74-2.81M — ~9% faster on a primitive-heavy workload; send-heavy
code should gain more (spike: 1.38x on pure send/return).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ates

- squeak.js / squeak_headless.js import vm.stackzone.js; the run/
  launcher's generic option parsing already forwards #stackZone from
  the URL fragment to the interpreter. squeak_node.js gets -stackZone.
- syncMarriedContext replaces whole-page sync at the hot call sites
  (thisContext, married send receivers, reflective reads): syncing one
  frame is O(frame) for the page top and marries the caller shallowly
  instead of cascading down the chain. Cuis 6's ensure:-heavy code made
  the cascade pathological: 68% of JS time in syncPage, 5x slowdown.
- married-context probes are gated by a class-identity compare before
  touching .frame, keeping megamorphic receivers off that load.
- makeBaseFrame refuses Context subclasses (the gates compare against
  MethodContext identity; a married subclass would evade them).
- zone stats + marriage-source counters reported by the harness bench.

Validated on both image formats: V3 (cuis.image) golden identical in
both modes; Spur 32 (Cuis6.2-32) context and frames traces identical.
Bench: V3 frames+jit still +9%; Spur frames+jit 1.68M vs 2.05M sends/s
(-18%, down from -83% before the sync fix) — remaining gap tracks the
154k closure-creation marriages per 10M sends and their GC pressure;
known issue, next optimization target.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
New Squeak.Compiler2 compiles V3 methods to JS where operand-stack
slots live in JS locals tracked at compile time, spilled to the zone
only at send sites and reloaded on trampoline re-entry (a local
`entering` flag distinguishes re-entry from internal jumps; reload
depth is the label's static stack depth, which also covers mid-method
interpreter-to-compiled transitions at loop heads). Monomorphic inline
caches per send site bypass findSelectorInClass on class hit.
Unsupported bytecodes bail the whole method to jit1.

Bugs already caught by the differential oracle and fixed: resume
labels past the last jump target were not being generated; closure
creation marries the active frame so it must spill first (stale vm.sp
corrupted married snapshots and GC roots); the IC send path skipped
sendZ's married-context sync gate.

One known semantic divergence remains (V3 block temp pattern in
WorldState>>runStepMethods, first differs at send ~717679 of the Cuis
startup), so jit2 is opt-in: options.jit2 / --jit2 in the harness.
Frames mode without jit2 remains golden-identical.

Perf preview (20M sends, primitive-heavy workload): frames+jit2
3.16M sends/s vs frames+jit1 2.85M vs contexts+jit1 2.77M (+14% over
today's VM) — with 75% method coverage, no direct calls yet, on the
least favorable workload. Send-heavy code should gain much more.

Also: Dialogo.32bits.image (Cuis 4507, V3 6521) validated in the
oracle: frames and contexts traces identical; bench at parity with a
known flushAll storm (23k full flushes / 10M sends from married-
context stores) to optimize.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Root cause of the remaining divergence: in V3 blocks, temps beyond
numArgs+numCopied are allocated as operand-stack slots (the pushNil
prologue), so a block's storeTemp writes the same physical slot that
jit2 maps to a JS local — the local kept the stale value and the next
spill clobbered the stored one. Found by per-method bisection
(JIT2SEL=div,rem) down to SharedQueue>>nextOrNil, whose dequeued item
was overwritten with nil, making the World's event queue look empty.

v1 fix: methods whose blocks access temps at or beyond their
args+copied ceiling bail to jit1 (correct, loses ~125 methods; a
pinned-zone-slot refinement can recover them later).

Validation: frames+jit2 now produces the IDENTICAL trace to the
context-mode golden over the full Cuis startup, and identical hashes
to context mode on Dialogo.32bits.image. Bench (20M sends): contexts
2.78M / frames+jit1 2.87M / frames+jit2 3.16M sends/s (+14%), with
1370 methods compiled and correctness fully intact.

Also adds JIT2SEL bisection switch and marriage-source counters to
the harness debug tooling.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
executeNewMethodZ and the closure activations now invoke the callee's
compiled function as a plain JS call instead of returning to the
trampoline. The JS stack never holds suspended state: every level
returns as soon as its Smalltalk continuation leaves its frame (the
existing activation checks), so a process switch unwinds the whole
chain with one compare+return per level — no exceptions needed.
Depth-capped at 512 for the JS stack limit. Because this lives in the
activation path, interpreter, jit1 and jit2 senders all get it with
zero template changes.

Validation: golden-identical on the full Cuis startup, hashes
identical on Dialogo. Perf: neutral on real workloads (startup wall
2.39s vs 2.39-2.50s; the trampoline hop was not the dominant send
cost — frame materialization is). Kept because it is the structural
prerequisite for frameless leaf calls (args as JS arguments, Cog-style
frameless methods), which is where the send-heavy 2.5x actually lives.

Also: -jit2 flag for squeak_node.js; the run/ launcher already
forwards #jit2 from the URL fragment.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Leaf-eligible methods (prim-less; pushes, temp/inst stores, smallint
arithmetic/comparisons, ==/class, forward jumps, returns; no sends, no
closures, no loops, no allocations — arithmetic overflow deopts instead
of allocating so a deopt-restart never double-allocates; inst stores
bail if any later deopt point exists) get a second compiled form that
takes (vm, rcvr, args...) as plain JS arguments and returns the result
or a deopt sentinel. jit2 send sites with an IC hit call it with NO
spill, NO frame, NO zone traffic.

Trace parity vs the golden is exact: sendCount++ on the leaf path
(-- on deopt; the framed retry re-increments); leaves only run when
interruptCheckCounter > 0 and decrement it, so check cadence matches
the framed path; a harness hook samples successful leaf sends with the
same (sendCount, method, pc) tuples as the executeNewMethod wrapper.

228 leaf forms out of 1370 jit2 methods on the Cuis startup; full-run
trace identical to the context-mode golden.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Every named-primitive call (prim 117) was rebuilding two JS strings
from Smalltalk bytes (module + function name), looking the module up
in a dictionary and doing a string-keyed function lookup. tryPrimitiveZ
now resolves once and caches a bound wrapper on the CompiledMethod
(method.primFn), preserving the namedPrimitive protocol exactly
(success flag reset, interpreterProxy.argCount/primitiveName,
primMethod, boolean-vs-success return, unbalance warning). Missing
modules/functions keep the slow path so warnings behave identically.

FloatArrayPlugin is denylisted: cached, it diverges the trace when
combined with any other cached plugin (undiagnosed global-state
interaction, possibly the at-cache); its slow path is correct.
Bisection tooling: PRIMFN_SKIP=mod1,mod2 or * disables caching.

Golden-identical on Cuis startup and Dialogo. Bench (20M sends):
3.22M sends/s vs 2.75M contexts baseline (+17%, best so far).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
loadModule checks interpreterProxy.failed() after initialiseModule,
which reads the AMBIENT primHandler.success flag. The original 117
path always runs after doPrimitive resets success=true on entry, but
resolveNamedPrim resolves before that point, so a stale success=false
from a previously failed primitive made the module load report
"initialization failed", get cached as null, and every named prim of
that module fail forever (Smalltalk fallbacks ran instead — correct
but trace-divergent, and slower). This also explains the phantom
"interaction between cached plugins": which module hit the stale flag
depended on which loads shifted to resolve time.

Fix: reset success before loadModule in resolveNamedPrim. The
FloatArrayPlugin denylist is removed — it was just the first victim.

Golden-identical with all plugins cached; Dialogo identical.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… quick prims

Coverage jumps from 1370/485 to 1779/76 (96%) on the Cuis startup:
- super sends (0x85, 0x84-op1) with a statically-filled IC (the lookup
  class is fixed per site: method class's superclass), plus 0x86
  second extended sends. Gotcha fixed: verifyAtClass for super sends
  must be the lookup class, not the receiver class — using rc poisoned
  the at:-cache cacheability test (makeAtCacheInfo) for super at: sends
- closure indirection temps (0x8A) built from mapped locals
- remote temp vector push/store/storePop (0x8C-0x8E)
- generic quick prims next/atEnd/new/x/y/nextPut:/new: as direct full
  sends (quickSendOther always fails for these)
- JIT2NO=feature,... bisection gates for future debugging

Golden-identical; Dialogo identical.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The generated per-class constructors now predeclare oop/hash/dirty/
mark/nextObject, so lazily-added properties (dirty on first store,
mark/nextObject during GC) no longer fork hidden classes per object.
Covers freshly instantiated objects and image-loaded ones (rebuilt via
renameFromImage's `new instProto`). Small win (~1%); also records the
negative result of the flat-single-shape experiment (~7% slower).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Dialogo's UI loop does ~23k sender-stores into married contexts per
10M sends (terminate-style chain surgery); each one flushed EVERY live
page. storeToMarriedContext now flushes only the owning page.

That exposed a page leak the full flush had been masking: pages of
terminated processes stay live-but-unreachable (termination cuts the
chains at the context level, never touching the page), growing the
zone to 23k pages and making allocPage scans O(n). Fix: when no dead
page exists and the zone holds 32+ pages, allocPage evicts a suspended
page by flushing it (always correct: resuming re-inflates via
makeBaseFrame) and reuses it. Zone now capped at 32 pages on Dialogo.

Golden and Dialogo traces identical. Dialogo bench 3.05M sends/s
(2.99M before; 2.16M with the leak). PAGEDBG/SMCDBG harness counters.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The run/ launcher rewrites fragment options as key= (empty value),
which parsed as falsy and silently disabled the flags in the browser.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…rn headless

Wraps all Node FilePlugin primitives in vm.freeze + setImmediate unfreeze,
mimicking the browser's async IndexedDB file access. Main loop is now async
and yields to the event loop while frozen (interpret gets a noop thenDo,
required by freeze). SSDBG counts enableSingleStepping calls.

Reproduces the accumulative frames+jit2 slowdown seen in the browser
(semantics identical: ctx and frames hashes match under FREEZE_SIM).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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