mirror of
https://github.com/apple/pkl.git
synced 2026-07-01 18:51:48 +02:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user