Skip to content

Commit 5bf34c3

Browse files
cosminbascalukasstadler
authored andcommitted
specialization to bypass the MRO loop if the lookup is fixed
1 parent e78ffb7 commit 5bf34c3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ public static LookupAttributeInMRONode create(String key) {
102102
*/
103103
public abstract Object execute(PythonClass klass);
104104

105+
protected PythonClass findClassInMRO(PythonClass klass) {
106+
PythonClass[] mro = klass.getMethodResolutionOrder();
107+
for (int i = 0; i < mro.length; i++) {
108+
PythonClass kls = mro[i];
109+
if (kls.getStorage().containsKey(key)) {
110+
return kls;
111+
}
112+
}
113+
return null;
114+
}
115+
116+
@Specialization(guards = {"klass == cachedKlass", "cachedAttrKlass != null"}, limit = "5", assumptions = "lookupStable", rewriteOn = IllegalStateException.class)
117+
protected Object lookupConstantMROCached(@SuppressWarnings("unused") PythonClass klass,
118+
@Cached("klass") @SuppressWarnings("unused") PythonClass cachedKlass,
119+
@Cached("cachedKlass.getLookupStableAssumption()") @SuppressWarnings("unused") Assumption lookupStable,
120+
@Cached("create()") ReadAttributeFromObjectNode readAttrNode,
121+
@Cached("findClassInMRO(cachedKlass)") @SuppressWarnings("unused") PythonClass cachedAttrKlass) {
122+
Object value = readAttrNode.execute(cachedAttrKlass, key);
123+
if (value == PNone.NO_VALUE) {
124+
// in case the attribute was deleted
125+
throw new IllegalStateException();
126+
}
127+
return value;
128+
}
129+
105130
@Specialization(guards = {"klass == cachedKlass", "mroLength < 32"}, limit = "5", assumptions = "lookupStable")
106131
@ExplodeLoop(kind = ExplodeLoop.LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN)
107132
protected Object lookupConstantMRO(@SuppressWarnings("unused") PythonClass klass,

0 commit comments

Comments
 (0)