Skip to content

Commit 31c9860

Browse files
committed
Eliminate redundant caching between call dispatch and argument creation
1 parent 85c3fb0 commit 31c9860

File tree

7 files changed

+207
-110
lines changed

7 files changed

+207
-110
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeNodes.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import com.oracle.truffle.api.frame.VirtualFrame;
8181
import com.oracle.truffle.api.nodes.Node;
8282
import com.oracle.truffle.api.nodes.RootNode;
83-
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
8483
import com.oracle.truffle.api.source.Source;
8584
import com.oracle.truffle.api.strings.TruffleString;
8685

@@ -259,10 +258,8 @@ public static Signature getInSingleContextMode(Node inliningTarget, PCode code)
259258
}
260259

261260
@Specialization(replaces = "doCached")
262-
static Signature doCode(Node inliningTarget, PCode code,
263-
@Cached InlinedConditionProfile signatureProfile,
264-
@Cached InlinedConditionProfile ctProfile) {
265-
return code.getSignature(inliningTarget, signatureProfile);
261+
static Signature doCode(PCode code) {
262+
return code.getSignature();
266263
}
267264
}
268265

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@
7979
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
8080
import com.oracle.graal.python.util.PythonUtils;
8181
import com.oracle.truffle.api.CompilerAsserts;
82-
import com.oracle.truffle.api.CompilerDirectives;
83-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
8482
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8583
import com.oracle.truffle.api.RootCallTarget;
8684
import com.oracle.truffle.api.bytecode.BytecodeNode;
@@ -91,10 +89,8 @@
9189
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9290
import com.oracle.truffle.api.library.ExportLibrary;
9391
import com.oracle.truffle.api.library.ExportMessage;
94-
import com.oracle.truffle.api.nodes.Node;
9592
import com.oracle.truffle.api.nodes.RootNode;
9693
import com.oracle.truffle.api.object.Shape;
97-
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
9894
import com.oracle.truffle.api.source.SourceSection;
9995
import com.oracle.truffle.api.strings.TruffleString;
10096

@@ -114,7 +110,7 @@ public final class PCode extends PythonBuiltinObject {
114110
public static final int CO_GRAALPYHON_MODULE = 0x1000;
115111

116112
private final RootCallTarget callTarget;
117-
@CompilationFinal private Signature signature;
113+
private final Signature signature;
118114

119115
// number of local variables
120116
private int nlocals = -1;
@@ -151,7 +147,7 @@ public final class PCode extends PythonBuiltinObject {
151147
public PCode(Object cls, Shape instanceShape, RootCallTarget callTarget) {
152148
super(cls, instanceShape);
153149
this.callTarget = callTarget;
154-
initializeSignature(callTarget);
150+
this.signature = Signature.fromCallTarget(callTarget);
155151
}
156152

157153
public PCode(Object cls, Shape instanceShape, RootCallTarget callTarget, int flags, int firstlineno, byte[] linetable, TruffleString filename) {
@@ -195,6 +191,7 @@ public PCode(Object cls, Shape instanceShape, RootCallTarget callTarget, Signatu
195191
this.cellvars = cellvars;
196192
this.callTarget = callTarget;
197193
this.signature = signature;
194+
assert signature != null;
198195
}
199196

200197
private static TruffleString[] extractFreeVars(RootNode rootNode) {
@@ -631,29 +628,6 @@ public boolean takesVarKeywordArgs() {
631628
}
632629

633630
public Signature getSignature() {
634-
return getSignature(null, InlinedConditionProfile.getUncached());
635-
}
636-
637-
public Signature getSignature(Node inliningTarget, InlinedConditionProfile signatureProfile) {
638-
if (signatureProfile.profile(inliningTarget, signature == null)) {
639-
if (CompilerDirectives.isPartialEvaluationConstant(this)) {
640-
CompilerDirectives.transferToInterpreterAndInvalidate();
641-
}
642-
signature = initializeSignature(callTarget);
643-
}
644-
return signature;
645-
}
646-
647-
@TruffleBoundary
648-
synchronized Signature initializeSignature(RootCallTarget rootCallTarget) {
649-
assert PythonContext.get(null).ownsGil(); // otherwise this is racy
650-
if (signature == null) {
651-
if (rootCallTarget.getRootNode() instanceof PRootNode) {
652-
signature = ((PRootNode) rootCallTarget.getRootNode()).getSignature();
653-
} else {
654-
signature = Signature.createVarArgsAndKwArgsOnly();
655-
}
656-
}
657631
return signature;
658632
}
659633

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import com.oracle.graal.python.nodes.ErrorMessages;
6666
import com.oracle.graal.python.nodes.PRaiseNode;
6767
import com.oracle.graal.python.nodes.StringLiterals;
68-
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
6968
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
7069
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
7170
import com.oracle.graal.python.nodes.call.CallDispatchers;
@@ -110,17 +109,15 @@ public abstract static class CallNode extends PythonBuiltinNode {
110109
@Specialization
111110
Object doIt(VirtualFrame frame, PFunction self, Object[] arguments, PKeyword[] keywords,
112111
@Bind Node inliningTarget,
113-
@Shared @Cached CreateArgumentsNode createArgs,
114-
@Cached CallDispatchers.FunctionCachedInvokeNode invoke) {
115-
return invoke.execute(frame, inliningTarget, self, createArgs.execute(self, arguments, keywords));
112+
@Cached CallDispatchers.FunctionCachedCallNode callNode) {
113+
return callNode.execute(frame, inliningTarget, self, arguments, keywords);
116114
}
117115

118116
@Specialization
119117
Object doIt(VirtualFrame frame, PBuiltinFunction self, Object[] arguments, PKeyword[] keywords,
120118
@Bind Node inliningTarget,
121-
@Shared @Cached CreateArgumentsNode createArgs,
122-
@Cached CallDispatchers.BuiltinFunctionCachedInvokeNode invoke) {
123-
return invoke.execute(frame, inliningTarget, self, createArgs.execute(self, arguments, keywords));
119+
@Cached CallDispatchers.BuiltinFunctionCachedCallNode callNode) {
120+
return callNode.execute(frame, inliningTarget, self, arguments, keywords);
124121
}
125122
}
126123

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@
3131
import java.util.ArrayList;
3232
import java.util.List;
3333

34+
import com.oracle.graal.python.nodes.PRootNode;
3435
import com.oracle.graal.python.util.PythonUtils;
3536
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3637
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
38+
import com.oracle.truffle.api.RootCallTarget;
3739
import com.oracle.truffle.api.strings.TruffleString;
3840

3941
public final class Signature {
4042
public static final Signature EMPTY = new Signature(-1, false, -1, PythonUtils.EMPTY_TRUFFLESTRING_ARRAY, PythonUtils.EMPTY_TRUFFLESTRING_ARRAY);
43+
public static final Signature GENERIC_VARARGS = new Signature(-1, true, 0, null, null);
4144

4245
private final int varArgIndex;
4346
private final int positionalOnlyArgIndex;
@@ -83,10 +86,6 @@ public Signature(int positionOnlyArgIndex, boolean takesVarKeywordArgs, int take
8386
this.hidden = hidden;
8487
}
8588

86-
public static Signature createVarArgsAndKwArgsOnly() {
87-
return new Signature(-1, true, 0, null, null);
88-
}
89-
9089
public int getNumOfRequiredKeywords() {
9190
return keywordOnlyNames.length;
9291
}
@@ -163,4 +162,8 @@ public TruffleString getRaiseErrorName() {
163162
public boolean isHidden() {
164163
return hidden;
165164
}
165+
166+
public static Signature fromCallTarget(RootCallTarget callTarget) {
167+
return callTarget.getRootNode() instanceof PRootNode rootNode ? rootNode.getSignature() : Signature.GENERIC_VARARGS;
168+
}
166169
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/AbstractMethodBuiltins.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
7171
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromPythonObjectNode;
7272
import com.oracle.graal.python.nodes.attributes.WriteAttributeToPythonObjectNode;
73+
import com.oracle.graal.python.nodes.call.CallDispatchers;
7374
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7475
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7576
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -110,27 +111,19 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
110111
@Slot(value = SlotKind.tp_call, isComplex = true)
111112
@SlotSignature(minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true)
112113
@GenerateNodeFactory
113-
public abstract static class CallNode extends PythonVarargsBuiltinNode {
114-
@Child private com.oracle.graal.python.nodes.call.CallNode callNode = com.oracle.graal.python.nodes.call.CallNode.create();
114+
abstract static class CallNode extends PythonVarargsBuiltinNode {
115115

116-
@Specialization(guards = "isFunction(self.getFunction())")
117-
protected Object doIt(VirtualFrame frame, PMethod self, Object[] arguments, PKeyword[] keywords) {
118-
return callNode.execute(frame, self, arguments, keywords);
119-
}
120-
121-
@Specialization(guards = "isFunction(self.getFunction())")
122-
protected Object doIt(VirtualFrame frame, PBuiltinMethod self, Object[] arguments, PKeyword[] keywords) {
123-
return callNode.execute(frame, self, arguments, keywords);
124-
}
125-
126-
@Specialization(guards = "!isFunction(self.getFunction())")
127-
protected Object doItNonFunction(VirtualFrame frame, PMethod self, Object[] arguments, PKeyword[] keywords) {
116+
@Specialization
117+
static Object doItNonFunction(VirtualFrame frame, PMethod self, Object[] arguments, PKeyword[] keywords,
118+
@Cached com.oracle.graal.python.nodes.call.CallNode callNode) {
128119
return callNode.execute(frame, self.getFunction(), PythonUtils.prependArgument(self.getSelf(), arguments), keywords);
129120
}
130121

131-
@Specialization(guards = "!isFunction(self.getFunction())")
132-
protected Object doItNonFunction(VirtualFrame frame, PBuiltinMethod self, Object[] arguments, PKeyword[] keywords) {
133-
return callNode.execute(frame, self.getFunction(), PythonUtils.prependArgument(self.getSelf(), arguments), keywords);
122+
@Specialization
123+
static Object doItNonFunction(VirtualFrame frame, PBuiltinMethod self, Object[] arguments, PKeyword[] keywords,
124+
@Bind Node inliningTarget,
125+
@Cached CallDispatchers.BuiltinMethodCachedCallNode callNode) {
126+
return callNode.execute(frame, inliningTarget, self, arguments, keywords);
134127
}
135128

136129
}

0 commit comments

Comments
 (0)