Skip to content

Commit a0f9ff9

Browse files
cosminbascalukasstadler
authored andcommitted
add profile for the MRO loop only when adding a new attribute, setting an existing attribute can go via the existing mechanism
1 parent 196915e commit a0f9ff9

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.oracle.truffle.api.dsl.Specialization;
5353
import com.oracle.truffle.api.frame.VirtualFrame;
5454
import com.oracle.truffle.api.object.DynamicObject;
55+
import com.oracle.truffle.api.profiles.ConditionProfile;
5556
import com.oracle.truffle.api.profiles.ValueProfile;
5657

5758
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "key", type = PNode.class), @NodeChild(value = "rhs", type = PNode.class)})
@@ -93,16 +94,21 @@ public PNode getPrimaryNode() {
9394
protected Object doClass(PythonClass cls, Object key, Object value,
9495
@Cached("createIdentityProfile()") ValueProfile setattributeProfile,
9596
@Cached("create(__SETATTR__)") LookupAttributeInMRONode setattributeLookup,
96-
@Cached("create()") CallTernaryMethodNode callSetattr) {
97-
Object descr = setattributeProfile.profile(setattributeLookup.execute(cls));
98-
PythonClass[] mro = cls.getMethodResolutionOrder();
99-
for (int i = 0; i < mro.length; i++) {
100-
PythonClass kls = mro[i];
101-
DynamicObject storage = kls.getStorage();
102-
if (storage.containsKey(key)) {
103-
storage.getShape().getProperty(key).getLocation().getFinalAssumption().invalidate();
97+
@Cached("create()") CallTernaryMethodNode callSetattr,
98+
@Cached("createBinaryProfile()") ConditionProfile isAddingAttributeProfile) {
99+
// add new attribute case: invalidate the final assumption for the classes in the MRO chain
100+
// this is needed to de-specialize LookupAttributeInMRONode.lookupConstantMROCached
101+
if (isAddingAttributeProfile.profile(cls.getStorage().containsKey(key))) {
102+
PythonClass[] mro = cls.getMethodResolutionOrder();
103+
for (int i = 0; i < mro.length; i++) {
104+
PythonClass kls = mro[i];
105+
DynamicObject storage = kls.getStorage();
106+
if (storage.containsKey(key)) {
107+
storage.getShape().getProperty(key).getLocation().getFinalAssumption().invalidate();
108+
}
104109
}
105110
}
111+
Object descr = setattributeProfile.profile(setattributeLookup.execute(cls));
106112
return callSetattr.execute(descr, cls, key, value);
107113
}
108114

0 commit comments

Comments
 (0)