Skip to content

perf(ts): share Ok(()) and Err(()) as one frozen object - #123

Merged
kixelated merged 6 commits into
mainfrom
quest/m2/ts-const-share
Oct 27, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m2/ts-const-share

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A result whose payload is undefined, such as Ok(()), Ok(None), ControlFlow::Continue(()), or Err(()), allocated a new { ok, value } on every evaluation. moq-net's encoder allocates one per successful write.

Approach

These results now lower to rt.OK or rt.ERR: one frozen runtime object shared by every evaluation. A successful write in moq-net's encoder now allocates no Result.

No write analysis is needed. Nothing writes into an rt.Result in place, because a store into its payload makes the result again (set = rt.Ok(a)), and undefined holds nothing to write into. Expr::result makes the choice wherever lowering builds a result. subst makes it again when a held-back temporary folds in as undefined, so no rt.Ok(undefined) is left in any snapshot. The runtime's private written (its own shared Ok(undefined) for formatting) becomes OK.

Consumer: moq-net's encoder (Encoder::varint, varint::write), via the codec fixture.

Measurements

just ts bench, this branch as translated against main as --route main, geometric mean of translated ÷ main:

engine encode decode A/A ÷ translated (encode)
Chrome 0.90 0.96 0.98
Firefox 0.75 1.02 1.01
Bun 0.75 0.93 0.94
Node 1.05 1.03 0.94 (noise)

Bundle: 31676 B raw / 9965 B gzipped, against main's 31729 / 9943.

Tests

  • lang gains a shared Ok(None) whose payload is set through a ref mut. The store rebuilds the result, and the next unset() still reads None. Writing into the frozen object would throw and fail the differential test.
  • All snapshot, differential, tsc, and bundle tests pass, as does just check.

Impact

  • Additive: the runtime exports rt.OK and rt.ERR, and translated functions return them for unit results. Callers now get the same frozen object for every Ok(()). rt.Result was already readonly, and the README already says callers must not change what they get in place, so there is no MIGRATE.md entry.

Alternatives

The quest planned to share all constant aggregates; only the kinds that measure were kept.

  • Results of other constants (Err(DecodeError::Short), Ok(true), Err(BoundsExceeded)): implemented as frozen lib.ts constants gated on the Copy write analysis. An A/B run against this version measured 0.95-0.98 on the Short-retry decodes in Node, Bun, and Chrome (1.09 in Firefox), and within A/A elsewhere. No gain, so they were removed, along with a new Item::Const, SymKind::Const, /* @__PURE__ */ printing, naming, and the write-analysis coupling. That earlier run measured Chrome encode at 0.96 (A/A 0.97), Firefox 0.81, Bun 0.93, and Node 0.97.
  • Bare enum and struct constants ({ tag: "Short" }) and u64 limbs: the spike already measured these as saving no allocation in V8.
  • const arrays: no bench workload uses them. A shared array is also unsafe here, since a Vec can take a moved array as its storage, and writes into a Vec don't count as writes into [T].
  • rt.fmtSpec objects: no bench workload formats.

The maintainer accepted this scope and the names rt.OK and rt.ERR (constant case like rt.DONE).

Follow-ups

None needed. If a workload ever formats in a hot loop, sharing rt.fmtSpec objects could be measured then.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3

(written by Claude Opus 5.5)

kixelated and others added 3 commits October 27, 2026 13:29
Quest-Claim: d4fa3787aad4e9fe
A result whose payload is `undefined`, such as `Ok(())`, `Ok(None)`, or
`ControlFlow::Continue(())`, is now `rt.OK` or `rt.ERR`, one frozen object
of the runtime, instead of a new object per evaluation. Nothing writes into
a result in place (a store into its payload makes the result again) and
`undefined` holds nothing to write into, so no analysis is needed.

`just ts bench` against main: encode at 0.90x in Chrome (A/A within 0.03),
0.75x in Firefox and Bun, Node within noise. Sharing results of other
constants, such as `Err(DecodeError::Short)`, measured no faster in an A/B
run, so they are made where evaluated. The runtime's private `written`
becomes `OK`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
@kixelated kixelated changed the title Share constant aggregates perf(ts): share Ok(()) and Err(()) as one frozen object Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 21:28
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T22:21:56.694115Z 0f5d6d1 New commits
🔒 Security Review ✅ Completed 2026-10-27T22:22:49.213220Z 0f5d6d1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 65d8138

No actionable bugs found. The narrow undefined-payload optimization is a sound direction: it preserves the Result discriminator, and payload stores rebuild the enclosing result rather than mutate the shared frozen object. Centralizing construction in Expr::result, including after substitution, keeps this simpler than extending write analysis or introducing general constant pooling. The new Ok(None) mutation regression exercises the important aliasing boundary.

Verification: static review of all changed files and relevant result-store/substitution/runtime code. I did not run tests or independently reproduce the benchmark measurements.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary:

(written by Claude Opus 5.5)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant