@@ -206,6 +206,7 @@ public CodeUnit assemble() {
206
206
}
207
207
int [] boxingMetric = new int [varCount ];
208
208
byte [] shouldUnboxVariable = new byte [varCount ];
209
+ Arrays .fill (shouldUnboxVariable , (byte ) 0xff );
209
210
210
211
SortedSet <int []> finishedExceptionHandlerRanges = new TreeSet <>(Comparator .comparingInt (a -> a [0 ]));
211
212
@@ -243,7 +244,6 @@ public CodeUnit assemble() {
243
244
if (i .opcode == OpCodes .STORE_FAST ) {
244
245
variableStores .get (i .arg ).add (i );
245
246
} else if (i .opcode == OpCodes .LOAD_FAST ) {
246
- shouldUnboxVariable [i .arg ] |= i .quickenOutput ;
247
247
boxingMetric [i .arg ] += i .quickenOutput != 0 ? 1 : -1 ;
248
248
}
249
249
i .bci = buf .size ();
@@ -296,14 +296,24 @@ public CodeUnit assemble() {
296
296
}
297
297
}
298
298
}
299
- for (int i = 0 ; i < varCount ; i ++) {
300
- List <Instruction > stores = variableStores .get (i );
301
- finishedGeneralizeVarsMap [i ] = new int [stores .size ()];
302
- for (int j = 0 ; j < stores .size (); j ++) {
303
- finishedGeneralizeVarsMap [i ][j ] = stores .get (j ).bci ;
304
- }
305
- if (boxingMetric [i ] <= 0 ) {
306
- shouldUnboxVariable [i ] = 0 ;
299
+ if (!scope .isGenerator ()) {
300
+ /*
301
+ * We do an optimization in the interpreter that we don't unbox variables that would
302
+ * mostly get boxed again. This helps for interpreter performance, but for compiled code
303
+ * we have to unbox all variables otherwise the compiler is not always able to prove the
304
+ * variable was initialized. In generators, transferring the variables between the two
305
+ * modes of usage (boxed vs unboxed) would be too complex, so we skip the optimization
306
+ * there and unbox all variables.
307
+ */
308
+ for (int i = 0 ; i < varCount ; i ++) {
309
+ List <Instruction > stores = variableStores .get (i );
310
+ finishedGeneralizeVarsMap [i ] = new int [stores .size ()];
311
+ for (int j = 0 ; j < stores .size (); j ++) {
312
+ finishedGeneralizeVarsMap [i ][j ] = stores .get (j ).bci ;
313
+ }
314
+ if (boxingMetric [i ] <= 0 ) {
315
+ shouldUnboxVariable [i ] = 0 ;
316
+ }
307
317
}
308
318
}
309
319
return new CodeUnit (toTruffleStringUncached (name ), toTruffleStringUncached (qualName ),
0 commit comments