Skip to content

Commit 88cc5a9

Browse files
committed
Initialize extension arrays with empty array constant instead of null.
1 parent f0e344d commit 88cc5a9

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-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
@@ -174,6 +174,9 @@
174174
@SuppressWarnings("deprecation")
175175
public abstract class DynamicObject implements TruffleObject {
176176

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

179182
/**
@@ -193,9 +196,9 @@ public abstract class DynamicObject implements TruffleObject {
193196
private Shape shape;
194197

195198
/** Object extension array. */
196-
@DynamicField private Object[] extRef;
199+
@DynamicField private Object[] extRef = EMPTY_OBJECT_ARRAY;
197200
/** Primitive extension array. */
198-
@DynamicField private int[] extVal;
201+
@DynamicField private int[] extVal = EMPTY_INT_ARRAY;
199202

200203
/**
201204
* 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: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ 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+
if (targetCapacity == 0) {
81+
return;
82+
}
8083
Object[] newObjectStore = new Object[targetCapacity];
8184
if (sourceCapacity != 0) {
8285
int sourceSize = thisShape.getObjectArraySize();
@@ -87,6 +90,9 @@ private static void growObjectStore(DynamicObject object, Shape thisShape, int s
8790
}
8891

8992
private static void growPrimitiveStore(DynamicObject object, Shape thisShape, int sourceCapacity, int targetCapacity) {
93+
if (targetCapacity == 0) {
94+
return;
95+
}
9096
int[] newPrimitiveArray = new int[targetCapacity];
9197
if (sourceCapacity != 0) {
9298
int sourceSize = thisShape.getPrimitiveArraySize();
@@ -108,81 +114,83 @@ static void trimToSize(DynamicObject object, Shape thisShape, Shape otherShape)
108114

109115
private static void resizeObjectStore(DynamicObject object, Shape oldShape, Shape newShape) {
110116
int destinationCapacity = newShape.getObjectArrayCapacity();
117+
int sourceCapacity = oldShape.getObjectArrayCapacity();
118+
if (sourceCapacity == destinationCapacity) {
119+
return;
120+
}
121+
Object[] newObjectStore;
111122
if (destinationCapacity == 0) {
112-
object.setObjectStore(null);
123+
newObjectStore = DynamicObject.EMPTY_OBJECT_ARRAY;
113124
} 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);
125+
int sourceSize = oldShape.getObjectArraySize();
126+
newObjectStore = new Object[destinationCapacity];
127+
if (sourceSize != 0) {
128+
Object[] oldObjectStore = object.getObjectStore();
129+
int destinationSize = newShape.getObjectArraySize();
130+
int length = Math.min(sourceSize, destinationSize);
131+
UnsafeAccess.arrayCopy(oldObjectStore, newObjectStore, length);
125132
}
126133
}
134+
object.setObjectStore(newObjectStore);
127135
}
128136

129137
private static void resizePrimitiveStore(DynamicObject object, Shape oldShape, Shape newShape) {
130138
assert newShape.hasPrimitiveArray();
131139
int destinationCapacity = newShape.getPrimitiveArrayCapacity();
140+
int sourceCapacity = oldShape.getPrimitiveArrayCapacity();
141+
if (sourceCapacity == destinationCapacity) {
142+
return;
143+
}
144+
int[] newPrimitiveArray;
132145
if (destinationCapacity == 0) {
133-
object.setPrimitiveStore(null);
146+
newPrimitiveArray = DynamicObject.EMPTY_INT_ARRAY;
134147
} 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);
148+
int sourceSize = oldShape.getPrimitiveArraySize();
149+
newPrimitiveArray = new int[destinationCapacity];
150+
if (sourceSize != 0) {
151+
int[] oldPrimitiveArray = object.getPrimitiveStore();
152+
int destinationSize = newShape.getPrimitiveArraySize();
153+
int length = Math.min(sourceSize, destinationSize);
154+
UnsafeAccess.arrayCopy(oldPrimitiveArray, newPrimitiveArray, length);
146155
}
147156
}
157+
object.setPrimitiveStore(newPrimitiveArray);
148158
}
149159

150160
private static void trimObjectStore(DynamicObject object, Shape thisShape, Shape newShape) {
151161
Object[] oldObjectStore = object.getObjectStore();
152162
int destinationCapacity = newShape.getObjectArrayCapacity();
163+
int sourceCapacity = thisShape.getObjectArrayCapacity();
164+
if (sourceCapacity <= destinationCapacity) {
165+
return;
166+
}
167+
Object[] newObjectStore;
153168
if (destinationCapacity == 0) {
154-
if (oldObjectStore != null) {
155-
object.setObjectStore(null);
156-
}
157-
// else nothing to do
169+
newObjectStore = DynamicObject.EMPTY_OBJECT_ARRAY;
158170
} 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-
}
171+
newObjectStore = new Object[destinationCapacity];
172+
int destinationSize = newShape.getObjectArraySize();
173+
UnsafeAccess.arrayCopy(oldObjectStore, newObjectStore, destinationSize);
166174
}
175+
object.setObjectStore(newObjectStore);
167176
}
168177

169178
private static void trimPrimitiveStore(DynamicObject object, Shape thisShape, Shape newShape) {
170179
int[] oldPrimitiveStore = object.getPrimitiveStore();
171180
int destinationCapacity = newShape.getPrimitiveArrayCapacity();
181+
int sourceCapacity = thisShape.getPrimitiveArrayCapacity();
182+
if (sourceCapacity <= destinationCapacity) {
183+
return;
184+
}
185+
int[] newPrimitiveStore;
172186
if (destinationCapacity == 0) {
173-
if (oldPrimitiveStore != null) {
174-
object.setPrimitiveStore(null);
175-
}
176-
// else nothing to do
187+
newPrimitiveStore = DynamicObject.EMPTY_INT_ARRAY;
177188
} 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-
}
189+
newPrimitiveStore = new int[destinationCapacity];
190+
int destinationSize = newShape.getPrimitiveArraySize();
191+
UnsafeAccess.arrayCopy(oldPrimitiveStore, newPrimitiveStore, destinationSize);
185192
}
193+
object.setPrimitiveStore(newPrimitiveStore);
186194
}
187195

188196
@SuppressWarnings("deprecation")

0 commit comments

Comments
 (0)