Skip to content

Commit 625863e

Browse files
cosminbascalukasstadler
authored andcommitted
reuse the attr stable in MRO assumption for delete attribute operations
1 parent a30ffea commit 625863e

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public Assumption createAttributeInMROFinalAssumption(Object name) {
127127
}
128128

129129
@TruffleBoundary
130-
public void setAttributeInMROFinalAssumption(Object name, Assumption assumption) {
130+
public void addAttributeInMROFinalAssumption(Object name, Assumption assumption) {
131131
List<Assumption> attrAssumptions = attributesInMROFinalAssumptions.getOrDefault(name, null);
132132
if (attrAssumptions == null) {
133133
attrAssumptions = new ArrayList<>();

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,24 @@ public static DeleteAttributeNode create(PNode object, PNode key) {
6363
public abstract Object execute(Object object, Object key);
6464

6565
@Specialization
66+
protected Object doClass(PythonClass cls, Object key,
67+
@Cached("createIdentityProfile()") ValueProfile setAttributeProfile,
68+
@Cached("create(__DELATTR__)") LookupAttributeInMRONode delAttributeLookup,
69+
@Cached("create()") CallBinaryMethodNode callDelAttribute) {
70+
cls.lookupChanged();
71+
cls.invalidateAttributeInMROFinalAssumptions(key);
72+
Object descr = setAttributeProfile.profile(delAttributeLookup.execute(cls));
73+
return callDelAttribute.executeObject(descr, cls, key);
74+
}
75+
76+
@Specialization(guards = "!isClass(object)")
6677
protected Object doIt(Object object, Object key,
6778
@Cached("createIdentityProfile()") ValueProfile setAttributeProfile,
6879
@Cached("create()") GetClassNode getClassNode,
6980
@Cached("create(__DELATTR__)") LookupAttributeInMRONode delAttributeLookup,
70-
@Cached("create()") CallBinaryMethodNode callDelAttr) {
81+
@Cached("create()") CallBinaryMethodNode callDelAttribute) {
7182
PythonClass type = getClassNode.execute(object);
7283
Object descr = setAttributeProfile.profile(delAttributeLookup.execute(type));
73-
return callDelAttr.executeObject(descr, object, key);
84+
return callDelAttribute.executeObject(descr, object, key);
7485
}
7586
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.oracle.truffle.api.dsl.Fallback;
4949
import com.oracle.truffle.api.dsl.Specialization;
5050
import com.oracle.truffle.api.nodes.ExplodeLoop;
51-
import com.oracle.truffle.api.profiles.ConditionProfile;
5251

5352
public abstract class LookupAttributeInMRONode extends PBaseNode {
5453

@@ -118,10 +117,13 @@ protected PythonClassAssumptionPair findAttrClassAndAssumptionInMRO(PythonClass
118117
Assumption attrAssumption = null;
119118
for (int i = 0; i < mro.length; i++) {
120119
PythonClass cls = mro[i];
121-
if (attrAssumption == null) {
122-
attrAssumption = cls.createAttributeInMROFinalAssumption(key);
123-
} else {
124-
cls.setAttributeInMROFinalAssumption(key, attrAssumption);
120+
if (i > 0) {
121+
assert cls != klass : "MRO chain is incorrect: '" + klass + "' was found at position " + i;
122+
if (attrAssumption == null) {
123+
attrAssumption = cls.createAttributeInMROFinalAssumption(key);
124+
} else {
125+
cls.addAttributeInMROFinalAssumption(key, attrAssumption);
126+
}
125127
}
126128

127129
if (cls.getStorage().containsKey(key)) {
@@ -131,20 +133,14 @@ protected PythonClassAssumptionPair findAttrClassAndAssumptionInMRO(PythonClass
131133
return new PythonClassAssumptionPair(null, attrAssumption);
132134
}
133135

134-
@Specialization(guards = {"klass == cachedKlass", "cachedAttributeClassAssumptionPair.cls != null"}, limit = "5", assumptions = {"lookupStable",
135-
"cachedAttributeClassAssumptionPair.assumption"}, rewriteOn = IllegalStateException.class)
136+
@Specialization(guards = {"klass == cachedKlass", "cachedAttributeStableInMROInfo.cls != null"}, limit = "5", assumptions = {"lookupStable",
137+
"cachedAttributeStableInMROInfo.assumption"})
136138
protected Object lookupConstantMROCached(@SuppressWarnings("unused") PythonClass klass,
137139
@Cached("klass") @SuppressWarnings("unused") PythonClass cachedKlass,
138140
@Cached("cachedKlass.getLookupStableAssumption()") @SuppressWarnings("unused") Assumption lookupStable,
139141
@Cached("create()") ReadAttributeFromObjectNode readAttrNode,
140-
@Cached("findAttrClassAndAssumptionInMRO(cachedKlass)") @SuppressWarnings("unused") PythonClassAssumptionPair cachedAttributeClassAssumptionPair,
141-
@Cached("createBinaryProfile()") ConditionProfile attributeDeletedProfile) {
142-
Object value = readAttrNode.execute(cachedAttributeClassAssumptionPair.cls, key);
143-
if (attributeDeletedProfile.profile(value == PNone.NO_VALUE)) {
144-
// in case the attribute was deleted
145-
throw new IllegalStateException();
146-
}
147-
return value;
142+
@Cached("findAttrClassAndAssumptionInMRO(cachedKlass)") @SuppressWarnings("unused") PythonClassAssumptionPair cachedAttributeStableInMROInfo) {
143+
return readAttrNode.execute(cachedAttributeStableInMROInfo.cls, key);
148144
}
149145

150146
protected ReadAttributeFromObjectNode[] create(int size) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.truffle.api.dsl.NodeChildren;
5252
import com.oracle.truffle.api.dsl.Specialization;
5353
import com.oracle.truffle.api.frame.VirtualFrame;
54+
import com.oracle.truffle.api.profiles.ConditionProfile;
5455
import com.oracle.truffle.api.profiles.ValueProfile;
5556

5657
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "key", type = PNode.class), @NodeChild(value = "rhs", type = PNode.class)})
@@ -92,7 +93,11 @@ public PNode getPrimaryNode() {
9293
protected Object doClass(PythonClass cls, Object key, Object value,
9394
@Cached("createIdentityProfile()") ValueProfile setAttributeProfile,
9495
@Cached("create(__SETATTR__)") LookupAttributeInMRONode setAttributeLookup,
95-
@Cached("create()") CallTernaryMethodNode callSetAttribute) {
96+
@Cached("create()") CallTernaryMethodNode callSetAttribute,
97+
@Cached("createBinaryProfile()") ConditionProfile isAddingAttributeProfile) {
98+
if (isAddingAttributeProfile.profile(!cls.getStorage().containsKey(key))) {
99+
cls.lookupChanged();
100+
}
96101
cls.invalidateAttributeInMROFinalAssumptions(key);
97102
Object descr = setAttributeProfile.profile(setAttributeLookup.execute(cls));
98103
return callSetAttribute.execute(descr, cls, key, value);

0 commit comments

Comments
 (0)