Reuse one frozen object per depth for a nested None
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.
modifiedRepresentation nested None
representation
Before
After
None ↦ undefinedNone ↦ undefinedSome(None) ↦ a new {BS_PRIVATE_NESTED_SOME_NONE: 0} per callSome(Some(None)) ↦ a new {BS_PRIVATE_NESTED_SOME_NONE: 1} per call
noteSome(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}noteSome(None) === Some(None) is true
modifiedJS Primitive_option.some
js
Before
After
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) {function some(x) { if (x === undefined) { if (x === undefined) { return { BS_PRIVATE_NESTED_SOME_NONE: 0 }; return marker(0); } else if (x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined) { } 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 { } else { return x; 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.
- Why freeze? The objects are now shared, so a write through Obj.magic would change every nested None of that depth.