Skip to content

Commit d4b3d69

Browse files
committed
Initialize extension arrays with empty array constant instead of null.
1 parent 33c7f56 commit d4b3d69

File tree

2 files changed

+55
-48
lines changed

2 files changed

+55
-48
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@
173173
@SuppressWarnings("deprecation")
174174
public abstract class DynamicObject implements TruffleObject {
175175

176+
static final Object[] EMPTY_OBJECT_ARRAY = {};
177+
static final int[] EMPTY_INT_ARRAY = {};
178+
176179
private static final MethodHandles.Lookup LOOKUP = internalLookup();
177180

178181
/**
@@ -192,9 +195,9 @@ public abstract class DynamicObject implements TruffleObject {
192195
private Shape shape;
193196

194197
/** Object extension array. */
195-
@DynamicField private Object[] extRef;
198+
@DynamicField private Object[] extRef = EMPTY_OBJECT_ARRAY;
196199
/** Primitive extension array. */
197-
@DynamicField private int[] extVal;
200+
@DynamicField private int[] extVal = EMPTY_INT_ARRAY;
198201

199202
/**
200203
* Constructor for {@link DynamicObject} subclasses. Initializes the object with the provided

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

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static void grow(DynamicObject object, Shape thisShape, Shape otherShape) {
7777
}
7878

7979
private static void growObjectStore(DynamicObject object, Shape thisShape, int sourceCapacity, int targetCapacity) {
80+
assert targetCapacity != 0;
8081
Object[] newObjectStore = new Object[targetCapacity];
8182
if (sourceCapacity != 0) {
8283
int sourceSize = thisShape.getObjectArraySize();
@@ -87,6 +88,7 @@ private static void growObjectStore(DynamicObject object, Shape thisShape, int s
8788
}
8889

8990
private static void growPrimitiveStore(DynamicObject object, Shape thisShape, int sourceCapacity, int targetCapacity) {
91+
assert targetCapacity != 0;
9092
int[] newPrimitiveArray = new int[targetCapacity];
9193
if (sourceCapacity != 0) {
9294
int sourceSize = thisShape.getPrimitiveArraySize();
@@ -108,81 +110,83 @@ static void trimToSize(DynamicObject object, Shape thisShape, Shape otherShape)
108110

109111
private static void resizeObjectStore(DynamicObject object, Shape oldShape, Shape newShape) {
110112
int destinationCapacity = newShape.getObjectArrayCapacity();
113+
int sourceCapacity = oldShape.getObjectArrayCapacity();
114+
if (sourceCapacity == destinationCapacity) {
115+
return;
116+
}
117+
Object[] newObjectStore;
111118
if (destinationCapacity == 0) {
112-
object.setObjectStore(null);
119+
newObjectStore = DynamicObject.EMPTY_OBJECT_ARRAY;
113120
} else {
114-
int sourceCapacity = oldShape.getObjectArrayCapacity();
115-
if (sourceCapacity != destinationCapacity) {
116-
int sourceSize = oldShape.getObjectArraySize();
117-
Object[] newObjectStore = new Object[destinationCapacity];
118-
if (sourceSize != 0) {
119-
Object[] oldObjectStore = object.getObjectStore();
120-
int destinationSize = newShape.getObjectArraySize();
121-
int length = Math.min(sourceSize, destinationSize);
122-
UnsafeAccess.arrayCopy(oldObjectStore, newObjectStore, length);
123-
}
124-
object.setObjectStore(newObjectStore);
121+
int sourceSize = oldShape.getObjectArraySize();
122+
newObjectStore = new Object[destinationCapacity];
123+
if (sourceSize != 0) {
124+
Object[] oldObjectStore = object.getObjectStore();
125+
int destinationSize = newShape.getObjectArraySize();
126+
int length = Math.min(sourceSize, destinationSize);
127+
UnsafeAccess.arrayCopy(oldObjectStore, newObjectStore, length);
125128
}
126129
}
130+
object.setObjectStore(newObjectStore);
127131
}
128132

129133
private static void resizePrimitiveStore(DynamicObject object, Shape oldShape, Shape newShape) {
130134
assert newShape.hasPrimitiveArray();
131135
int destinationCapacity = newShape.getPrimitiveArrayCapacity();
136+
int sourceCapacity = oldShape.getPrimitiveArrayCapacity();
137+
if (sourceCapacity == destinationCapacity) {
138+
return;
139+
}
140+
int[] newPrimitiveArray;
132141
if (destinationCapacity == 0) {
133-
object.setPrimitiveStore(null);
142+
newPrimitiveArray = DynamicObject.EMPTY_INT_ARRAY;
134143
} else {
135-
int sourceCapacity = oldShape.getPrimitiveArrayCapacity();
136-
if (sourceCapacity != destinationCapacity) {
137-
int sourceSize = oldShape.getPrimitiveArraySize();
138-
int[] newPrimitiveArray = new int[destinationCapacity];
139-
if (sourceSize != 0) {
140-
int[] oldPrimitiveArray = object.getPrimitiveStore();
141-
int destinationSize = newShape.getPrimitiveArraySize();
142-
int length = Math.min(sourceSize, destinationSize);
143-
UnsafeAccess.arrayCopy(oldPrimitiveArray, newPrimitiveArray, length);
144-
}
145-
object.setPrimitiveStore(newPrimitiveArray);
144+
int sourceSize = oldShape.getPrimitiveArraySize();
145+
newPrimitiveArray = new int[destinationCapacity];
146+
if (sourceSize != 0) {
147+
int[] oldPrimitiveArray = object.getPrimitiveStore();
148+
int destinationSize = newShape.getPrimitiveArraySize();
149+
int length = Math.min(sourceSize, destinationSize);
150+
UnsafeAccess.arrayCopy(oldPrimitiveArray, newPrimitiveArray, length);
146151
}
147152
}
153+
object.setPrimitiveStore(newPrimitiveArray);
148154
}
149155

150156
private static void trimObjectStore(DynamicObject object, Shape thisShape, Shape newShape) {
151157
Object[] oldObjectStore = object.getObjectStore();
152158
int destinationCapacity = newShape.getObjectArrayCapacity();
159+
int sourceCapacity = thisShape.getObjectArrayCapacity();
160+
if (sourceCapacity <= destinationCapacity) {
161+
return;
162+
}
163+
Object[] newObjectStore;
153164
if (destinationCapacity == 0) {
154-
if (oldObjectStore != null) {
155-
object.setObjectStore(null);
156-
}
157-
// else nothing to do
165+
newObjectStore = DynamicObject.EMPTY_OBJECT_ARRAY;
158166
} else {
159-
int sourceCapacity = thisShape.getObjectArrayCapacity();
160-
if (sourceCapacity > destinationCapacity) {
161-
Object[] newObjectStore = new Object[destinationCapacity];
162-
int destinationSize = newShape.getObjectArraySize();
163-
UnsafeAccess.arrayCopy(oldObjectStore, newObjectStore, destinationSize);
164-
object.setObjectStore(newObjectStore);
165-
}
167+
newObjectStore = new Object[destinationCapacity];
168+
int destinationSize = newShape.getObjectArraySize();
169+
UnsafeAccess.arrayCopy(oldObjectStore, newObjectStore, destinationSize);
166170
}
171+
object.setObjectStore(newObjectStore);
167172
}
168173

169174
private static void trimPrimitiveStore(DynamicObject object, Shape thisShape, Shape newShape) {
170175
int[] oldPrimitiveStore = object.getPrimitiveStore();
171176
int destinationCapacity = newShape.getPrimitiveArrayCapacity();
177+
int sourceCapacity = thisShape.getPrimitiveArrayCapacity();
178+
if (sourceCapacity <= destinationCapacity) {
179+
return;
180+
}
181+
int[] newPrimitiveStore;
172182
if (destinationCapacity == 0) {
173-
if (oldPrimitiveStore != null) {
174-
object.setPrimitiveStore(null);
175-
}
176-
// else nothing to do
183+
newPrimitiveStore = DynamicObject.EMPTY_INT_ARRAY;
177184
} else {
178-
int sourceCapacity = thisShape.getPrimitiveArrayCapacity();
179-
if (sourceCapacity > destinationCapacity) {
180-
int[] newPrimitiveStore = new int[destinationCapacity];
181-
int destinationSize = newShape.getPrimitiveArraySize();
182-
UnsafeAccess.arrayCopy(oldPrimitiveStore, newPrimitiveStore, destinationSize);
183-
object.setPrimitiveStore(newPrimitiveStore);
184-
}
185+
newPrimitiveStore = new int[destinationCapacity];
186+
int destinationSize = newShape.getPrimitiveArraySize();
187+
UnsafeAccess.arrayCopy(oldPrimitiveStore, newPrimitiveStore, destinationSize);
185188
}
189+
object.setPrimitiveStore(newPrimitiveStore);
186190
}
187191

188192
@SuppressWarnings("deprecation")

0 commit comments

Comments
 (0)