Skip to content

Commit 56a1f5d

Browse files
committed
simplify LookupAttributeInMRONode, cache context independent class attrs
1 parent af75e29 commit 56a1f5d

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/LookupAttributeInMRONode.java

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.oracle.graal.python.PythonLanguage;
4444
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4545
import com.oracle.graal.python.builtins.objects.PNone;
46-
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
4746
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4847
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
4948
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroStorageNode;
@@ -57,9 +56,10 @@
5756
import com.oracle.truffle.api.CompilerAsserts;
5857
import com.oracle.truffle.api.CompilerDirectives;
5958
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
60-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6159
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
6260
import com.oracle.truffle.api.dsl.Cached;
61+
import com.oracle.truffle.api.dsl.CachedContext;
62+
import com.oracle.truffle.api.dsl.GenerateUncached;
6363
import com.oracle.truffle.api.dsl.ImportStatic;
6464
import com.oracle.truffle.api.dsl.ReportPolymorphism;
6565
import com.oracle.truffle.api.dsl.Specialization;
@@ -68,23 +68,10 @@
6868
@ImportStatic(PythonOptions.class)
6969
@ReportPolymorphism
7070
public abstract class LookupAttributeInMRONode extends PNodeWithContext {
71+
@GenerateUncached
7172
public abstract static class Dynamic extends PNodeWithContext {
72-
private static final DynamicUncached UNCACHED = new DynamicUncached();
73-
7473
public abstract Object execute(Object klass, Object key);
7574

76-
public static LookupAttributeInMRONode.Dynamic create() {
77-
return LookupAttributeInMRONodeGen.DynamicCachedNodeGen.create();
78-
}
79-
80-
public static LookupAttributeInMRONode.Dynamic getUncached() {
81-
return UNCACHED;
82-
}
83-
}
84-
85-
abstract static class DynamicCached extends Dynamic {
86-
@CompilationFinal private ContextReference<PythonContext> contextRef;
87-
8875
protected static boolean compareStrings(String key, String cachedKey) {
8976
return cachedKey.equals(key);
9077
}
@@ -97,37 +84,25 @@ protected static Object lookupConstantMRO(Object klass, @SuppressWarnings("unuse
9784
}
9885

9986
@Specialization(replaces = "lookupConstantMRO")
100-
protected Object lookup(PythonBuiltinClassType klass, Object key) {
101-
if (contextRef == null) {
102-
CompilerDirectives.transferToInterpreterAndInvalidate();
103-
contextRef = lookupContextReference(PythonLanguage.class);
104-
}
105-
return findAttr(contextRef.get().getCore(), klass, key);
87+
protected Object lookup(PythonBuiltinClassType klass, Object key,
88+
@CachedContext(PythonLanguage.class) PythonContext ctx,
89+
@Cached ReadAttributeFromDynamicObjectNode readAttrNode) {
90+
return findAttr(ctx.getCore(), klass, key, readAttrNode);
10691
}
10792

10893
@Specialization(replaces = "lookupConstantMRO")
10994
protected static Object lookup(Object klass, Object key,
110-
@Cached("create()") GetMroStorageNode getMroNode,
111-
@Cached("createForceType()") ReadAttributeFromObjectNode readAttrNode) {
95+
@Cached GetMroStorageNode getMroNode,
96+
@Cached(value = "createForceType()", uncached = "getUncachedForceType()") ReadAttributeFromObjectNode readAttrNode) {
11297
return lookupSlow(klass, key, getMroNode, readAttrNode, false);
11398
}
114-
}
11599

116-
static class DynamicUncached extends Dynamic {
117-
private final ReadAttributeFromObjectNode readAttrNode = ReadAttributeFromObjectNode.getUncachedForceType();
118-
private final GetMroStorageNode getMroNode = GetMroStorageNode.getUncached();
100+
public static LookupAttributeInMRONode.Dynamic create() {
101+
return LookupAttributeInMRONodeGen.DynamicNodeGen.create();
102+
}
119103

120-
@TruffleBoundary
121-
@Override
122-
public Object execute(Object klass, Object key) {
123-
if (klass instanceof PythonBuiltinClassType) {
124-
return findAttr(PythonLanguage.getCore(), (PythonBuiltinClassType) klass, key);
125-
} else if (klass instanceof PythonAbstractClass) {
126-
return lookupSlow(klass, key, getMroNode, readAttrNode, false);
127-
} else {
128-
CompilerDirectives.transferToInterpreter();
129-
throw new RuntimeException("not implemented: lookup inherited attribute from non-PythonClass");
130-
}
104+
public static LookupAttributeInMRONode.Dynamic getUncached() {
105+
return LookupAttributeInMRONodeGen.DynamicNodeGen.getUncached();
131106
}
132107
}
133108

@@ -170,17 +145,21 @@ public static LookupAttributeInMRONode createForLookupOfUnmanagedClasses(String
170145
*/
171146
public abstract Object execute(Object klass);
172147

173-
@TruffleBoundary
174148
protected static Object findAttr(PythonCore core, PythonBuiltinClassType klass, Object key) {
149+
return findAttr(core, klass, key, ReadAttributeFromDynamicObjectNode.getUncached());
150+
}
151+
152+
protected static Object findAttr(PythonCore core, PythonBuiltinClassType klass, Object key, ReadAttributeFromDynamicObjectNode readAttrNode) {
175153
PythonBuiltinClassType current = klass;
176-
while (current != PythonBuiltinClassType.PythonObject) {
177-
Object value = ReadAttributeFromDynamicObjectNode.getUncached().execute(core.lookupType(current).getStorage(), key);
154+
Object value = PNone.NO_VALUE;
155+
while (current != null) {
156+
value = readAttrNode.execute(core.lookupType(current), key);
178157
if (value != PNone.NO_VALUE) {
179158
return value;
180159
}
181160
current = current.getBase();
182161
}
183-
return ReadAttributeFromDynamicObjectNode.getUncached().execute(core.lookupType(current).getStorage(), key);
162+
return value;
184163
}
185164

186165
@Specialization(guards = {"klass == cachedKlass"}, limit = "getAttributeAccessInlineCacheMaxDepth()", assumptions = "singleContextAssumption()")
@@ -190,9 +169,25 @@ protected static Object lookupPBCTCached(@SuppressWarnings("unused") PythonBuilt
190169
return cachedValue;
191170
}
192171

172+
protected static boolean canCache(Object value) {
173+
return value instanceof Long ||
174+
value instanceof Integer ||
175+
value instanceof Boolean ||
176+
value instanceof Double ||
177+
value instanceof PNone;
178+
}
179+
180+
@Specialization(guards = {"klass == cachedKlass", "canCache(cachedValue)"}, limit = "getAttributeAccessInlineCacheMaxDepth()")
181+
protected static Object lookupPBCTCachedMulti(@SuppressWarnings("unused") PythonBuiltinClassType klass,
182+
@Cached("klass") @SuppressWarnings("unused") PythonBuiltinClassType cachedKlass,
183+
@Cached("findAttr(getCore(), cachedKlass, key)") Object cachedValue) {
184+
return cachedValue;
185+
}
186+
193187
@Specialization(replaces = "lookupPBCTCached")
194-
protected Object lookupPBCTGeneric(PythonBuiltinClassType klass) {
195-
return findAttr(getCore(), klass, key);
188+
protected Object lookupPBCTGeneric(PythonBuiltinClassType klass,
189+
@Cached ReadAttributeFromDynamicObjectNode readAttrNode) {
190+
return findAttr(getCore(), klass, key, readAttrNode);
196191
}
197192

198193
static final class PythonClassAssumptionPair {

0 commit comments

Comments
 (0)