# The edit / normals core: one Rust core, native on the server, wasm in the client Round `wasm-edit-core` (2026-10-29), from main 15f89850: the owner's "(b) plus the scoped refresh" of `docs/rust-core.md` §3.9. Scratch, runs and scripts: `logs/wasm-edit-core/` (`runs/`, `bl/`, `run-*.sh`, `table.py`, `click.py`). Measurement code: the `bench:` commits of branch `wasm-edit-core` (`bench/wasm-edit-core/`, NOT FOR MERGE) and the same probe patch applied to a detached main 15f89850 (`logs/wasm-edit-core/base-15f8`). Every before/after pair alternates main and branch, two runs each, Bun 1.3.14. ## Answer - **One core.** CSG edits, the normals build / refresh / prune, compaction, voxel patches (undo / redo, the CA delta apply) and voxel diffs run in voxel-core: natively in the server (world-rs `edit.rs`, `ec_*`), as wasm in the client and in plain Bun processes (`voxel-wasm`, `voxel_core.wasm`). The TS implementations are deleted (§1.4): every holder of an edit runs the same code, so client and server bytes agree by construction. - **Bytes.** Main 15f89850's TS outputs are frozen as goldens (1 260 edit / refresh / prune outputs in `edit-core-corpus`, 31 reference builds in `normals-build-corpus`); the native and the wasm core reproduce every one (§2). - **Client edit + normals per edit** (4.7 MB earthlike region, Phase 0 corpus): V8 6.9–12.6 → **4.9–6.9 ms** (p95 7.7–18.4 → 5.3–7.5); JSC 8.2–13.2 → **4.2–7.5 ms** (p95 9.6–15.9 → 4.6–8.5). The server's native core: 3.2–5.9 ms. The client's normals prune at upload: ml 1 2.8–3.4 → 1.6–1.8 ms, ml 2–3 within 0.1 ms of main (§3.1). - **Rotated stamps reach peers as their voxel diff** (§4): peer click → visible 253–260 → **124–133 ms** p50 (p95 365–378 → 171–183), 20–26 KB per peer per die stamp (deflated; the command was 107–179 B). The editor's own stamp shows in 18 ms (89–94 on main). - **Copy mode** (each call copies its inputs into linear memory) is what landed; regions resident in wasm memory would save ~1–2 ms per edit and is an owner decision (§6). - **wasm**: 130.7 KB raw, 54.2 KB gzip, **43.3 KB brotli**; streamed compile + instantiate 7.7–7.8 ms (Chromium), compile 10–11 ms (JSC); linear memory 101 MiB after the whole corpus (§3.2, §5). - **Server look tick** unchanged (pile-huge p50 1.04–1.18 vs 1.09–1.32 ms, p99 13.8–14.4 vs 13.8–14.9; the 50 m/s walk 29.04–29.09 vs 29.10–29.13 Hz, no interval ≥ 100 ms on either; §3.3). Safari: the module uses only Safari 15 features and no threads; JSC (Bun) is the proxy — no real Safari was measured. - **Gates**: §7. ## 1. What was built ### 1.1 voxel-core | piece | where | |---|---| | net-edit's edit algorithm: the flag refresh scoped to the brushes' window (`refresh_flags_windowed`), compaction against the LIVE size (`SVT_WASTE_RATIO` 1.5), `normalize3` by sqrt (not hypot) | `svt64_edit.rs` | | the `text` brush: glyphs rasterised by JS (`marshalBrushes`: the block's `bitmapTextPixelAt` per pixel), sampled exactly as `_textContainsPoint` | `csg.rs` | | brushes as an f64 **marshal** (the values the caller holds; a fixture's f64 brushes are not rounded through the f32 wire), stamp sources by index | `csg.rs` `read_brushes` | | `pruneNormalSVT` and the coarse-normal facing rule (was `coarse-normal-occupancy.ts`) | `normals_prune.rs` | | `compact_if_bloated`; `diff_region_voxels`, `apply_region_patch`, `csg_via_patch` | `svt64_edit.rs`, `edit_command.rs` | | a one-block cache over the stamp's source reads (`CachedReader`; the source-leaf cache of rust-core §3.3), also used by the diff | `svt64_read.rs` | The Phase 0 wire decoder (`decode_csg_brushes`), `has_unsupported_shapes` and the 20d4dbe2-algorithm corpus test are deleted. ### 1.2 Server (native) - `server/src/world/edit-core-native.ts` binds `ec_*` and installs itself as the process's backend on import (`manager.ts` imports it). - **Terrain edits in place** (`applyTerrainEdit`): the input is the region version's views (no copy into JS); the result comes back as a detached region version stamped with the version the edit bumps to, viewed without a copy (`gcView`) and installed with `lruSetPinned` (`wr_insert`), so no region bytes are copied on the TS side. - Structures (`applyStructureCsg`), the stream prune (`pruneForStream`), the structure normals build, undo / redo diffs and patches and the CA write-back's `applyVoxelDelta` run on the same native core (bytes in, views out). - The server's region normals were already native (Phase 2). ### 1.3 Client (wasm) - `voxel-wasm/build.sh` (`bun run build:wasm`, run by `build:client`): wasm32, fat LTO, `wasm-opt -O3 --strip-debug`; the client build copies it next to the bundle and the bundle fetches it as `voxel_core.wasm?v=`. - `client/src/edit-core-boot.ts` starts the fetch when the bundle loads; `main()` awaits it first (the module is ready before the world client starts). `server/src/main.ts` serves `.wasm` as `application/wasm` (streaming compile). - `common/src/edit-core.ts` keeps the old API (`applyCsgEditFull`, `buildNormalSVT`, `refreshNormalSVT`, `pruneNormalSVT`, `compactSvtIfBloated`, `applyVoxelDelta`, `diffRegionVoxels`) over the process's backend; region-edits, entity-edits, region-manager and structure-pool call it unchanged. - **Copy mode** (`edit-core-wasm.ts`): a call copies its inputs into linear memory and copies its outputs out, with three exceptions: - the module keeps the last edit's nodes until the next edit (the predicted region's normals refresh, and the next edit when it comes back, read them in place); - region-sized inputs (≥ 64 KB: a halo neighbour, the region an edit starts from) are kept in linear memory by array identity, up to 32 MiB (region and normals arrays are never mutated once held; an entry is freed on eviction or when its array is collected); - the normals prune reads its trees in place through an import (`vw_tree_word`): it walks the normals tree only down to the cut and probes few material words, so copying the region and its six face neighbours (~5 ms on this region) cost more than the prune. - A Bun process with no backend set (tests, scripts) loads `voxel-wasm/pkg/voxel_core.wasm` synchronously on first use. ### 1.4 Deleted - `common/src/svt64-edit.ts` (2 089 lines; the voxel reads physics and tools use moved to `svt64-voxel.ts`), `coarse-normal-occupancy.ts` (351), `svt64-patch.ts` (823), the normals builder in `voxel-normals-svt.ts` (3 048 → 261 lines: format constants and the TS reads), the shape predicates in `csg.ts` (1 231 → 952), `svt64-edit-flags.test.ts`, the Phase 0 wasm bench and `scripts/svt64-patch-record.ts`. - Tests of TS internals (the predicates, the flag walk) are gone; the Rust unit tests cover the windowed flags (`windowed_flags_match_full_walk`) and the cached reader; the goldens cover the outputs. - Over the branch (non-test, non-bench TS): +2 336 / −6 898 lines; Rust +1 838 / −632. - The client bundle: 13.24 → 12.83 MB (not minified, inline sourcemaps); the net Worker and the audio Worker bundles each shrink by 0.28 MB (they pulled the edit TS in through `@voxel/common`). ## 2. Bytes - `server/src/world/edit-core-corpus.test.ts` (`test:corpus`), recorded on main's TS before anything changed (`GOLDEN_RECORD=1`, `golden.ts`), run against the native and the wasm core: terrain and crystal-caverns 3×3×3 blocks; every brush shape (sphere, aligned and rotated box, cylinder on each axis, ellipsoid, text aligned / centred / end / multi-line / fallback glyphs / unknown font / pixel size 2, stamps identity / yaw 90 / yaw 45 / tilted / masked / unresolvable); masks all / only / not; through and first-surface; REGION palette (stained glass) and RAW16 overflow; edits across a face, an edge and a corner; multi-brush lists; an f64 brush; a 60-edit chain on one region (compaction decisions); a region carved empty, edited empty and filled; an undo diff and patch; structure edits on the die prefab; normals builds, every edit's refreshes (own box and the neighbours' facing layers), prunes at ml 0–4 with no, full and pruned face neighbours. 1 260 goldens, all equal on both backends. - `normals-build-corpus`: the deleted exact reference's 31 outputs frozen. - Commands replayed as a patch (§4) have their result defined through the diff: the corpus checks they hold the same voxels as main's edit and that replaying the diff reproduces the holders' bytes. ## 3. Edit cost Phase 0 corpus (`logs/rust-core-p0/corpus`: earthlike seed 42691, region [4, 0, −4], 4.7 MB, its 26 neighbours, 9 edits). Per edit: `applyRegionCsg` then `refreshNormalSVT` over the edit's box with the halo a client assembles; medians of 25 (Chromium) / 15 (Bun) runs after 3 warm-ups, ms. | edit | V8 main (TS) | V8 branch (wasm) | JSC main (TS) | JSC branch (wasm) | native (server core) | |---|---|---|---|---|---| | sphere carve r4 | 7.8 / 7.7 | **5.8 / 6.0** | 9.0 / 9.2 | **6.3 / 6.3** | 2.7 + 1.6 | | sphere carve r12 | 8.0 / 8.2 | **5.7 / 5.2** | 9.4 / 9.6 | **5.2 / 5.2** | 2.8 + 1.8 | | sphere carve r24 | 9.8 / 12.6 | **6.9 / 6.8** | 13.2 / 13.2 | **7.4 / 7.5** | 3.0 + 2.6 | | sphere fill r12 | 7.7 / 9.4 | **5.3 / 6.0** | 9.4 / 8.9 | **5.0 / 5.1** | 2.0 + 2.0 | | sphere at the −X face r12 | 8.0 / 9.4 | **5.0 / 5.2** | 9.3 / 8.7 | **4.6 / 4.5** | 1.7 + 1.7 | | box / cylinder / ellipsoid | 6.9–8.4 | **4.9–5.3** | 8.2–8.8 | **4.2–5.2** | 1.6–1.8 + 1.5–1.8 | | rotated die stamp | 71.1 / 73.1 | **64.7 / 61.6** | 73.8 / 73.1 | **60.2 / 64.1** | 44–45 + 8.0–8.2 | | p95, spheres and primitives | 7.7–18.4 | **5.3–7.5** | 9.6–15.9 | **4.6–8.5** | | | first call after load | 33.3 / 33.7 | **17.3 / 17.4** | 33.1 / 34.3 | **19.8 / 19.3** | | (`runs/v8-*`, `runs/jsc-*`, `runs/native-branch-*`; the stamp runs through the patch definition on the branch, §4.) - Split: V8 wasm edit 3.0–3.6 ms + refresh 1.8–3.3 ms; TS 4.4–5.8 + 2.4–6.9. Every run starts from a fresh copy of the region (as the client holds a region after an edit: a new SAB copy), so the wasm pays the copy in. - **Server `applyTerrainEdit`** (the main thread's whole call: the edit plus the store, cache and version bookkeeping; the normals refresh is dispatched to the job pool): main 17.2–19.3 → branch **13.5–15.2 ms** (sphere r4–r24, primitives), the face-crossing sphere 34.9–36.0 → 26.6, the rotated stamp 71.7–75.2 → 56.6–59.0 (`runs/srv-*`). The edit itself is 2–3 ms of it; the rest is bookkeeping this round does not touch (leftovers, §8). ### 3.1 The normals prune at upload `pruneNormalSVT` of the corpus region with its material and six face neighbours, as the client prunes every region it uploads at ml ≥ 1 (`runs/prune-*`, JSC, median of 15). "Cold": every tree new (the first prunes of an arrival); "own": the region's own trees new, its neighbours seen; "warm": a re-prune. TS reads every tree in place, so its three are the same work. | | main ml 1 / 2 / 3 | branch ml 1 / 2 / 3 | |---|---|---| | cold | 3.1–3.4 / 0.31–0.35 / 0.08 | **1.8 / 0.31 / 0.17** | | own | 2.8–2.9 / 0.16–0.17 / 0.02–0.03 | **1.6 / 0.11–0.12 / 0.03** | | warm | 1.1–2.7 / 0.06–0.13 / 0.01 | **1.5–1.6 / 0.09–0.10 / 0.02** | The first wasm build copied the trees in: 6.3–6.4 / 5.2–5.3 / 5.2–5.3 ms whatever the level, which the in-place reads remove. ### 3.2 Linear memory After the whole corpus (the 4.7 MB region edited from fresh copies, its halo, the die stamp through the patch, the ~12 MB normals scratch, the input cache at its 32 MiB bound): **101 MiB** (132 MiB with a 64 MiB cache bound); it never shrinks. ### 3.3 Server look tick The `docs/tick-long-tasks.md` §2 runs (`bl/`, `run-baselines.sh`, `tickstats.py`), alternating main and branch: | | main run 1 / 2 | branch run 1 / 2 | |---|---|---| | pile-huge, 2 clients, 75 s: tick p50 / p99 / max | 1.32 / 14.9 / 24.4; 1.09 / 13.8 / 30.3 ms | 1.04 / 13.8 / 21.4; 1.18 / 14.4 / 25.3 ms | | pile-huge: rate, interval p99 | 29.69 / 29.78 Hz; 38.7 / 37.9 ms | 29.78 / 29.74 Hz; 38.2 / 37.4 ms | | 50 m/s walk, 90 s: rate | 29.10 / 29.13 Hz | 29.04 / 29.09 Hz | | walk: interval p50 / p99 / max | 33.0 / 46.1 / 76.0; 33.0 / 45.0 / 64.7 ms | 33.0 / 46.0 / 74.0; 33.0 / 46.9 / 75.0 ms | | walk: intervals ≥ 100 ms | 0 / 0 | 0 / 0 | | walk: peak RSS | 770 / 806 MB | 781 / 793 MB | ## 4. Rotated stamps to peers as a voxel diff - **Rule** (`csgReplaysAsPatch`): a command holding a stamp whose rotation is not the identity reaches peers as a patch command (as undo / redo travel), the voxel writes that took each region from its input to the result; the editor's ack keeps the brushes. The old peer replay of such a stamp is gone. A non-rotated stamp still travels as its brushes. - **One result for every holder** (`csg_via_patch`): the edit is applied, diffed against the input over the leaves the brushes touch, and the diff applied to the input (`apply_region_patch`). The server, the editor's prediction and the peers applying the diff therefore produce the same bytes (the same voxels as main's edit; other bytes, since the tree is laid out by the patch). "The leaves the brushes touch": the walker writes inside every leaf a brush's AABB touches, and a stamp samples past a `worldHalf` smaller than its source, so the box is widened to whole leaves (`leafAlignedBbox`), also as the command's dirty box. - **Wire**: a die stamp's diff is ~0.5 MB as a `RegionVoxelDelta` body (270k writes); every patch command is deflated once for all peers (level 2, as region payloads): **20–26 KB**. The client decodes a patch straight into `(localIdx, mat)` u32 pairs (an object per voxel took ~150 ms per die stamp in the first run). - **Click → visible** (headed Chromium ×2 on utsuho — editor and peer — LAN, FIXTURE=surface-gi-motion, 12 sphere r12 and 12 rotated die stamps per run, each at its own spot; `runs/click-*`, `click.py`): | | main run 1 / 2 | branch run 1 / 2 | |---|---|---| | sphere: own visible p50 / p95 | 13 / 16; 14 / 17 | 12 / 16; 12 / 18 | | sphere: peer visible p50 / p95 | 36 / 53; 39 / 45 | **26 / 33; 26 / 36** | | stamp: editor local apply p50 | 91; 87 | **17; 16** | | stamp: own visible p50 / p95 | 94 / 149; 89 / 138 | **18 / 44; 18 / 41** | | stamp: ack p50 | 175; 160 | **79; 89** | | stamp: peer visible p50 / p95 / max | 260 / 378 / 378; 253 / 365 / 365 | **124 / 171 / 171; 133 / 183 / 183** | | peer apply per region p50 / p95 (all edits) | 22.3 / 74.5; 21.9 / 81.5 | **9.3 / 24.7; 9.2 / 24.3** | | bytes to a peer: sphere / stamp | 76 / 107–179 B | 76 B / **19.8–25.9 KB** | - The fixture's stamps cover less terrain than the corpus's (the editor's local apply is 16–17 ms here, ~62 ms on the corpus region). - Before the deflate and the packed decode, the first branch run sent the same stamps raw (0.51 MB per peer) and the peer saw them at 299 ms p50; sphere edits queued behind them (177 ms p50). ## 5. wasm: size, lifecycle, memory, Safari | | raw | gzip -9 | brotli -q 11 | |---|---|---|---| | `voxel_core.wasm` (wasm-opt -O3) | 130.7 KB | 54.2 KB | **43.3 KB** | | before wasm-opt | 159.2 KB | 60.0 KB | 47.6 KB | | Phase 0 module (edit + normals only) | 85 KB | 36.6 KB | 29.8 KB | - Chromium (utsuho): `instantiateStreaming` of the served module 7.7–7.8 ms (fetch included); `compile` of the cached bytes 0.5–0.6 ms, `instantiate` 0.1 ms. JSC (Bun): compile 10.0–10.8 ms, instantiate 0.4–4.6 ms. The first edit after load is 17–20 ms (TS: 33–34 ms with its JIT warm-up). - Linear memory: 101 MiB peak on the corpus (§3.2). - Features (`wasm-opt --detect-features`): mutable-globals, sign-ext, nontrapping-fptoint, bulk-memory, reference-types, multivalue — Safari 15+ per the feature tables (not verified on a real Safari). No threads and no shared memory: no cross-origin-isolation dependency. ## 6. Owner decisions (decided 2026-10-29) The owner took all three recommendations: copy mode; every non-identity stamp rotation sends its diff; every stamp's dirty box is widened to whole leaves (`regionBrushBbox`), stamp bytes unchanged. 1. **Copy mode or regions resident in wasm memory.** Landed: copy mode (the client's SAB region model is untouched; the module holds only the last edit's nodes and a 32 MiB input cache). Resident mode would save the copies — about the gap between the wasm and the native core, **~1–2 ms per edit** on this 4.7 MB region (JSC wasm 4.2–7.5 vs native 3.2–5.9 ms) — and cost: linear memory that never shrinks (Phase 0: 195 MiB for 27 regions resident vs 101 MiB peak here), the wasm32 4 GiB cap and mobile Safari's large-`grow` failures, every arrival copied in (the net Worker decodes into a SAB a non-shared memory cannot alias), and a rewrite of the region manager's storage. Recommendation: keep copy mode; revisit with net-entity step d's local simulation, which would share the memory. 2. **The patch rule's scope.** Any non-identity stamp rotation sends the diff (90° yaws included). Peers pay 20–26 KB instead of ~0.1 KB per die stamp; at 1 stamp / s a peer receives ~25 KB/s. Alternatives: diffs only above a replay-cost threshold, or for every stamp. 3. **Stamps write past their declared AABB** (pre-existing): a stamp writes inside every leaf its `worldHalf` box touches, sampling its source there, so a `worldHalf` smaller than the source writes up to 3 voxels outside the box the command reports, and the normals refresh (over the reported box) can miss them. Patch commands now report the leaf-aligned box; brush commands are unchanged. Options: clip stamp writes to the AABB (changes bytes; a deliberate re-baseline) or widen every stamp's dirty box to leaves. ## 7. Gates - Bytes: `edit-core-corpus` (1 260 goldens) and `normals-build-corpus` (31) on both backends, part of `test:corpus`. - `physics-suite --raw`: bit-identical to main 15f89850, 812 `RAW` lines, zero diff. - `test:corpus`: `TOTAL: 91 pass, 0 fail across 1 shard`. - `bun run check`: passes. `bun run lint`: passes. `bun run test`: `TOTAL: 2079 pass, 0 fail across 38 shards`. - `test:ca`: `TOTAL: 73 pass, 1 fail across 4 shards` (the pre-existing `late join … authoritative water`; `13-earthlike-coast: reload=FAIL`), as main. - **Containerfile**: built end to end with rootless podman on reisen (not pushed, image removed): the Rust stage builds `libvoxel_native.so` and `voxel_core.wasm` (binaryen's wasm-opt, the wasm32 target), the bun stage copies the module in and builds the client. The first attempt with Debian bookworm's lld 14 failed (`undefined symbol: __heap_end`); the image links with the toolchain's own rust-lld. ## 8. Leftovers - The server's `applyTerrainEdit` spends ~11 ms of ~13 on bookkeeping (the pre-edit material hash for the normals cache key, the SQLite put). - The input cache's 32 MiB bound is a guess (101 MiB peak with it, 132 with 64); a client playing a long session was not measured. - `pruneTree` and the stream encode stay TS (rust-core Phase 4). - Entity structure normals: unchanged path, now native (`StructureManager`). - The `bench:` commits and the probe patch are not for merge. ## 9. Reproduce ```sh GOLDEN_RECORD=1 bun test server/src/world/edit-core-corpus.test.ts # (on main's TS only) bun run build:wasm && bun run build:world-rs bun test server/src/world/edit-core-corpus.test.ts logs/wasm-edit-core/run-cpu.sh # JSC client + server edit, alternating flock logs/utsuho.lock logs/wasm-edit-core/run-v8.sh # V8 on utsuho flock logs/utsuho.lock logs/wasm-edit-core/run-click.sh # click -> visible, editor + peer logs/wasm-edit-core/run-baselines.sh looktick|walkfast both python3 logs/wasm-edit-core/table.py; python3 logs/wasm-edit-core/click.py ```