Repository navigation
Reuse one frozen object per depth for a nested None - #9025
Conversation
Primitive_option.some and valFromOption created a new
{BS_PRIVATE_NESTED_SOME_NONE: depth} object on every call. That allocates
each time a nested None is wrapped or unwrapped, and makes two equal
values different objects: Some(None) === Some(None) was false while
Some(1) === Some(1) is true. Each depth now has one frozen object,
created on first use.
Representation modified (nested None):
Before (representation):
None ↦ undefined
Some(None) ↦ a new {BS_PRIVATE_NESTED_SOME_NONE: 0} per call
Some(Some(None)) ↦ a new {BS_PRIVATE_NESTED_SOME_NONE: 1} per call
note: Some(None) === Some(None) is false
After (representation):
None ↦ undefined
Some(None) ↦ the shared frozen {BS_PRIVATE_NESTED_SOME_NONE: 0}
Some(Some(None)) ↦ the shared frozen {BS_PRIVATE_NESTED_SOME_NONE: 1}
note: Some(None) === Some(None) is true
JS modified (Primitive_option.some):
Before (js):
function some(x) {
if (x === undefined) {
return {
BS_PRIVATE_NESTED_SOME_NONE: 0
};
} else if (x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined) {
return {
BS_PRIVATE_NESTED_SOME_NONE: x.BS_PRIVATE_NESTED_SOME_NONE + 1 | 0
};
} else {
return x;
}
}
After (js):
let markers = [];
function marker(depth) {
let m = markers[depth];
if (m !== undefined) {
return m;
}
let m$1 = Object.freeze({
BS_PRIVATE_NESTED_SOME_NONE: depth
});
markers[depth] = m$1;
return m$1;
}
function some(x) {
if (x === undefined) {
return marker(0);
} else if (x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined) {
return marker(x.BS_PRIVATE_NESTED_SOME_NONE + 1 | 0);
} else {
return x;
}
}
Review: Does this work with two copies of the runtime, e.g. in two
bundles? Yes: a value is recognised by its property, not by identity, so
each copy's objects work with the other copy; only === between them stays
false, as before.
Review: Why freeze? The objects are now shared, so a write through
Obj.magic would change every nested None of that depth.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C
Signed-off-by: Christoph Knittel <ck@cca.io>
Changelog💅 Polish
Semantic Diff1 commit · Full page Reuse one frozen object per depth for a nested Nonefbb13e4 · cknitt + Claude · Oct 24, 2026, 17:15 UTC Primitive_option.some and valFromOption created a new {BS_PRIVATE_NESTED_SOME_NONE: depth} object on every call. That allocates each time a nested None is wrapped or unwrapped, and makes two equal values different objects: Some(None) === Some(None) was false while Some(1) === Some(1) is true. Each depth now has one frozen object, created on first use. Representation modified (nested None)
None ↦ undefined
-Some(None) ↦ a new {BS_PRIVATE_NESTED_SOME_NONE: 0} per call
-Some(Some(None)) ↦ a new {BS_PRIVATE_NESTED_SOME_NONE: 1} per call
-note: Some(None) === Some(None) is false
+Some(None) ↦ the shared frozen {BS_PRIVATE_NESTED_SOME_NONE: 0}
+Some(Some(None)) ↦ the shared frozen {BS_PRIVATE_NESTED_SOME_NONE: 1}
+note: Some(None) === Some(None) is trueJS modified (Primitive_option.some)
+let markers = [];
+
+function marker(depth) {
+ let m = markers[depth];
+ if (m !== undefined) {
+ return m;
+ }
+ let m$1 = Object.freeze({
+ BS_PRIVATE_NESTED_SOME_NONE: depth
+ });
+ markers[depth] = m$1;
+ return m$1;
+}
+
function some(x) {
if (x === undefined) {
- return {
- BS_PRIVATE_NESTED_SOME_NONE: 0
- };
+ return marker(0);
} else if (x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined) {
- return {
- BS_PRIVATE_NESTED_SOME_NONE: x.BS_PRIVATE_NESTED_SOME_NONE + 1 | 0
- };
+ return marker(x.BS_PRIVATE_NESTED_SOME_NONE + 1 | 0);
} else {
return x;
}
}Review
|
|
@codex review |
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. |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9025 +/- ##
==========================================
+ Coverage 81.29% 81.31% +0.02%
==========================================
Files 456 456
Lines 63836 64006 +170
==========================================
+ Hits 51893 52047 +154
- Misses 11943 11959 +16 🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9025 |
Primitive_option.someandvalFromOptioncreated a new{BS_PRIVATE_NESTED_SOME_NONE: depth}object on every call. This had two effects:Noneallocated each time. Generic code such asOption.mapover anoption<option<_>>runs through these functions.Some(None) === Some(None)wasfalse, whileSome(1) === Some(1)istrue.Now each depth has one frozen object, created on first use and returned by both functions:
Generated code doesn't change: it calls the same two functions. Values are still recognised by the
BS_PRIVATE_NESTED_SOME_NONEproperty, so two copies of the runtime, e.g. in two bundles, still work with each other's values. Only===between their objects staysfalse, as before. The objects are frozen because they are now shared, so a write throughObj.magicwould otherwise affect every nestedNoneof that depth.New test
option_nested_marker_test.res: identity at each depth, unwrapping to the marker one level down, frozen markers, and unchanged matching and equality.🤖 Generated with Claude Code
https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C