4141package com .oracle .graal .python .compiler ;
4242
4343import 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 ;
4448import static com .oracle .graal .python .util .PythonUtils .toTruffleStringUncached ;
4549
4650import java .io .ByteArrayOutputStream ;
6266import com .oracle .graal .python .pegparser .scope .Scope ;
6367import com .oracle .graal .python .pegparser .tokenizer .SourceRange ;
6468import com .oracle .graal .python .util .PythonUtils ;
65- import com .oracle .truffle .api .strings .TruffleString ;
6669
6770public 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 ();
0 commit comments