Skip to content

Commit 001632c

Browse files
committed
cache the closure on the PClosureRootNode in the single context case
1 parent 9aaa0ec commit 001632c

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class PFunction extends PythonObject {
5353
private final Assumption codeStableAssumption;
5454
private final Assumption defaultsStableAssumption;
5555
private final PythonObject globals;
56-
private final PCell[] closure;
56+
@CompilationFinal(dimensions = 1) private final PCell[] closure;
5757
private final boolean isStatic;
5858
@CompilationFinal private PCode code;
5959
@CompilationFinal(dimensions = 1) private Object[] defaultValues;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureFunctionRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
*/
4141
package com.oracle.graal.python.nodes;
4242

43+
import com.oracle.graal.python.PythonLanguage;
4344
import com.oracle.graal.python.builtins.objects.function.Signature;
4445
import com.oracle.graal.python.parser.ExecutionCellSlots;
4546
import com.oracle.truffle.api.Assumption;
4647
import com.oracle.truffle.api.CompilerDirectives;
47-
import com.oracle.truffle.api.TruffleLanguage;
4848
import com.oracle.truffle.api.frame.FrameDescriptor;
4949
import com.oracle.truffle.api.frame.FrameSlot;
5050

@@ -53,7 +53,7 @@ public abstract class PClosureFunctionRootNode extends PClosureRootNode {
5353
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final Assumption[] cellEffectivelyFinalAssumptions;
5454
private final Signature signature;
5555

56-
protected PClosureFunctionRootNode(TruffleLanguage<?> language, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots, Signature signature) {
56+
protected PClosureFunctionRootNode(PythonLanguage language, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots, Signature signature) {
5757
super(language, frameDescriptor, executionCellSlots.getFreeVarSlots());
5858
this.cellVarSlots = executionCellSlots.getCellVarSlots();
5959
this.cellEffectivelyFinalAssumptions = executionCellSlots.getCellVarAssumptions();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureRootNode.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,65 @@
4040
*/
4141
package com.oracle.graal.python.nodes;
4242

43+
import com.oracle.graal.python.PythonLanguage;
4344
import com.oracle.graal.python.builtins.objects.cell.PCell;
4445
import com.oracle.graal.python.builtins.objects.function.PArguments;
46+
import com.oracle.truffle.api.Assumption;
4547
import com.oracle.truffle.api.CompilerDirectives;
46-
import com.oracle.truffle.api.TruffleLanguage;
4748
import com.oracle.truffle.api.frame.Frame;
4849
import com.oracle.truffle.api.frame.FrameDescriptor;
4950
import com.oracle.truffle.api.frame.FrameSlot;
5051
import com.oracle.truffle.api.frame.VirtualFrame;
5152
import com.oracle.truffle.api.nodes.ExplodeLoop;
5253

5354
public abstract class PClosureRootNode extends PRootNode {
55+
private static final PCell[] NO_CLOSURE = new PCell[0];
56+
private final Assumption singleContextAssumption;
5457
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final FrameSlot[] freeVarSlots;
58+
@CompilerDirectives.CompilationFinal(dimensions = 1) protected PCell[] closure;
5559
private final int length;
5660

57-
protected PClosureRootNode(TruffleLanguage<?> language, FrameDescriptor frameDescriptor, FrameSlot[] freeVarSlots) {
61+
protected PClosureRootNode(PythonLanguage language, FrameDescriptor frameDescriptor, FrameSlot[] freeVarSlots) {
5862
super(language, frameDescriptor);
63+
this.singleContextAssumption = language.singleContextAssumption;
5964
this.freeVarSlots = freeVarSlots;
6065
this.length = freeVarSlots != null ? freeVarSlots.length : 0;
6166
}
6267

6368
protected void addClosureCellsToLocals(Frame frame) {
64-
PCell[] closure = PArguments.getClosure(frame);
65-
if (closure != null) {
69+
PCell[] frameClosure = PArguments.getClosure(frame);
70+
if (frameClosure != null) {
71+
if (singleContextAssumption.isValid() && closure == null) {
72+
CompilerDirectives.transferToInterpreterAndInvalidate();
73+
closure = frameClosure;
74+
} else if ((!singleContextAssumption.isValid() && closure != null && closure != NO_CLOSURE) || closure != frameClosure) {
75+
CompilerDirectives.transferToInterpreterAndInvalidate();
76+
closure = NO_CLOSURE;
77+
}
6678
assert freeVarSlots != null : "closure root node: the free var slots cannot be null when the closure is not null";
67-
assert closure.length == freeVarSlots.length : "closure root node: the closure must have the same length as the free var slots array";
68-
if (freeVarSlots.length < 32) {
79+
assert frameClosure.length == freeVarSlots.length : "closure root node: the closure must have the same length as the free var slots array";
80+
if (closure != null && closure != NO_CLOSURE) {
6981
addClosureCellsToLocalsExploded(frame, closure);
7082
} else {
71-
addClosureCellsToLocalsLoop(frame, closure);
83+
if (freeVarSlots.length < 32) {
84+
addClosureCellsToLocalsExploded(frame, frameClosure);
85+
} else {
86+
addClosureCellsToLocalsLoop(frame, frameClosure);
87+
}
7288
}
7389
}
7490
}
7591

76-
protected void addClosureCellsToLocalsLoop(Frame frame, PCell[] closure) {
92+
protected void addClosureCellsToLocalsLoop(Frame frame, PCell[] frameClosure) {
7793
for (int i = 0; i < length; i++) {
78-
frame.setObject(freeVarSlots[i], closure[i]);
94+
frame.setObject(freeVarSlots[i], frameClosure[i]);
7995
}
8096
}
8197

8298
@ExplodeLoop
83-
protected void addClosureCellsToLocalsExploded(Frame frame, PCell[] closure) {
99+
protected void addClosureCellsToLocalsExploded(Frame frame, PCell[] frameClosure) {
84100
for (int i = 0; i < length; i++) {
85-
frame.setObject(freeVarSlots[i], closure[i]);
101+
frame.setObject(freeVarSlots[i], frameClosure[i]);
86102
}
87103
}
88104

0 commit comments

Comments
 (0)