Skip to content

Commit cb99ea2

Browse files
committed
Improve uncached case and do another caching case on methods.
1 parent 385d36e commit cb99ea2

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ public PCode getCode() {
154154
return uncachedCode;
155155
}
156156

157+
public PCode getUncachedCode() {
158+
return uncachedCode;
159+
}
160+
157161
public void setCode(PCode code) {
158162
codeStableAssumption.invalidate("code changed for function " + getName());
159163
this.code = this.uncachedCode = code;
@@ -173,6 +177,10 @@ public Object[] getDefaults() {
173177
return uncachedDefaultValues;
174178
}
175179

180+
public Object[] getUncachedDefaultValues() {
181+
return uncachedDefaultValues;
182+
}
183+
176184
public void setDefaults(Object[] defaults) {
177185
this.defaultsStableAssumption.invalidate("defaults changed for function " + getName());
178186
this.defaultValues = this.uncachedDefaultValues = defaults;
@@ -188,6 +196,10 @@ public PKeyword[] getKwDefaults() {
188196
return uncachedKwDefaultValues;
189197
}
190198

199+
public PKeyword[] getUncachedKwDefaults() {
200+
return uncachedKwDefaultValues;
201+
}
202+
191203
public void setKwDefaults(PKeyword[] defaults) {
192204
this.defaultsStableAssumption.invalidate("kw defaults changed for function " + getName());
193205
this.kwDefaultValues = this.uncachedKwDefaultValues = defaults;

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

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public static CreateArgumentsNode create() {
7777
}
7878

7979
@Specialization(guards = {"isMethod(method)", "method == cachedMethod"}, limit = "getVariableArgumentInlineCacheLimit()")
80-
@ExplodeLoop
8180
Object[] doMethodCached(PythonObject method, Object[] userArguments, PKeyword[] keywords,
8281
@Cached("create()") CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
8382
@Cached("method") @SuppressWarnings("unused") PythonObject cachedMethod) {
@@ -93,8 +92,7 @@ Object[] doMethodCached(PythonObject method, Object[] userArguments, PKeyword[]
9392

9493
@Specialization(guards = {"isMethod(method)", "getFunction(method) == cachedFunction",
9594
"getSelf(method) == cachedSelf"}, limit = "getVariableArgumentInlineCacheLimit()", replaces = "doMethodCached")
96-
@ExplodeLoop
97-
Object[] doMethodFunctionCached(PythonObject method, Object[] userArguments, PKeyword[] keywords,
95+
Object[] doMethodFunctionAndSelfCached(PythonObject method, Object[] userArguments, PKeyword[] keywords,
9896
@Cached("create()") CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
9997
@Cached("getFunction(method)") @SuppressWarnings("unused") Object cachedFunction,
10098
@Cached("getSelf(method)") Object cachedSelf) {
@@ -107,8 +105,21 @@ Object[] doMethodFunctionCached(PythonObject method, Object[] userArguments, PKe
107105
return createAndCheckArgumentsNode.execute(method, userArguments, keywords, arity, cachedSelf, defaults, kwdefaults, isMethodCall(cachedSelf));
108106
}
109107

108+
@Specialization(guards = {"isMethod(method)", "getFunction(method) == cachedFunction"}, limit = "getVariableArgumentInlineCacheLimit()", replaces = "doMethodFunctionAndSelfCached")
109+
Object[] doMethodFunctionCached(PythonObject method, Object[] userArguments, PKeyword[] keywords,
110+
@Cached("create()") CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
111+
@Cached("getFunction(method)") @SuppressWarnings("unused") Object cachedFunction) {
112+
113+
// We do not directly cache these objects because they are compilation final anyway and the
114+
// getter check the appropriate assumptions.
115+
Arity arity = getArity(cachedFunction);
116+
Object[] defaults = getDefaults(cachedFunction);
117+
PKeyword[] kwdefaults = getKwDefaults(cachedFunction);
118+
Object self = getSelf(method);
119+
return createAndCheckArgumentsNode.execute(method, userArguments, keywords, arity, self, defaults, kwdefaults, isMethodCall(self));
120+
}
121+
110122
@Specialization(guards = {"isFunction(callable)", "callable == cachedCallable"}, limit = "getVariableArgumentInlineCacheLimit()")
111-
@ExplodeLoop
112123
Object[] doFunctionCached(PythonObject callable, Object[] userArguments, PKeyword[] keywords,
113124
@Cached("create()") CreateAndCheckArgumentsNode createAndCheckArgumentsNode,
114125
@Cached("callable") @SuppressWarnings("unused") PythonObject cachedCallable) {
@@ -121,17 +132,17 @@ Object[] doFunctionCached(PythonObject callable, Object[] userArguments, PKeywor
121132
return createAndCheckArgumentsNode.execute(callable, userArguments, keywords, arity, null, defaults, kwdefaults, false);
122133
}
123134

124-
@Specialization(replaces = {"doFunctionCached", "doMethodCached", "doMethodFunctionCached"})
135+
@Specialization(replaces = {"doFunctionCached", "doMethodCached", "doMethodFunctionAndSelfCached", "doMethodFunctionCached"})
125136
Object[] uncached(PythonObject callable, Object[] userArguments, PKeyword[] keywords,
126137
@Cached("create()") CreateAndCheckArgumentsNode createAndCheckArgumentsNode) {
127138

128139
// mostly we will be calling proper functions directly here,
129140
// but sometimes also methods that have functions directly.
130141
// In all other cases, the arguments
131142

132-
Arity arity = getArity(callable);
133-
Object[] defaults = getDefaults(callable);
134-
PKeyword[] kwdefaults = getKwDefaults(callable);
143+
Arity arity = getArityUncached(callable);
144+
Object[] defaults = getDefaultsUncached(callable);
145+
PKeyword[] kwdefaults = getKwDefaultsUncached(callable);
135146
Object self = getSelf(callable);
136147
boolean methodcall = !(self instanceof PythonModule);
137148

@@ -560,6 +571,18 @@ public static FindKwDefaultNode create() {
560571
}
561572
}
562573

574+
protected static Arity getArityUncached(Object callable) {
575+
return getProperty(callable, UncachedArityGetter.INSTANCE);
576+
}
577+
578+
protected static Object[] getDefaultsUncached(Object callable) {
579+
return getProperty(callable, UncachedDefaultsGetter.INSTANCE);
580+
}
581+
582+
protected static PKeyword[] getKwDefaultsUncached(Object callable) {
583+
return getProperty(callable, UncachedKwDefaultsGetter.INSTANCE);
584+
}
585+
563586
protected static Arity getArity(Object callable) {
564587
return getProperty(callable, ArityGetter.INSTANCE);
565588
}
@@ -670,6 +693,48 @@ public PKeyword[] fromPBuiltinFunction(PBuiltinFunction fun) {
670693
}
671694
}
672695

696+
private static final class UncachedArityGetter extends Getter<Arity> {
697+
private static final UncachedArityGetter INSTANCE = new UncachedArityGetter();
698+
699+
@Override
700+
public Arity fromPFunction(PFunction fun) {
701+
return fun.getUncachedCode().getArity();
702+
}
703+
704+
@Override
705+
public Arity fromPBuiltinFunction(PBuiltinFunction fun) {
706+
return fun.getArity();
707+
}
708+
}
709+
710+
private static final class UncachedDefaultsGetter extends Getter<Object[]> {
711+
private static final UncachedDefaultsGetter INSTANCE = new UncachedDefaultsGetter();
712+
713+
@Override
714+
public Object[] fromPFunction(PFunction fun) {
715+
return fun.getUncachedDefaultValues();
716+
}
717+
718+
@Override
719+
public Object[] fromPBuiltinFunction(PBuiltinFunction fun) {
720+
return fun.getDefaults();
721+
}
722+
}
723+
724+
private static final class UncachedKwDefaultsGetter extends Getter<PKeyword[]> {
725+
private static final UncachedKwDefaultsGetter INSTANCE = new UncachedKwDefaultsGetter();
726+
727+
@Override
728+
public PKeyword[] fromPFunction(PFunction fun) {
729+
return fun.getUncachedKwDefaults();
730+
}
731+
732+
@Override
733+
public PKeyword[] fromPBuiltinFunction(PBuiltinFunction fun) {
734+
return fun.getKwDefaults();
735+
}
736+
}
737+
673738
private static final class NameGetter extends Getter<String> {
674739
private static final NameGetter INSTANCE = new NameGetter();
675740

0 commit comments

Comments
 (0)