in c and asssembler, you can unroll loops at build time so that instead of:
mov eax,320you have 320 do somethings..
beginning_of_loop:
;do something
dec eax
jnz beginning_of_loop
beginning_of_loop:
;do something
;do something
;do something
;do something
;do something
... (314 more)
;do something
if you want to do it less than 320 times,
you jump to beginning_of_loop + size_of 'do something' * (320-how_many_times_you_want_to_do_it) instead of beginning_of_loop.
but if do something looks like:
temp=0;you won't know what a is until runtime. so petzold writes about unrolling this part on the fly. and he claims his generated MSIL-code runs at 4 times the speed of the c# version.
for (i = 0; i < a; i++)
temp += s[offset+i];
end
d[offset++] = temp/a;
this is interesting. because a modern JIT compiler should be able to unroll this part too.
does it? how can I see the assembler code that the jit actually runs? how many iterations are needed before the jit recompiles this loop into the fastest possible inner loop?
pekka pls explain.
also, in this case you could just keep temp, substract s[offset-1] and add s[offset+a] instead of going through the loop at all.
2 kommenttia:
I have not tried it myself, but there should be a command line option ("-XX:+PrintAssembly" or similar) in debug builds of Hotspot that dumps the generated machine code for each compiled method.
As for loop unrolling, it's a very basic compiler optimization which tries to improve instruction level parallelism (ILP) so that out-of-order (OoO) CPUs can execute your code faster. Usually, you unroll the loop only a few times, as unrolling all iterations makes the size of the executed code huge which can hurt performance (think instruction cache here).
I don't think Hotspot does adaptive optimizations (continuously improving the executed code) so you should see the effects of loop unrolling immediately after the executed method is compiled into machine code if the optimizer thinks it's a gain.
hi. thanks for the comment.
I thought it did adaptive optimizations, atleast on the Server HotSpot.
http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html
-XX:CompileThreshold=n
Controls how many times a method is executed before compiling or
re-optimizing.
-XX:+PrintCompilation
Prints handy information about HotSpot compilation.
Post a Comment