-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify VmListingOrMapping data structure and code
Motivation: As part of implementing lazy type checking of listings and mappings, major modifications were made to VmListingOrMapping and its subclasses. I believe a simpler implementation is possible and desirable. Changes: - Implement listing/mapping type cast via amendment (`parent`) instead of delegation (`delegate`). This is the key idea. - handle type checking of *computed* elements/entries in the same way as type checking of computed properties - ElementOrEntryNode is the equivalent of TypeCheckedPropertyNode - remove fields VmListingOrMapping.delegate/typeNodeFrame/cachedMembers/checkedMembers - remove overrides of VmObject methods that are no longer required - having a single implementation simplifies code and helps JITs Result: - simpler code that will be easier to optimize - For example, restoring the invariant that a shallow-forced object has a fully populated cachedValues map can be exploited to speed up iteration (not part of this PR). - smaller memory footprint - better performance in some cases - fixes apple#785 Examples for potential future optimizations: - avoid type check overhead for untyped listings/mappings - make forcing a typed listing/mapping more efficient - currently, the parent chain is traversed once per member for type checking, which nullifies much of the performance advantage that shallow-forcing an object has over computing each value individually - avoid creating an intermediate untyped listing/mapping in the following cases: - new Listing<X> {...} - `property: Listing<X>` is amended
- Loading branch information
Showing
21 changed files
with
231 additions
and
276 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
61 changes: 61 additions & 0 deletions
61
pkl-core/src/main/java/org/pkl/core/ast/member/ElementOrEntryNode.java
This file contains 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,61 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.pkl.core.ast.member; | ||
|
||
import com.oracle.truffle.api.dsl.Executed; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.frame.FrameDescriptor; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.nodes.IndirectCallNode; | ||
import org.pkl.core.ast.ExpressionNode; | ||
import org.pkl.core.ast.expression.primary.GetReceiverNode; | ||
import org.pkl.core.runtime.VmDynamic; | ||
import org.pkl.core.runtime.VmLanguage; | ||
import org.pkl.core.runtime.VmListing; | ||
import org.pkl.core.runtime.VmMapping; | ||
import org.pkl.core.runtime.VmUtils; | ||
import org.pkl.core.util.Nullable; | ||
|
||
public abstract class ElementOrEntryNode extends RegularMemberNode { | ||
@Child @Executed protected ExpressionNode receiverNode = new GetReceiverNode(); | ||
@Child private IndirectCallNode callNode = IndirectCallNode.create(); | ||
|
||
protected ElementOrEntryNode( | ||
@Nullable VmLanguage language, | ||
FrameDescriptor descriptor, | ||
ObjectMember member, | ||
ExpressionNode bodyNode) { | ||
|
||
super(language, descriptor, member, bodyNode); | ||
} | ||
|
||
@Specialization | ||
protected Object evalListing(VirtualFrame frame, VmListing receiver) { | ||
var result = executeBody(frame); | ||
return receiver.doTypeCast(result, VmUtils.getOwner(frame), callNode, null, null); | ||
} | ||
|
||
@Specialization | ||
protected Object evalMapping(VirtualFrame frame, VmMapping receiver) { | ||
var result = executeBody(frame); | ||
return receiver.doTypeCast(result, VmUtils.getOwner(frame), callNode, null, null); | ||
} | ||
|
||
@Specialization | ||
protected Object evalDynamic(VirtualFrame frame, @SuppressWarnings("unused") VmDynamic receiver) { | ||
return executeBody(frame); | ||
} | ||
} |
This file contains 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 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 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 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 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.