Skip to content

Commit 3466892

Browse files
committed
Avoid allocating empty arrays in code units
1 parent 19db61f commit 3466892

File tree

5 files changed

+73
-28
lines changed

5 files changed

+73
-28
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MarshalModuleBuiltins.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
import static com.oracle.graal.python.builtins.modules.io.IONodes.T_WRITE;
3232
import static com.oracle.graal.python.nodes.StringLiterals.T_VERSION;
3333
import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.isJavaString;
34+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_BOOLEAN_ARRAY;
35+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_DOUBLE_ARRAY;
36+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_INT_ARRAY;
37+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_LONG_ARRAY;
38+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_OBJECT_ARRAY;
39+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_SHORT_ARRAY;
40+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
3441
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
3542

3643
import java.io.ByteArrayInputStream;
@@ -1234,6 +1241,9 @@ private Object readJavaArray() {
12341241

12351242
private int[] readIntArray() {
12361243
int length = readInt();
1244+
if (length == 0) {
1245+
return EMPTY_INT_ARRAY;
1246+
}
12371247
int[] a = new int[length];
12381248
for (int i = 0; i < length; i++) {
12391249
a[i] = readInt();
@@ -1243,6 +1253,9 @@ private int[] readIntArray() {
12431253

12441254
private long[] readLongArray() {
12451255
int length = readInt();
1256+
if (length == 0) {
1257+
return EMPTY_LONG_ARRAY;
1258+
}
12461259
long[] a = new long[length];
12471260
for (int i = 0; i < length; i++) {
12481261
a[i] = readLong();
@@ -1252,6 +1265,9 @@ private long[] readLongArray() {
12521265

12531266
private double[] readDoubleArray() {
12541267
int length = readInt();
1268+
if (length == 0) {
1269+
return EMPTY_DOUBLE_ARRAY;
1270+
}
12551271
double[] a = new double[length];
12561272
for (int i = 0; i < length; i++) {
12571273
a[i] = readDouble();
@@ -1261,6 +1277,9 @@ private double[] readDoubleArray() {
12611277

12621278
private short[] readShortArray() {
12631279
int length = readInt();
1280+
if (length == 0) {
1281+
return EMPTY_SHORT_ARRAY;
1282+
}
12641283
short[] a = new short[length];
12651284
for (int i = 0; i < length; i++) {
12661285
a[i] = readShort();
@@ -1270,6 +1289,9 @@ private short[] readShortArray() {
12701289

12711290
private boolean[] readBooleanArray() {
12721291
int length = readInt();
1292+
if (length == 0) {
1293+
return EMPTY_BOOLEAN_ARRAY;
1294+
}
12731295
boolean[] a = new boolean[length];
12741296
for (int i = 0; i < length; i++) {
12751297
a[i] = readByte() != 0;
@@ -1279,6 +1301,9 @@ private boolean[] readBooleanArray() {
12791301

12801302
private TruffleString[] readStringArray() {
12811303
int length = readInt();
1304+
if (length == 0) {
1305+
return EMPTY_TRUFFLESTRING_ARRAY;
1306+
}
12821307
TruffleString[] a = new TruffleString[length];
12831308
for (int i = 0; i < length; i++) {
12841309
a[i] = readString();
@@ -1288,6 +1313,9 @@ private TruffleString[] readStringArray() {
12881313

12891314
private Object[] readObjectArray() {
12901315
int length = readInt();
1316+
if (length == 0) {
1317+
return EMPTY_OBJECT_ARRAY;
1318+
}
12911319
Object[] a = new Object[length];
12921320
for (int i = 0; i < length; i++) {
12931321
a[i] = readObject();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/OSErrorEnum.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,8 @@ public enum OSErrorEnum {
223223

224224
OSErrorEnum(int number, TruffleString message, TruffleString... alternativeMessages) {
225225
this.number = number;
226-
this.message = message != null ? message : null;
227-
this.alternativeMessages = new TruffleString[alternativeMessages.length];
228-
for (int i = 0; i < alternativeMessages.length; i++) {
229-
this.alternativeMessages[i] = alternativeMessages[i];
230-
}
226+
this.message = message;
227+
this.alternativeMessages = alternativeMessages;
231228
}
232229

233230
OSErrorEnum(int number, TruffleString message) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/Signature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public Signature(int positionOnlyArgIndex, boolean takesVarKeywordArgs, int take
7979
this.positionalOnlyArgIndex = positionOnlyArgIndex;
8080
this.takesVarKeywordArgs = takesVarKeywordArgs;
8181
this.varArgIndex = takesVarArgs;
82-
this.positionalParameterNames = (parameterIds != null) ? parameterIds : PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
83-
this.keywordOnlyNames = (keywordNames != null) ? keywordNames : PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
82+
this.positionalParameterNames = (parameterIds != null && parameterIds.length > 0) ? parameterIds : PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
83+
this.keywordOnlyNames = (keywordNames != null && keywordNames.length > 0) ? keywordNames : PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
8484
this.checkEnclosingType = checkEnclosingType;
8585
this.raiseErrorName = raiseErrorName;
8686
this.hidden = hidden;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/CompilationUnit.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
package com.oracle.graal.python.compiler;
4242

4343
import static com.oracle.graal.python.compiler.CompilationScope.Class;
44+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_INT_ARRAY;
45+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_LONG_ARRAY;
46+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_OBJECT_ARRAY;
47+
import static com.oracle.graal.python.util.PythonUtils.EMPTY_TRUFFLESTRING_ARRAY;
4448
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4549

4650
import java.io.ByteArrayOutputStream;
@@ -62,7 +66,6 @@
6266
import com.oracle.graal.python.pegparser.scope.Scope;
6367
import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
6468
import com.oracle.graal.python.util.PythonUtils;
65-
import com.oracle.truffle.api.strings.TruffleString;
6669

6770
public final class CompilationUnit {
6871
final String name;
@@ -259,12 +262,17 @@ public BytecodeCodeUnit assemble() {
259262
}
260263

261264
final int rangeElements = 4;
262-
int[] exceptionHandlerRanges = new int[finishedExceptionHandlerRanges.size() * rangeElements];
263-
int rangeIndex = 0;
264-
for (int[] range : finishedExceptionHandlerRanges) {
265-
assert range.length == rangeElements;
266-
System.arraycopy(range, 0, exceptionHandlerRanges, rangeIndex, rangeElements);
267-
rangeIndex += rangeElements;
265+
int[] exceptionHandlerRanges;
266+
if (finishedExceptionHandlerRanges.isEmpty()) {
267+
exceptionHandlerRanges = EMPTY_INT_ARRAY;
268+
} else {
269+
exceptionHandlerRanges = new int[finishedExceptionHandlerRanges.size() * rangeElements];
270+
int rangeIndex = 0;
271+
for (int[] range : finishedExceptionHandlerRanges) {
272+
assert range.length == rangeElements;
273+
System.arraycopy(range, 0, exceptionHandlerRanges, rangeIndex, rangeElements);
274+
rangeIndex += rangeElements;
275+
}
268276
}
269277

270278
byte[] finishedCanQuickenOutput = new byte[buf.size()];
@@ -300,9 +308,13 @@ public BytecodeCodeUnit assemble() {
300308
*/
301309
for (int i = 0; i < varCount; i++) {
302310
List<Instruction> stores = variableStores.get(i);
303-
finishedGeneralizeVarsMap[i] = new int[stores.size()];
304-
for (int j = 0; j < stores.size(); j++) {
305-
finishedGeneralizeVarsMap[i][j] = stores.get(j).bodyBci();
311+
if (!stores.isEmpty()) {
312+
finishedGeneralizeVarsMap[i] = new int[stores.size()];
313+
for (int j = 0; j < stores.size(); j++) {
314+
finishedGeneralizeVarsMap[i][j] = stores.get(j).bodyBci();
315+
}
316+
} else {
317+
finishedGeneralizeVarsMap[i] = EMPTY_INT_ARRAY;
306318
}
307319
if (boxingMetric[i] <= 0) {
308320
shouldUnboxVariable[i] = 0;
@@ -311,11 +323,11 @@ public BytecodeCodeUnit assemble() {
311323
}
312324
return new BytecodeCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(qualName),
313325
argCount, kwOnlyArgCount, positionalOnlyArgCount, flags,
314-
orderedKeys(names, new TruffleString[0], PythonUtils::toTruffleStringUncached), orderedKeys(varnames, new TruffleString[0], PythonUtils::toTruffleStringUncached),
315-
orderedKeys(cellvars, new TruffleString[0], PythonUtils::toTruffleStringUncached),
316-
orderedKeys(freevars, new TruffleString[0], cellvars.size(), PythonUtils::toTruffleStringUncached),
326+
orderedKeys(names, EMPTY_TRUFFLESTRING_ARRAY, PythonUtils::toTruffleStringUncached), orderedKeys(varnames, EMPTY_TRUFFLESTRING_ARRAY, PythonUtils::toTruffleStringUncached),
327+
orderedKeys(cellvars, EMPTY_TRUFFLESTRING_ARRAY, PythonUtils::toTruffleStringUncached),
328+
orderedKeys(freevars, EMPTY_TRUFFLESTRING_ARRAY, cellvars.size(), PythonUtils::toTruffleStringUncached),
317329
cell2arg,
318-
orderedKeys(constants, new Object[0]),
330+
orderedKeys(constants, EMPTY_OBJECT_ARRAY),
319331
startLocation.startLine,
320332
startLocation.startColumn,
321333
startLocation.endLine,
@@ -459,23 +471,29 @@ private static void emitBytecode(Instruction instr, ByteArrayOutputStream buf, S
459471
}
460472
}
461473

462-
private static <T, U> U[] orderedKeys(HashMap<T, Integer> map, U[] template, int offset, com.oracle.graal.python.util.Function<T, U> convertor) {
463-
U[] ary = Arrays.copyOf(template, map.size());
474+
private static <T, U> U[] orderedKeys(HashMap<T, Integer> map, U[] empty, int offset, com.oracle.graal.python.util.Function<T, U> convertor) {
475+
if (map.isEmpty()) {
476+
return empty;
477+
}
478+
U[] ary = Arrays.copyOf(empty, map.size());
464479
for (Map.Entry<T, Integer> e : map.entrySet()) {
465480
ary[e.getValue() - offset] = convertor.apply(e.getKey());
466481
}
467482
return ary;
468483
}
469484

470-
private static <T> T[] orderedKeys(HashMap<T, Integer> map, T[] template) {
471-
return orderedKeys(map, template, 0, i -> i);
485+
private static <T> T[] orderedKeys(HashMap<T, Integer> map, T[] empty) {
486+
return orderedKeys(map, empty, 0, i -> i);
472487
}
473488

474-
private static <T, U> U[] orderedKeys(HashMap<T, Integer> map, U[] template, com.oracle.graal.python.util.Function<T, U> convertor) {
475-
return orderedKeys(map, template, 0, convertor);
489+
private static <T, U> U[] orderedKeys(HashMap<T, Integer> map, U[] empty, com.oracle.graal.python.util.Function<T, U> convertor) {
490+
return orderedKeys(map, empty, 0, convertor);
476491
}
477492

478493
private static long[] orderedLong(HashMap<Long, Integer> map) {
494+
if (map.isEmpty()) {
495+
return EMPTY_LONG_ARRAY;
496+
}
479497
long[] ary = new long[map.size()];
480498
for (Map.Entry<Long, Integer> e : map.entrySet()) {
481499
ary[e.getValue()] = e.getKey();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/PythonUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import javax.management.ObjectName;
6363
import javax.management.ReflectionException;
6464

65-
import com.oracle.truffle.api.Assumption;
6665
import org.graalvm.nativeimage.VMRuntime;
6766
import org.graalvm.polyglot.io.ByteSequence;
6867

@@ -84,6 +83,7 @@
8483
import com.oracle.graal.python.pegparser.tokenizer.CodePoints;
8584
import com.oracle.graal.python.runtime.PythonOptions;
8685
import com.oracle.graal.python.runtime.object.PFactory;
86+
import com.oracle.truffle.api.Assumption;
8787
import com.oracle.truffle.api.CallTarget;
8888
import com.oracle.truffle.api.CompilerAsserts;
8989
import com.oracle.truffle.api.CompilerDirectives;
@@ -144,6 +144,8 @@ public static TruffleStringBuilderUTF32 createStringBuilder() {
144144
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
145145
public static final int[] EMPTY_INT_ARRAY = new int[0];
146146
public static final long[] EMPTY_LONG_ARRAY = new long[0];
147+
public static final short[] EMPTY_SHORT_ARRAY = new short[0];
148+
public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
147149
public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
148150
public static final char[] EMPTY_CHAR_ARRAY = new char[0];
149151
public static final Assumption[] EMPTY_ASSUMPTION_ARRAY = new Assumption[0];

0 commit comments

Comments
 (0)