Skip to content

Commit ecab15b

Browse files
committed
Simplify putGeneric and use setInternal.
1 parent 147f8c1 commit ecab15b

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,10 @@ static boolean updateShape(DynamicObject object) {
10511051
return ObsolescenceStrategy.updateShape(object);
10521052
}
10531053

1054+
static boolean updateShape(DynamicObject object, Shape currentShape) {
1055+
return ObsolescenceStrategy.updateShape(object, currentShape);
1056+
}
1057+
10541058
/**
10551059
* Checks if this object contains a property with the given key.
10561060
*
@@ -1189,7 +1193,7 @@ static boolean doGeneric(DynamicObject receiver, Object key) {
11891193

11901194
@TruffleBoundary
11911195
static boolean removePropertyGeneric(DynamicObject receiver, Shape cachedShape, Property cachedProperty) {
1192-
updateShape(receiver);
1196+
updateShape(receiver, cachedShape);
11931197
Shape oldShape = receiver.getShape();
11941198
Property existingProperty = reusePropertyLookup(cachedShape, cachedProperty, oldShape);
11951199

@@ -1806,14 +1810,14 @@ private static void changePropertyFlagsGeneric(DynamicObject receiver, Shape cac
18061810
assert cachedProperty != null;
18071811
assert cachedProperty.getFlags() != propertyFlags;
18081812

1809-
updateShape(receiver);
1813+
updateShape(receiver, cachedShape);
18101814

18111815
Shape oldShape = receiver.getShape();
18121816
final Property existingProperty = reusePropertyLookup(cachedShape, cachedProperty, oldShape);
18131817
Shape newShape = oldShape.setPropertyFlags(existingProperty, propertyFlags);
18141818
if (newShape != oldShape) {
18151819
DynamicObjectSupport.setShapeWithStoreFence(receiver, newShape);
1816-
updateShape(receiver);
1820+
updateShape(receiver, newShape);
18171821
}
18181822
}
18191823

@@ -1853,7 +1857,7 @@ static Property reusePropertyLookup(Shape cachedShape, Property cachedProperty,
18531857
static void maybeUpdateShape(DynamicObject store, Shape newShape) {
18541858
CompilerAsserts.partialEvaluationConstant(newShape);
18551859
if (!newShape.isValid()) {
1856-
updateShape(store);
1860+
updateShape(store, newShape);
18571861
}
18581862
}
18591863

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

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.graalvm.collections.Pair;
5858

5959
import com.oracle.truffle.api.CompilerAsserts;
60-
import com.oracle.truffle.api.CompilerDirectives;
6160
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6261
import com.oracle.truffle.api.object.ExtLocations.ObjectLocation;
6362
import com.oracle.truffle.api.object.Transition.AbstractReplacePropertyTransition;
@@ -159,74 +158,53 @@ private static boolean assertLocationInRange(final Shape shape, final Location l
159158
}
160159

161160
@TruffleBoundary
162-
static boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags) {
163-
Shape shape = object.getShape();
164-
Property existingProperty = shape.getProperty(key);
165-
return putGeneric(object, key, value, newPropertyFlags, putFlags, shape, existingProperty);
161+
static boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int mode) {
162+
return putGeneric(object, key, value, newPropertyFlags, mode, null, null);
166163
}
167164

168165
@TruffleBoundary
169-
static boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags, Shape s, Property existingProperty) {
170-
if (existingProperty == null) {
171-
if (Flags.isPutIfPresent(putFlags)) {
172-
return false;
173-
}
174-
} else {
175-
if (Flags.isPutIfAbsent(putFlags)) {
176-
return false;
177-
} else if (!Flags.isUpdateFlags(putFlags) && existingProperty.getLocation().canStore(value)) {
178-
existingProperty.getLocation().setSafe(object, value, false, false);
179-
return true;
180-
}
181-
}
182-
return putGenericSlowPath(object, key, value, newPropertyFlags, putFlags, s, existingProperty);
183-
}
184-
185-
private static boolean putGenericSlowPath(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags,
186-
Shape initialShape, Property propertyOfInitialShape) {
166+
static boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int mode,
167+
Shape cachedShape, Property propertyOfCachedShape) {
187168
CompilerAsserts.neverPartOfCompilation();
188169
updateShape(object);
189170
Shape oldShape;
190171
Shape newShape;
191172
Property property;
192173
do {
193174
oldShape = object.getShape();
194-
final Property existingProperty = reusePropertyLookup(key, initialShape, propertyOfInitialShape, oldShape);
175+
final Property existingProperty = reusePropertyLookup(key, cachedShape, propertyOfCachedShape, oldShape);
195176
if (existingProperty == null) {
196-
if (Flags.isPutIfPresent(putFlags)) {
177+
if (Flags.isPutIfPresent(mode)) {
197178
return false;
198179
} else {
199-
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
180+
newShape = defineProperty(oldShape, key, value, newPropertyFlags, null, mode);
200181
property = newShape.getProperty(key);
201182
}
202-
} else if (Flags.isPutIfAbsent(putFlags)) {
183+
} else if (Flags.isPutIfAbsent(mode)) {
203184
return false;
204-
} else if (Flags.isUpdateFlags(putFlags) && newPropertyFlags != existingProperty.getFlags()) {
205-
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
206-
property = newShape.getProperty(key);
207-
} else {
208-
if (existingProperty.getLocation().canStore(value)) {
185+
} else if (!Flags.isUpdateFlags(mode) || newPropertyFlags == existingProperty.getFlags()) {
186+
if (existingProperty.getLocation().canStoreValue(value)) {
209187
newShape = oldShape;
210188
property = existingProperty;
211189
} else {
212-
assert !Flags.isUpdateFlags(putFlags) || newPropertyFlags == existingProperty.getFlags();
213-
newShape = defineProperty(oldShape, key, value, existingProperty.getFlags(), existingProperty, putFlags);
190+
newShape = defineProperty(oldShape, key, value, existingProperty.getFlags(), existingProperty, mode);
214191
property = newShape.getProperty(key);
215192
}
193+
} else {
194+
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, mode);
195+
property = newShape.getProperty(key);
216196
}
217197
} while (updateShape(object));
218198

219199
assert object.getShape() == oldShape;
200+
assert !Flags.isPutIfAbsent(mode) || oldShape != newShape;
201+
220202
Location location = property.getLocation();
203+
location.setInternal(object, value, false, oldShape, newShape);
204+
221205
if (oldShape != newShape) {
222-
DynamicObjectSupport.grow(object, oldShape, newShape);
223-
location.setSafe(object, value, false, true);
224206
DynamicObjectSupport.setShapeWithStoreFence(object, newShape);
225207
updateShape(object);
226-
} else if (Flags.isPutIfAbsent(putFlags)) {
227-
return false;
228-
} else {
229-
location.setSafe(object, value, false, false);
230208
}
231209
return true;
232210
}
@@ -503,11 +481,15 @@ public String getMessage() {
503481
}
504482
}
505483

506-
@TruffleBoundary
507484
static boolean updateShape(DynamicObject object) {
508-
boolean changed = checkForObsoleteShapeAndMigrate(object);
509-
// shape should be valid now, but we cannot assert this due to a possible race
510-
return changed;
485+
return updateShape(object, object.getShape());
486+
}
487+
488+
static boolean updateShape(DynamicObject object, Shape currentShape) {
489+
if (currentShape.isValid()) {
490+
return false;
491+
}
492+
return migrateObsoleteShape(object, currentShape);
511493
}
512494

513495
private static Shape ensureValid(Shape newShape) {
@@ -800,21 +782,14 @@ private static void setPropertyInternal(Property toProperty, DynamicObject toObj
800782
toProperty.getLocation().set(toObject, value, false, true);
801783
}
802784

803-
private static boolean checkForObsoleteShapeAndMigrate(DynamicObject store) {
804-
Shape currentShape = store.getShape();
805-
if (currentShape.isValid()) {
806-
return false;
807-
}
808-
CompilerDirectives.transferToInterpreter();
809-
return migrateObsoleteShape(currentShape, store);
810-
}
811-
812-
private static boolean migrateObsoleteShape(Shape currentShape, DynamicObject store) {
785+
@TruffleBoundary
786+
private static boolean migrateObsoleteShape(DynamicObject store, Shape currentShape) {
813787
CompilerAsserts.neverPartOfCompilation();
814788
synchronized (currentShape.getMutex()) {
815789
if (!currentShape.isValid()) {
816790
assert !currentShape.isShared();
817791
reshape(store);
792+
// shape should be valid now, but we cannot assert this due to a possible race
818793
return true;
819794
}
820795
return false;

0 commit comments

Comments
 (0)