Skip to content

Commit d3a5978

Browse files
committed
Move putGeneric() from DynamicObjectLibraryImpl to ObsolescenceStrategy
1 parent 8b5732e commit d3a5978

File tree

2 files changed

+71
-71
lines changed

2 files changed

+71
-71
lines changed

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

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -293,68 +293,6 @@ public static Property[] getPropertyArray(@SuppressWarnings("unused") DynamicObj
293293
return cachedShape.getPropertyArray();
294294
}
295295

296-
@TruffleBoundary
297-
protected static boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags, Shape s, Property existingProperty) {
298-
if (existingProperty == null && Flags.isSetExisting(putFlags)) {
299-
return false;
300-
}
301-
if (existingProperty != null && !Flags.isUpdateFlags(putFlags) && existingProperty.getLocation().canStore(value)) {
302-
existingProperty.getLocation().setSafe(object, value, false, false);
303-
return true;
304-
} else {
305-
return putGenericSlowPath(object, key, value, newPropertyFlags, putFlags, s, existingProperty);
306-
}
307-
}
308-
309-
private static boolean putGenericSlowPath(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags,
310-
Shape initialShape, Property propertyOfInitialShape) {
311-
CompilerAsserts.neverPartOfCompilation();
312-
ObsolescenceStrategy.singleton().updateShape(object);
313-
Shape oldShape;
314-
Property existingProperty;
315-
Shape newShape;
316-
Property property;
317-
do {
318-
oldShape = object.getShape();
319-
if (oldShape == initialShape) {
320-
existingProperty = propertyOfInitialShape;
321-
} else {
322-
existingProperty = oldShape.getProperty(key);
323-
}
324-
if (existingProperty == null) {
325-
if (Flags.isSetExisting(putFlags)) {
326-
return false;
327-
} else {
328-
newShape = ObsolescenceStrategy.singleton().defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
329-
property = newShape.getProperty(key);
330-
}
331-
} else if (Flags.isUpdateFlags(putFlags) && newPropertyFlags != existingProperty.getFlags()) {
332-
newShape = ObsolescenceStrategy.singleton().defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
333-
property = newShape.getProperty(key);
334-
} else {
335-
if (existingProperty.getLocation().canStore(value)) {
336-
newShape = oldShape;
337-
property = existingProperty;
338-
} else {
339-
newShape = ObsolescenceStrategy.singleton().defineProperty(oldShape, key, value, existingProperty.getFlags(), existingProperty, putFlags);
340-
property = newShape.getProperty(key);
341-
}
342-
}
343-
} while (ObsolescenceStrategy.singleton().updateShape(object));
344-
345-
assert object.getShape() == oldShape;
346-
Location location = property.getLocation();
347-
if (oldShape != newShape) {
348-
DynamicObjectSupport.grow(object, oldShape, newShape);
349-
location.setSafe(object, value, false, true);
350-
DynamicObjectSupport.setShapeWithStoreFence(object, newShape);
351-
ObsolescenceStrategy.singleton().updateShape(object);
352-
} else {
353-
location.setSafe(object, value, false, false);
354-
}
355-
return true;
356-
}
357-
358296
static RemovePlan prepareRemove(Shape shapeBefore, Shape shapeAfter, Property removedProperty) {
359297
assert !shapeBefore.isShared();
360298
List<Move> moves = new ArrayList<>();
@@ -710,7 +648,7 @@ public double getDoubleOrDefault(DynamicObject object, Shape cachedShape, Object
710648

711649
@Override
712650
public boolean put(DynamicObject object, Shape cachedShape, Object key, Object value, int propertyFlags, int putFlags) {
713-
return putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, cachedShape.getProperty(key));
651+
return ObsolescenceStrategy.singleton().putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, cachedShape.getProperty(key));
714652
}
715653

716654
@Override
@@ -1287,7 +1225,7 @@ protected boolean putImpl(DynamicObject object, Shape cachedShape, Object key, O
12871225
Shape oldShape = cachedShape;
12881226
MutateCacheData start = cache;
12891227
if (start == MutateCacheData.GENERIC || !cachedShape.isValid()) {
1290-
return putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
1228+
return ObsolescenceStrategy.singleton().putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
12911229
}
12921230
for (MutateCacheData c = start; c != null; c = c.next) {
12931231
if (!c.isValid()) {
@@ -1325,7 +1263,7 @@ protected boolean putIntImpl(DynamicObject object, Shape cachedShape, Object key
13251263
Shape oldShape = cachedShape;
13261264
MutateCacheData start = cache;
13271265
if (start == MutateCacheData.GENERIC || !cachedShape.isValid()) {
1328-
return putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
1266+
return ObsolescenceStrategy.singleton().putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
13291267
}
13301268
for (MutateCacheData c = start; c != null; c = c.next) {
13311269
if (!c.isValid()) {
@@ -1393,7 +1331,7 @@ protected boolean putLongImpl(DynamicObject object, Shape cachedShape, Object ke
13931331
Shape oldShape = cachedShape;
13941332
MutateCacheData start = cache;
13951333
if (start == MutateCacheData.GENERIC) {
1396-
return putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
1334+
return ObsolescenceStrategy.singleton().putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
13971335
}
13981336
for (MutateCacheData c = start; c != null; c = c.next) {
13991337
if (!c.isValid()) {
@@ -1442,7 +1380,7 @@ protected boolean putDoubleImpl(DynamicObject object, Shape cachedShape, Object
14421380
Shape oldShape = cachedShape;
14431381
MutateCacheData start = cache;
14441382
if (start == MutateCacheData.GENERIC) {
1445-
return putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
1383+
return ObsolescenceStrategy.singleton().putGeneric(object, key, value, propertyFlags, putFlags, cachedShape, oldProperty);
14461384
}
14471385
for (MutateCacheData c = start; c != null; c = c.next) {
14481386
if (!c.isValid()) {

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

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,66 @@ public void visitObjectArray(int index, int count) {
173173
return true;
174174
}
175175

176-
private Shape ensureSpace(Shape shape, Location location) {
177-
Objects.requireNonNull(location);
178-
assert assertLocationInRange(shape, location);
179-
return shape;
176+
@TruffleBoundary
177+
boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags, Shape s, Property existingProperty) {
178+
if (existingProperty == null && Flags.isSetExisting(putFlags)) {
179+
return false;
180+
}
181+
if (existingProperty != null && !Flags.isUpdateFlags(putFlags) && existingProperty.getLocation().canStore(value)) {
182+
existingProperty.getLocation().setSafe(object, value, false, false);
183+
return true;
184+
} else {
185+
return putGenericSlowPath(object, key, value, newPropertyFlags, putFlags, s, existingProperty);
186+
}
187+
}
188+
189+
private boolean putGenericSlowPath(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags,
190+
Shape initialShape, Property propertyOfInitialShape) {
191+
CompilerAsserts.neverPartOfCompilation();
192+
updateShape(object);
193+
Shape oldShape;
194+
Property existingProperty;
195+
Shape newShape;
196+
Property property;
197+
do {
198+
oldShape = object.getShape();
199+
if (oldShape == initialShape) {
200+
existingProperty = propertyOfInitialShape;
201+
} else {
202+
existingProperty = oldShape.getProperty(key);
203+
}
204+
if (existingProperty == null) {
205+
if (Flags.isSetExisting(putFlags)) {
206+
return false;
207+
} else {
208+
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
209+
property = newShape.getProperty(key);
210+
}
211+
} else if (Flags.isUpdateFlags(putFlags) && newPropertyFlags != existingProperty.getFlags()) {
212+
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
213+
property = newShape.getProperty(key);
214+
} else {
215+
if (existingProperty.getLocation().canStore(value)) {
216+
newShape = oldShape;
217+
property = existingProperty;
218+
} else {
219+
newShape = defineProperty(oldShape, key, value, existingProperty.getFlags(), existingProperty, putFlags);
220+
property = newShape.getProperty(key);
221+
}
222+
}
223+
} while (updateShape(object));
224+
225+
assert object.getShape() == oldShape;
226+
Location location = property.getLocation();
227+
if (oldShape != newShape) {
228+
DynamicObjectSupport.grow(object, oldShape, newShape);
229+
location.setSafe(object, value, false, true);
230+
DynamicObjectSupport.setShapeWithStoreFence(object, newShape);
231+
updateShape(object);
232+
} else {
233+
location.setSafe(object, value, false, false);
234+
}
235+
return true;
180236
}
181237

182238
Shape defineProperty(Shape shape, Object key, Object value, int flags) {
@@ -367,6 +423,12 @@ private Shape addPropertyInner(Shape shape, Property property) {
367423
return newShape;
368424
}
369425

426+
private Shape ensureSpace(Shape shape, Location location) {
427+
Objects.requireNonNull(location);
428+
assert assertLocationInRange(shape, location);
429+
return shape;
430+
}
431+
370432
private AddPropertyTransition newAddPropertyTransition(Property property) {
371433
return new AddPropertyTransition(property, toLocationOrType(property.getLocation()));
372434
}

0 commit comments

Comments
 (0)