Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/runtime/VmListingOrMapping.java (Java)

Code before:
      return delegate.findMember(key);
    }
    // member is guaranteed to exist; this is only called if `getCachedValue()` returns non-null
    // and `setCachedValue` will record the object member in `cachedMembers`.
    throw PklBugException.unreachableCode();
  }

  public @Nullable ListingOrMappingTypeCastNode getTypeCastNode() {
    return typeCastNode;
  }

  @Override
  public void setCachedValue(Object key, Object value, ObjectMember objectMember) {
    super.setCachedValue(key, value, objectMember);
    EconomicMaps.put(cachedMembers, key, objectMember);
  }

  @Override
  public boolean hasCachedValue(Object key) {
    if (super.hasCachedValue(key)) {
      return true;
    }
    if (delegate != null) {
      return delegate.hasCachedValue(key);
    }
    return false;
  }

  @Override
  public @Nullable Object getCachedValue(Object key) {
    var myCachedValue = super.getCachedValue(key);
    if (myCachedValue != null || delegate == null) {
      return myCachedValue;
    }
    var memberValue = delegate.getCachedValue(key);
    // if this object member appears inside `checkedMembers`, we have already checked its type
    // and can safely return it.
    if (EconomicSets.contains(checkedMembers, key)) {
      return memberValue;
    }
    if (memberValue == null) {
      return null;
    }
    // If a cached value already exists on the delegate, run a typecast on it.
    // optimization: don't use `VmUtils.findMember` to avoid iterating over all members
    var objectMember = findMember(key);
    var ret = typecastObjectMember(objectMember, memberValue, IndirectCallNode.getUncached());
    if (ret != memberValue) {
      EconomicMaps.put(cachedValues, key, ret);
    } else {
      // optimization: don't add to own cached values if typecast results in the same value

Review comment (performance):
I've not looked at JVM and/or `native-image` branch elimination optimisations in forever, but I'm still inclined to generally formulate with fewer branch points.
```suggestion
    return super.hasCachedValue(key) || delegate != null && delegate.hasCachedValue(key);
```

Code after:
      return delegate.findMember(key);
    }
    // member is guaranteed to exist; this is only called if `getCachedValue()` returns non-null
    // and `setCachedValue` will record the object member in `cachedMembers`.
    throw PklBugException.unreachableCode();
  }

  public @Nullable ListingOrMappingTypeCastNode getTypeCastNode() {
    return typeCastNode;
  }

  @Override
  public void setCachedValue(Object key, Object value, ObjectMember objectMember) {
    super.setCachedValue(key, value, objectMember);
    EconomicMaps.put(cachedMembers, key, objectMember);
  }

  @Override
  public boolean hasCachedValue(Object key) {
    return super.hasCachedValue(key) || delegate != null && delegate.hasCachedValue(key);
  }

  @Override
  public @Nullable Object getCachedValue(Object key) {
    var myCachedValue = super.getCachedValue(key);
    if (myCachedValue != null || delegate == null) {
      return myCachedValue;
    }
    var memberValue = delegate.getCachedValue(key);
    // if this object member appears inside `checkedMembers`, we have already checked its type
    // and can safely return it.
    if (EconomicSets.contains(checkedMembers, key)) {
      return memberValue;
    }
    if (memberValue == null) {
      return null;
    }
    // If a cached value already exists on the delegate, run a typecast on it.
    // optimization: don't use `VmUtils.findMember` to avoid iterating over all members
    var objectMember = findMember(key);
    var ret = typecastObjectMember(objectMember, memberValue, IndirectCallNode.getUncached());
    if (ret != memberValue) {
      EconomicMaps.put(cachedValues, key, ret);
    } else {
      // optimization: don't add to own cached values if typecast results in the same value
      EconomicSets.add(checkedMembers, key);
    }
    return ret;
  }

  @Override