java - Why javac sometimes creates unnecessary copies of variables? -
java - Why javac sometimes creates unnecessary copies of variables? -
i have code looks this:
boolean[] array = new boolean[200]; int[] indexes = {10, 42, 62, 74}; while(true) { //some code here stringbuilder sb = new stringbuilder(); (int j : indexes) { sb.append(array[j] ? '1' : '0'); } }
bytecode this:
astore 3 //"indexes" array ... aload 3 astore 8 aload 8 arraylength ...
i not sure why javac re-create ref array local var.
the for-each loop converted this:
{ int[] hidden_array_ref = indexes; int hidden_length = hidden_array_ref.length; for(int hidden_counter = 0; hidden_counter < hidden_length; hidden_counter++) { int j = hidden_array_ref[hidden_counter]; sb.append(array[j] ? '1' : '0'); } }
in particular, notice int[] hidden_array_ref = indexes;
. that's re-create asking about.
the compiler way if write like:
for(int j : indexes) { indexes = new int[0]; sb.append(array[j] ? '1' : '0'); }
the assignment indexes
doesn't impact loop.
java javac bytecode
Comments
Post a Comment