Optimization: execute const object bodies and typechecks only once (#915)

If the object member is const, it only needs to be executed once, and all children in the prototype chain can use its cached value.

There is a possible other optimization here, not included in this PR: we can avoid adding these values to the child object's cachedMembers.
This commit is contained in:
Daniel Chao
2025-01-29 07:19:55 -08:00
committed by GitHub
parent 90df0662af
commit 3815a0206b
3 changed files with 37 additions and 0 deletions
@@ -254,6 +254,18 @@ public final class VmUtils {
IndirectCallNode callNode) {
final var constantValue = member.getConstantValue();
// const members only need to be executed once on the prototype, and its cached value
// can be re-used for all children in the amends chain.
if (member.isConst() && owner != receiver) {
assert member.isProp();
assert owner.isPrototype();
var result = readMemberOrNull(owner, memberKey, checkType, callNode);
assert result != null;
receiver.setCachedValue(memberKey, result);
return result;
}
if (constantValue != null) {
var result = constantValue;
// for a property, Listing element, or Mapping value, do a type check