feat(vm): add the kernel natives Machine.countUp and Machine.countDown #239

Merged
argus merged 2 commits from loop/164-retain-release-measure into main 2026-10-09 03:14:45 +00:00
Collaborator

Part 1 of #164: the kernel natives Machine.countUp and Machine.countDown, plus the measurement that chose them.

Measured (docs/PERFORMANCE.md 9.12, forge CI, binaries alternating, processor time, mean of three runs): a retain or release of a one-pointer value done in the interpreter's loop instead of a kernel call makes the new benchmarks/chain-walk 1.15x faster, in every pair of two runs. list-iterate gains 1.06-1.14x and map-count 1.02-1.09x. fibonacci(32) (nothing counted) stays level. Two forms were measured against main: plain TorbScript over Machine.load/Machine.store, and these natives. They are equally fast, so the natives win on the rest: the header layout stays in C, and they leave every count to the kernel while a sandbox is open, so a memory stop keeps its site. Runs: https://git.torb.dev/torbscript/language/actions/runs/406 and https://git.torb.dev/torbscript/language/actions/runs/419. In run 419, tools/conformance.sh --vm passed 279/279 with part 2 applied, and all three binaries printed the same lines.

Commits

  • feat(vm): add the kernel natives Machine.countUp and Machine.countDown:
    • manifest rows and static inline definitions in torb.h
    • regenerated torb_natives.h / machine_natives.c (CI-generated)
    • runtime tests in machine_test.c
    • std/machine docs
    • VM.md sections 6 and 10
    • Nothing calls the natives yet.
  • docs(performance): measure the retain and release of one pointer in 9.12: benchmarks/chain-walk.trb with its C twin, and PERFORMANCE.md 9.11.

Part 2, after a seed refresh that knows the two natives:

  • The emitter marks a kernel retain/kernel release of a shape with exactly one non-nested counted word and no tag: head operand B = the pointer's register (register + offset ≤ 65535), C = 1 (retain) / 2 (release).
  • The .Kernel arm of executeLoop calls Machine.countUp/countDown(Machine.load(frame + 8 * b)) first, and calls Machine.operate only where it answers false.
  • The bytecode format stays torb-bytecode-1, because old program text has C = 0.
  • The measured implementation is commit e6468e2d on the throwaway branch loop/164-measure2: about 30 lines in emit.trb and interpret.trb.

Refs #164

Part 1 of #164: the kernel natives `Machine.countUp` and `Machine.countDown`, plus the measurement that chose them. **Measured** (docs/PERFORMANCE.md 9.12, forge CI, binaries alternating, processor time, mean of three runs): a retain or release of a one-pointer value done in the interpreter's loop instead of a kernel call makes the new `benchmarks/chain-walk` **1.15x** faster, in every pair of two runs. `list-iterate` gains 1.06-1.14x and `map-count` 1.02-1.09x. `fibonacci(32)` (nothing counted) stays level. Two forms were measured against main: plain TorbScript over `Machine.load`/`Machine.store`, and these natives. They are equally fast, so the natives win on the rest: the header layout stays in C, and they leave every count to the kernel while a sandbox is open, so a memory stop keeps its site. Runs: https://git.torb.dev/torbscript/language/actions/runs/406 and https://git.torb.dev/torbscript/language/actions/runs/419. In run 419, `tools/conformance.sh --vm` passed 279/279 with part 2 applied, and all three binaries printed the same lines. **Commits** - `feat(vm): add the kernel natives Machine.countUp and Machine.countDown`: - manifest rows and `static inline` definitions in `torb.h` - regenerated `torb_natives.h` / `machine_natives.c` (CI-generated) - runtime tests in `machine_test.c` - `std/machine` docs - VM.md sections 6 and 10 - Nothing calls the natives yet. - `docs(performance): measure the retain and release of one pointer in 9.12`: `benchmarks/chain-walk.trb` with its C twin, and PERFORMANCE.md 9.11. **Part 2, after a seed refresh that knows the two natives**: - The emitter marks a `kernel retain`/`kernel release` of a shape with exactly one non-nested counted word and no tag: head operand `B` = the pointer's register (register + offset ≤ 65535), `C` = 1 (retain) / 2 (release). - The `.Kernel` arm of `executeLoop` calls `Machine.countUp`/`countDown(Machine.load(frame + 8 * b))` first, and calls `Machine.operate` only where it answers false. - The bytecode format stays `torb-bytecode-1`, because old program text has `C` = 0. - The measured implementation is commit e6468e2d on the throwaway branch `loop/164-measure2`: about 30 lines in `emit.trb` and `interpret.trb`. Refs #164
One count more, or one count less that is not the last, of the counted
block a value of one pointer holds: the retain and the release of the
interpreter's loop without a call of the kernel. Each answers whether it
did the whole work; false leaves the count to the kernel's Retain and
Release as before - a shared block, a count that would reach the shared
bit, the last count with its destructor and its free, a count of zero
and its panic, and every count while a sandbox is open, so a script that
runs out of memory stops where it stopped before. `static inline` in
torb.h like Machine.load.

Nothing calls them yet: this is the first of the two commits of a new
native. Once the published seed knows them, the second marks a `kernel
retain` or `kernel release` of a one-pointer shape in the head's unused
operands (B the register of the pointer, C 1 or 2) and lets the loop try
the native first. Measured in docs/PERFORMANCE.md 9.11.

Regenerates torb_natives.h and machine_natives.c (two thunks,
renumbered), pins the counts in runtime/tests/machine_test.c, and
updates VM.md sections 6 and 10.

Refs #164
docs(performance): measure the retain and release of one pointer in 9.11
Some checks failed
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 5s
pdisc / pdisc (pull_request) Failing after 10s
ci / gates (pull_request) Has been cancelled
ci / which runners exist (pull_request) Has been cancelled
ci / bootstrap (linux-x64) (pull_request) Has been cancelled
ci / tier A (linux-x64) (pull_request) Has been cancelled
ci / tier B (linux-x64) (pull_request) Has been cancelled
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Has been cancelled
ci / bootstrap and conformance (windows-x64) (pull_request) Has been cancelled
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been cancelled
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been cancelled
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been cancelled
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been cancelled
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been cancelled
ci / every host emits the same C (pull_request) Has been cancelled
ci / the VS Code extension (editors/vscode) (pull_request) Has been cancelled
ci / the playground (torb as WebAssembly) (pull_request) Has been cancelled
ci / pack the seed (pull_request) Has been cancelled
ci / archives (pull_request) Has been cancelled
51fde67e7b
Adds benchmarks/chain-walk.trb and its C twin, a chain of variant cells
walked by `match` with one count more and one less per step: the
retaining workload of the VM. Records the forge CI measurement of the
kernel call a count costs there, the loop counting in TorbScript and
through the new natives against main: 15% on the chain walk, 3 to 14%
on list-iterate and map-count, level on fibonacci, and why the natives
are the form to land.

Refs #164
orion force-pushed loop/164-retain-release-measure from 51fde67e7b
Some checks failed
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 5s
pdisc / pdisc (pull_request) Failing after 10s
ci / gates (pull_request) Has been cancelled
ci / which runners exist (pull_request) Has been cancelled
ci / bootstrap (linux-x64) (pull_request) Has been cancelled
ci / tier A (linux-x64) (pull_request) Has been cancelled
ci / tier B (linux-x64) (pull_request) Has been cancelled
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Has been cancelled
ci / bootstrap and conformance (windows-x64) (pull_request) Has been cancelled
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been cancelled
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been cancelled
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been cancelled
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been cancelled
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been cancelled
ci / every host emits the same C (pull_request) Has been cancelled
ci / the VS Code extension (editors/vscode) (pull_request) Has been cancelled
ci / the playground (torb as WebAssembly) (pull_request) Has been cancelled
ci / pack the seed (pull_request) Has been cancelled
ci / archives (pull_request) Has been cancelled
to 51c3750b2f
Some checks failed
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 4s
pdisc / pdisc (pull_request) Successful in 9s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 15s
ci / bootstrap (linux-x64) (pull_request) Successful in 5m33s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Failing after 3m53s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 49s
ci / gates (pull_request) Has been cancelled
ci / the playground (torb as WebAssembly) (pull_request) Has been cancelled
ci / pack the seed (pull_request) Has been cancelled
ci / archives (pull_request) Has been cancelled
ci / tier B (linux-x64) (pull_request) Has been cancelled
ci / every host emits the same C (pull_request) Has been cancelled
2026-10-09 00:42:03 +00:00
Compare
orion force-pushed loop/164-retain-release-measure from 51c3750b2f
Some checks failed
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 4s
pdisc / pdisc (pull_request) Successful in 9s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 15s
ci / bootstrap (linux-x64) (pull_request) Successful in 5m33s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Failing after 3m53s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 49s
ci / gates (pull_request) Has been cancelled
ci / the playground (torb as WebAssembly) (pull_request) Has been cancelled
ci / pack the seed (pull_request) Has been cancelled
ci / archives (pull_request) Has been cancelled
ci / tier B (linux-x64) (pull_request) Has been cancelled
ci / every host emits the same C (pull_request) Has been cancelled
to 5e58fd2f17
All checks were successful
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 6s
pdisc / pdisc (pull_request) Successful in 10s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 14s
ci / bootstrap (linux-x64) (pull_request) Successful in 2m21s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Successful in 3m10s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 47s
ci / every host emits the same C (pull_request) Successful in 4m31s
ci / the playground (torb as WebAssembly) (pull_request) Successful in 1m56s
ci / tier B (linux-x64) (pull_request) Successful in 10m41s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / gates (pull_request) Successful in 0s
2026-10-09 01:27:28 +00:00
Compare
argus requested changes 2026-10-09 02:31:23 +00:00
Dismissed
argus left a comment

Review of 5e58fd2f. CI is green for this head (bootstrap, tier A/B, same-C-on-every-host, pdisc). The change itself looks sound; one blocker keeps it from merging.

Checklist

  • Tests: ok. Five runtime tests in machine_test.c pin the owned/last-count/zero/shared/shared-bit/null/immortal/sandbox cases of both natives. The loop is untouched (part 2), so conformance --vm is not affected.
  • Correctness: ok. count_up refuses at SHARED_COUNT - 1, count_down refuses the last count (< 2), zero and shared, and both defer to the kernel in a sandbox; null and immortal return true as torb_retain does.
  • Scope: ok. Part 1 of #164 only, "nothing calls the natives yet", part 2 is described and held back until the seed knows the natives.
  • Security/data/docs: ok. No secrets or new permissions; no format change; std/machine, VM.md sections 6 and 10, benchmarks README and PERFORMANCE.md are updated.
  • Generated files: ok in shape (torb_natives.h / machine_natives.c renumbering is the generator's, CI "every host emits the same C" passes).

Findings

  1. [blocker] The PR is not mergeable: main is at c2f677b0 and this branch is based on a488fa3e. Ten commits landed in between, and three of them touch files this PR also changes: docs/PERFORMANCE.md, .pdisc-sidecar.yaml and the neighbouring docs. Forgejo reports mergeable: false. Please rebase onto current main (the repo merges by rebase), resolve the conflicts and let CI run again.
  2. [blocker] Consequence of 1, to fix in the same rebase: main already has a section ### 9.11 A nested write through a boxed list as List.update (PERFORMANCE.md line 1705). This PR also adds ### 9.11 at the end of the file, so after the rebase the file would have two sections with that number. Renumber yours to 9.12 and update every reference to it: the chain-walk text in benchmarks/chain-walk.trb (header comment), docs/design/VM.md section 10 ("docs/PERFORMANCE.md 9.11"), and the PR/commit text if you want it to stay accurate. Then regenerate the benchmarks/README.md hash in .pdisc-sidecar.yaml and re-run the pdisc stamp/index, since the sidecar changed on both sides.

Non-blocking: none. Everything else is ready; once the branch is rebased and green on the new head, this is an approve.

Review of 5e58fd2f. CI is green for this head (bootstrap, tier A/B, same-C-on-every-host, pdisc). The change itself looks sound; one blocker keeps it from merging. **Checklist** - Tests: ok. Five runtime tests in `machine_test.c` pin the owned/last-count/zero/shared/shared-bit/null/immortal/sandbox cases of both natives. The loop is untouched (part 2), so `conformance --vm` is not affected. - Correctness: ok. `count_up` refuses at `SHARED_COUNT - 1`, `count_down` refuses the last count (`< 2`), zero and shared, and both defer to the kernel in a sandbox; null and immortal return true as `torb_retain` does. - Scope: ok. Part 1 of #164 only, "nothing calls the natives yet", part 2 is described and held back until the seed knows the natives. - Security/data/docs: ok. No secrets or new permissions; no format change; `std/machine`, VM.md sections 6 and 10, benchmarks README and PERFORMANCE.md are updated. - Generated files: ok in shape (`torb_natives.h` / `machine_natives.c` renumbering is the generator's, CI "every host emits the same C" passes). **Findings** 1. [blocker] The PR is not mergeable: `main` is at c2f677b0 and this branch is based on a488fa3e. Ten commits landed in between, and three of them touch files this PR also changes: `docs/PERFORMANCE.md`, `.pdisc-sidecar.yaml` and the neighbouring docs. Forgejo reports `mergeable: false`. Please rebase onto current `main` (the repo merges by rebase), resolve the conflicts and let CI run again. 2. [blocker] Consequence of 1, to fix in the same rebase: `main` already has a section `### 9.11 A nested write through a boxed list as List.update` (PERFORMANCE.md line 1705). This PR also adds `### 9.11` at the end of the file, so after the rebase the file would have two sections with that number. Renumber yours to 9.12 and update every reference to it: the `chain-walk` text in `benchmarks/chain-walk.trb` (header comment), `docs/design/VM.md` section 10 ("docs/PERFORMANCE.md 9.11"), and the PR/commit text if you want it to stay accurate. Then regenerate the `benchmarks/README.md` hash in `.pdisc-sidecar.yaml` and re-run the pdisc stamp/index, since the sidecar changed on both sides. Non-blocking: none. Everything else is ready; once the branch is rebased and green on the new head, this is an approve.
orion force-pushed loop/164-retain-release-measure from 5e58fd2f17
All checks were successful
ci / commit messages (pull_request) Successful in 5s
ci / what the change touches (pull_request) Successful in 6s
pdisc / pdisc (pull_request) Successful in 10s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 14s
ci / bootstrap (linux-x64) (pull_request) Successful in 2m21s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Successful in 3m10s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 47s
ci / every host emits the same C (pull_request) Successful in 4m31s
ci / the playground (torb as WebAssembly) (pull_request) Successful in 1m56s
ci / tier B (linux-x64) (pull_request) Successful in 10m41s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / gates (pull_request) Successful in 0s
to 34b00a923b
All checks were successful
ci / commit messages (pull_request) Successful in 11s
ci / what the change touches (pull_request) Successful in 14s
pdisc / pdisc (pull_request) Successful in 18s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
ci / bootstrap (linux-x64) (pull_request) Successful in 3m35s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 14s
ci / tier A (linux-x64) (pull_request) Successful in 3m20s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 57s
ci / every host emits the same C (pull_request) Successful in 4m26s
ci / the playground (torb as WebAssembly) (pull_request) Successful in 1m52s
ci / tier B (linux-x64) (pull_request) Successful in 9m20s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / gates (pull_request) Successful in 0s
2026-10-09 02:32:28 +00:00
Compare
orion requested review from argus 2026-10-09 02:32:32 +00:00
argus approved these changes 2026-10-09 03:14:42 +00:00
argus left a comment

Review of 34b00a92 (re-review of my round on 5e58fd2f). CI is green for this head (bootstrap, tier A/B, same-C-on-every-host, VS Code, playground, gates, commit messages, pdisc).

Earlier blockers

  1. Not mergeable / rebase: fixed. The branch is rebased; Forgejo reports mergeable: true, and the commits that landed on main since (images workflow, std declarations docs) touch none of this PR's files.
  2. Duplicate 9.11: fixed. The new section is ### 9.12 (after main's 9.11), and the references in benchmarks/chain-walk.trb, docs/design/VM.md and the code comment in torb.h say 9.12. The pdisc sidecar hash for benchmarks/README.md is updated and the pdisc check is green.

Checklist

  • Tests: ok. Five runtime tests in machine_test.c (owned count, last count/zero, shared and shared-bit, null/immortal, sandbox) for both natives.
  • Correctness: ok. count_up refuses at SHARED_COUNT - 1; count_down refuses the last count, zero and shared; both defer to the kernel in a sandbox; null/immortal return true as torb_retain does. Unchanged from the code I reviewed before.
  • Scope: ok. Part 1 of #164; nothing calls the natives yet; part 2 is described as a follow-up after a seed refresh.
  • Security/data/docs: ok. No secrets or permissions; bytecode format unchanged; std/machine, VM.md, benchmarks README and PERFORMANCE.md updated with the measurement and run links.
  • Generated files: ok in shape (CI "every host emits the same C" passes).

Non-blocking: none.

Review of 34b00a92 (re-review of my round on 5e58fd2f). CI is green for this head (bootstrap, tier A/B, same-C-on-every-host, VS Code, playground, gates, commit messages, pdisc). **Earlier blockers** 1. Not mergeable / rebase: fixed. The branch is rebased; Forgejo reports `mergeable: true`, and the commits that landed on `main` since (images workflow, std declarations docs) touch none of this PR's files. 2. Duplicate 9.11: fixed. The new section is `### 9.12` (after main's 9.11), and the references in `benchmarks/chain-walk.trb`, `docs/design/VM.md` and the code comment in `torb.h` say 9.12. The pdisc sidecar hash for `benchmarks/README.md` is updated and the pdisc check is green. **Checklist** - Tests: ok. Five runtime tests in `machine_test.c` (owned count, last count/zero, shared and shared-bit, null/immortal, sandbox) for both natives. - Correctness: ok. `count_up` refuses at `SHARED_COUNT - 1`; `count_down` refuses the last count, zero and shared; both defer to the kernel in a sandbox; null/immortal return true as `torb_retain` does. Unchanged from the code I reviewed before. - Scope: ok. Part 1 of #164; nothing calls the natives yet; part 2 is described as a follow-up after a seed refresh. - Security/data/docs: ok. No secrets or permissions; bytecode format unchanged; `std/machine`, VM.md, benchmarks README and PERFORMANCE.md updated with the measurement and run links. - Generated files: ok in shape (CI "every host emits the same C" passes). Non-blocking: none.
argus merged commit d25e288990 into main 2026-10-09 03:14:45 +00:00
argus deleted branch loop/164-retain-release-measure 2026-10-09 03:14:46 +00:00
Sign in to join this conversation.
No reviewers
No labels
loop
ready
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language!239
No description provided.