Skip to content

Commit 6ba3516

Browse files
committed
Extract reusePropertyLookup() in ObsolescenceStrategy
1 parent d3a5978 commit 6ba3516

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

truffle/src/com.oracle.truffle.api.object.test/src/com/oracle/truffle/api/object/test/ObjectModelRegressionTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ public void testDefinePropertyWithFlagsChangeAndFinalInvalidation() {
142142
assertObjectLocation(b.getShape().getProperty("x").getLocation());
143143
}
144144

145+
@Test
146+
public void testAddNewPropertyOnObsoleteShape() {
147+
Shape emptyShape = newEmptyShape();
148+
DynamicObject a = newInstance(emptyShape);
149+
DynamicObject b = newInstance(emptyShape);
150+
151+
DynamicObjectLibrary library = createLibrary(DynamicObjectLibrary.class, a);
152+
153+
library.put(a, "x", 1);
154+
library.put(b, "x", 1);
155+
library.put(b, "x", "");
156+
assertTrue(b.getShape().isValid()); // updated by put()
157+
assertFalse(a.getShape().isValid()); // obsolete
158+
library.put(a, "y", 2); // put on obsolete shape
159+
assertTrue(a.getShape().isValid());
160+
161+
assertEquals(1, library.getOrDefault(a, "x", null));
162+
assertEquals("", library.getOrDefault(b, "x", null));
163+
assertEquals(2, library.getOrDefault(a, "y", null));
164+
}
165+
145166
@Test
146167
public void testRemovePropertyOnObsoleteShape() {
147168
Shape emptyShape = newEmptyShape();

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObsolescenceStrategy.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,11 @@ private boolean putGenericSlowPath(DynamicObject object, Object key, Object valu
191191
CompilerAsserts.neverPartOfCompilation();
192192
updateShape(object);
193193
Shape oldShape;
194-
Property existingProperty;
195194
Shape newShape;
196195
Property property;
197196
do {
198197
oldShape = object.getShape();
199-
if (oldShape == initialShape) {
200-
existingProperty = propertyOfInitialShape;
201-
} else {
202-
existingProperty = oldShape.getProperty(key);
203-
}
198+
final Property existingProperty = reusePropertyLookup(key, initialShape, propertyOfInitialShape, oldShape);
204199
if (existingProperty == null) {
205200
if (Flags.isSetExisting(putFlags)) {
206201
return false;
@@ -235,6 +230,15 @@ private boolean putGenericSlowPath(DynamicObject object, Object key, Object valu
235230
return true;
236231
}
237232

233+
static Property reusePropertyLookup(Object key, Shape cachedShape, Property cachedProperty, Shape updatedShape) {
234+
assert cachedProperty == null || cachedProperty.getKey().equals(key);
235+
if (updatedShape == cachedShape) {
236+
return cachedProperty;
237+
} else {
238+
return updatedShape.getProperty(key);
239+
}
240+
}
241+
238242
Shape defineProperty(Shape shape, Object key, Object value, int flags) {
239243
return defineProperty(shape, key, value, flags, Flags.DEFAULT);
240244
}

0 commit comments

Comments
 (0)