Repository navigation
[JSC] A StringObject with immutable properties keeps the compilers' fast conversion to a string #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dylan-conway
wants to merge
4
commits into
main
Choose a base branch
from
claude/immutable-string-object-add
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[JSC] A StringObject with immutable properties keeps the compilers' fast conversion to a string #765
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e5f0eb
[JSC] A StringObject with immutable properties keeps the compilers' f…
dylan-conway ba43cb2
[JSC] The check for an untouched StringObject is for the realm of the…
dylan-conway 74c903e
immutable-properties-string-object.js: three parts that could not fai…
dylan-conway 7a0253d
Merge branch 'main' into claude/immutable-string-object-add
dylan-conway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| // The optimizing compilers turn a StringObject into its string without a call where they can check that nobody has touched the object, | ||
| // which they do by its structure. A StringObject whose properties were made immutable passes that check too: the operations below | ||
| // give the same results for it, and a site that sees one is not reoptimized for it. | ||
|
|
||
| function shouldBe(actual, expected, message) { | ||
| if (actual !== expected) | ||
| throw new Error((message ? message + ": " : "") + "expected " + String(expected) + " but got " + String(actual)); | ||
| } | ||
|
|
||
| // Enough calls for a site to be compiled, and then to exit often enough to be reoptimized if it is going to be. | ||
| let hot = Math.ceil(testLoopCount / 4); | ||
|
|
||
| let operations = { | ||
| add: "object + suffix", | ||
| addLeft: "suffix + object", | ||
| template: "`${object}${suffix}`", | ||
| stringCall: "String(object) + suffix", | ||
| toStringCall: "object.toString() + suffix", | ||
| valueOfCall: "object.valueOf() + suffix", | ||
| charAt: "object.charAt(1) + suffix", | ||
| index: "object[1] + suffix", | ||
| length: "object.length + suffix", | ||
| equals: "(object == 'abc') + suffix", | ||
| concat: "object.concat(suffix)", | ||
| }; | ||
|
|
||
| // (Functions with the same source share what the compilers have learnt about it, exits included: each site gets a source of its own.) | ||
| let sites = 0; | ||
| function makeSite(source) { | ||
| let site = new Function("object", "suffix", "return " + source + "; // site " + ++sites); | ||
| noInline(site); | ||
| return site; | ||
| } | ||
|
|
||
| function run(site, pick, expected, label) { | ||
| for (let i = 0; i < hot; ++i) | ||
| shouldBe(site(pick(i), "-" + (i & 3)), expected(i), label); | ||
| } | ||
|
|
||
| // The realm's first such object appears when sites are hot already. Their code is thrown away, once, and no exit is held against them. | ||
| { | ||
| let hotSites = Object.values(operations).map(makeSite), ordinary = new String("abc"); | ||
| let results = hotSites.map(site => { run(site, () => ordinary, i => site(ordinary, "-" + (i & 3))); return site(ordinary, "!"); }); | ||
| let immutable = $vm.makePropertiesImmutable(new String("abc")); | ||
| hotSites.forEach((site, index) => { | ||
| shouldBe(site(immutable, "!"), results[index]); | ||
| run(site, i => i & 1 ? immutable : ordinary, i => site(ordinary, "-" + (i & 3))); | ||
| }); | ||
| } | ||
|
|
||
| // A call of a method looks the method up by the object's structure first, and a site learns a second structure as it does for any | ||
| // other object, by being reoptimized if it was compiled too early. Those are here for their results. | ||
| let looksUpAMethod = new Set(["toStringCall", "valueOfCall", "charAt", "concat"]); | ||
|
|
||
| for (let [name, source] of Object.entries(operations)) { | ||
| let reference = makeSite(source); | ||
| let expected = i => reference(new String("abc"), "-" + (i & 3)); | ||
|
|
||
| // 1. The site is hot on ordinary objects, and then sees objects with immutable properties. | ||
| let later = makeSite(source), ordinary = new String("abc"); | ||
| run(later, () => ordinary, expected, name + ", ordinary"); | ||
| let immutable = $vm.makePropertiesImmutable(new String("abc")); | ||
| run(later, () => immutable, expected, name + ", immutable"); | ||
| run(later, i => i & 1 ? immutable : ordinary, expected, name + ", both"); | ||
|
|
||
| // 2. The site sees both from the start. | ||
| let fromTheStart = makeSite(source); | ||
| run(fromTheStart, i => i & 1 ? immutable : ordinary, expected, name + ", both from the start"); | ||
|
|
||
| // 3. The same number of calls with ordinary objects alone: whatever reoptimization this configuration causes by itself. | ||
| let control = makeSite(source); | ||
| for (let round = 0; round < 3; ++round) | ||
| run(control, () => ordinary, expected, name + ", control"); | ||
|
|
||
| if (!looksUpAMethod.has(name)) { | ||
| shouldBe(reoptimizationRetryCount(later) <= reoptimizationRetryCount(control), true, name + ": reoptimized for an object with immutable properties, which it saw later"); | ||
| shouldBe(reoptimizationRetryCount(fromTheStart) <= reoptimizationRetryCount(control), true, name + ": reoptimized for an object with immutable properties"); | ||
| } | ||
| shouldBe($vm.hasImmutableProperties(immutable), true); | ||
| } | ||
|
|
||
| // What the check is for still holds. An object that was touched first does not pass it, immutable properties or not. | ||
| { | ||
| let site = makeSite("object + suffix"); | ||
| let ordinary = new String("abc"); | ||
| let withOwnToString = new String("abc"); | ||
| withOwnToString.toString = () => "own toString"; | ||
| withOwnToString.valueOf = () => "own valueOf"; | ||
| $vm.makePropertiesImmutable(withOwnToString); | ||
| let withOtherPrototype = Object.setPrototypeOf(new String("abc"), { __proto__: String.prototype, valueOf() { return "inherited valueOf"; } }); | ||
| $vm.makePropertiesImmutable(withOtherPrototype); | ||
| class Derived extends String { valueOf() { return "derived valueOf"; } } | ||
| let derived = $vm.makePropertiesImmutable(new Derived("abc")); | ||
| for (let i = 0; i < hot; ++i) { | ||
| shouldBe(site(ordinary, "!"), "abc!"); | ||
| shouldBe(site(withOwnToString, "!"), "own valueOf!"); | ||
| shouldBe(site(withOtherPrototype, "!"), "inherited valueOf!"); | ||
| shouldBe(site(derived, "!"), "derived valueOf!"); | ||
| } | ||
| } | ||
|
|
||
| // Each realm has its own. | ||
| { | ||
| let other = $vm.createGlobalObject(); | ||
| let site = makeSite("object + suffix"); | ||
| let objects = [new String("abc"), $vm.makePropertiesImmutable(new String("abc")), new other.String("abc"), $vm.makePropertiesImmutable(new other.String("abc"))]; | ||
| for (let i = 0; i < hot; ++i) | ||
| shouldBe(site(objects[i & 3], "!"), "abc!"); | ||
| } | ||
|
|
||
| // The object comes from an inlined function of another realm: it is that realm's String.prototype that counts, which the site, in this | ||
| // realm, does not watch. Such an object, immutable properties or not, is not taken for one of this realm's. | ||
| for (let change of [object => object, object => $vm.makePropertiesImmutable(object)]) { | ||
| let other = $vm.createGlobalObject(); | ||
| change(new other.String("abc")); | ||
| let object = change(new other.String("abc")); | ||
| let fresh = other.Function("return new String('abc'); // site " + ++sites); | ||
| let existing = other.Function("object", "return object; // site " + ++sites); | ||
| let addFresh = new Function("make", "suffix", "return make() + suffix; // site " + ++sites); | ||
| let addExisting = new Function("make", "object", "suffix", "return make(object) + suffix; // site " + ++sites); | ||
| noInline(addFresh); | ||
| noInline(addExisting); | ||
| for (let i = 0; i < hot; ++i) { | ||
| shouldBe(addFresh(fresh, "!"), "abc!"); | ||
|
dylan-conway marked this conversation as resolved.
Resolved
|
||
| shouldBe(addExisting(existing, object, "!"), "abc!"); | ||
| } | ||
| other.String.prototype.valueOf = other.Function("return 'replaced';"); | ||
| for (let i = 0; i < hot; ++i) { | ||
| shouldBe(addFresh(fresh, "!"), "replaced!"); | ||
| shouldBe(addExisting(existing, object, "!"), "replaced!"); | ||
| } | ||
| } | ||
|
|
||
| // A change to String.prototype reaches such an object, hot, as it reaches any other. | ||
| { | ||
| let site = makeSite("object + suffix"); | ||
| let immutable = $vm.makePropertiesImmutable(new String("abc")); | ||
| for (let i = 0; i < hot; ++i) | ||
| shouldBe(site(immutable, "!"), "abc!"); | ||
| let valueOf = String.prototype.valueOf; | ||
| String.prototype.valueOf = function () { return "replaced"; }; | ||
| for (let i = 0; i < hot; ++i) | ||
| shouldBe(site(immutable, "!"), "replaced!"); | ||
| String.prototype.valueOf = valueOf; | ||
| shouldBe(site(immutable, "!"), "abc!"); | ||
| } | ||
|
|
||
| // The structure is kept: after every such object is gone, a new one gets the structure compiled code checks for. | ||
| { | ||
| let site = makeSite("object + suffix"); | ||
| for (let round = 0; round < 3; ++round) { | ||
| for (let i = 0; i < hot; ++i) | ||
| shouldBe(site($vm.makePropertiesImmutable(new String("abc")), "!"), "abc!"); | ||
| fullGC(); | ||
| } | ||
| let control = makeSite("object + suffix"); | ||
| for (let round = 0; round < 3; ++round) { | ||
| for (let i = 0; i < hot; ++i) | ||
| shouldBe(control(new String("abc"), "!"), "abc!"); | ||
| fullGC(); | ||
| } | ||
| shouldBe(reoptimizationRetryCount(site) <= reoptimizationRetryCount(control), true, "reoptimized after a collection"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.