Repository navigation
perf(ts): share Ok(()) and Err(()) as one frozen object - #123
Conversation
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
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
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.
|
Merge summary:
(written by Claude Opus 5.5) |
Problem
A result whose payload is
undefined, such asOk(()),Ok(None),ControlFlow::Continue(()), orErr(()), allocated a new{ ok, value }on every evaluation. moq-net's encoder allocates one per successful write.Approach
These results now lower to
rt.OKorrt.ERR: one frozen runtime object shared by every evaluation. A successful write in moq-net's encoder now allocates noResult.No write analysis is needed. Nothing writes into an
rt.Resultin place, because a store into its payload makes the result again (set = rt.Ok(a)), andundefinedholds nothing to write into.Expr::resultmakes the choice wherever lowering builds a result.substmakes it again when a held-back temporary folds in asundefined, so nort.Ok(undefined)is left in any snapshot. The runtime's privatewritten(its own sharedOk(undefined)for formatting) becomesOK.Consumer: moq-net's encoder (
Encoder::varint,varint::write), via the codec fixture.Measurements
just ts bench, this branch astranslatedagainst main as--route main, geometric mean oftranslated ÷ main:Bundle: 31676 B raw / 9965 B gzipped, against main's 31729 / 9943.
Tests
langgains a sharedOk(None)whose payload is set through aref mut. The store rebuilds the result, and the nextunset()still readsNone. Writing into the frozen object would throw and fail the differential test.tsc, and bundle tests pass, as doesjust check.Impact
rt.OKandrt.ERR, and translated functions return them for unit results. Callers now get the same frozen object for everyOk(()).rt.Resultwas alreadyreadonly, and the README already says callers must not change what they get in place, so there is noMIGRATE.mdentry.Alternatives
The quest planned to share all constant aggregates; only the kinds that measure were kept.
Err(DecodeError::Short),Ok(true),Err(BoundsExceeded)): implemented as frozenlib.tsconstants gated on theCopywrite analysis. An A/B run against this version measured 0.95-0.98 on theShort-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 newItem::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.{ tag: "Short" }) andu64limbs: the spike already measured these as saving no allocation in V8.constarrays: no bench workload uses them. A shared array is also unsafe here, since aVeccan take a moved array as its storage, and writes into aVecdon't count as writes into[T].rt.fmtSpecobjects: no bench workload formats.The maintainer accepted this scope and the names
rt.OKandrt.ERR(constant case likert.DONE).Follow-ups
None needed. If a workload ever formats in a hot loop, sharing
rt.fmtSpecobjects could be measured then.🤖 Generated with Claude Code
https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
(written by Claude Opus 5.5)