Skip to content

Commit 6fa7cc4

Browse files
committed
avoid the arity check in interpreted cases when it is constant
1 parent 24bd0b5 commit 6fa7cc4

File tree

1 file changed

+27
-4
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/argument

1 file changed

+27
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/argument/ArityCheckNode.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.builtins.objects.function.PythonCallable;
5454
import com.oracle.graal.python.nodes.PBaseNode;
5555
import com.oracle.graal.python.runtime.PythonOptions;
56+
import com.oracle.graal.python.runtime.exception.PException;
5657
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5758
import com.oracle.truffle.api.dsl.Cached;
5859
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -93,11 +94,33 @@ private String[] extractKeywordNames(PKeyword[] keywords) {
9394
return extractKeywordNames(keywords.length, keywords);
9495
}
9596

97+
protected boolean arityCheckWithoutKeywords(Arity arity, Object[] arguments) {
98+
try {
99+
arityCheck(arity, arguments, arity.getNumParameterIds(), PArguments.getNumberOfUserArgs(arguments), 0, 0, new String[0]);
100+
} catch (PException e) {
101+
return false;
102+
}
103+
return true;
104+
}
105+
106+
@SuppressWarnings("unused")
107+
@Specialization(guards = {
108+
"arity == cachedArity",
109+
"keywords.length == 0",
110+
"arguments.length == cachedArgLen",
111+
"isValid",
112+
}, limit = "1")
113+
void constantArityCheck(Arity arity, Object[] arguments, PKeyword[] keywords,
114+
@Cached("arity") Arity cachedArity,
115+
@Cached("arguments.length") int cachedArgLen,
116+
@Cached("arityCheckWithoutKeywords(arity, arguments)") boolean isValid) {
117+
}
118+
96119
@Specialization(guards = {
97120
"cachedLen == keywords.length",
98121
"cachedNumParamIds == arity.getNumParameterIds()",
99122
"cachedDeclLen == arity.getNumKeywordNames()"
100-
}, limit = "getVariableArgumentInlineCacheLimit()")
123+
}, limit = "getVariableArgumentInlineCacheLimit()", replaces = "constantArityCheck")
101124
void arityCheck(Arity arity, Object[] arguments, PKeyword[] keywords,
102125
@Cached("arity.getNumParameterIds()") int cachedNumParamIds,
103126
@Cached("arity.getNumKeywordNames()") int cachedDeclLen,
@@ -110,7 +133,7 @@ void arityCheck(Arity arity, Object[] arguments, PKeyword[] keywords,
110133
"cachedLen == keywords.length",
111134
"cachedNumParamIds == callee.getArity().getNumParameterIds()",
112135
"cachedDeclLen == callee.getArity().getNumKeywordNames()"
113-
}, limit = "getVariableArgumentInlineCacheLimit()")
136+
}, limit = "getVariableArgumentInlineCacheLimit()", replaces = "constantArityCheck")
114137
void arityCheckCallable(PythonCallable callee, Object[] arguments, PKeyword[] keywords,
115138
@Cached("callee.getArity().getNumParameterIds()") int cachedNumParamIds,
116139
@Cached("callee.getArity().getNumKeywordNames()") int cachedDeclLen,
@@ -120,13 +143,13 @@ void arityCheckCallable(PythonCallable callee, Object[] arguments, PKeyword[] ke
120143
arityCheck(arity, arguments, cachedNumParamIds, PArguments.getNumberOfUserArgs(arguments), cachedDeclLen, cachedLen, kwNames);
121144
}
122145

123-
@Specialization(replaces = "arityCheck")
146+
@Specialization(replaces = {"arityCheck", "constantArityCheck"})
124147
void uncachedCheck(Arity arity, Object[] arguments, PKeyword[] keywords) {
125148
String[] kwNames = extractKeywordNames(keywords);
126149
arityCheck(arity, arguments, PArguments.getNumberOfUserArgs(arguments), kwNames);
127150
}
128151

129-
@Specialization(replaces = "arityCheckCallable")
152+
@Specialization(replaces = {"arityCheckCallable", "constantArityCheck"})
130153
void uncachedCheckCallable(PythonCallable callee, Object[] arguments, PKeyword[] keywords) {
131154
String[] kwNames = extractKeywordNames(keywords);
132155
arityCheck(callee.getArity(), arguments, PArguments.getNumberOfUserArgs(arguments), kwNames);

0 commit comments

Comments
 (0)