Excerpts from the RavenDB Performance Team Report: Optimizers, Assemble!
Join the DZone community and get the full member experience.Join For Free
[this article was written by federico lois]
in the previous post on the topic after inspecting the decompiled source using ilspy we were able to uncover potential things we could do.
do you remember the loop from a couple of posts behind?
this loop was the responsible to check for the last bytes; either because the words compare found a difference or we are at the end of the memory block and the last block is not 8 bytes wide. you may remember it easier because of the awful code it generated.
after optimization work that loop looked sensibly different:
and we can double-check that the optimization did create a packed msil.
now let’s look at the different operations involved. in red, blue and orange we have unavoidable instructions, as we need to do the comparison to be able to return the result.
13 instructions to do the work and 14 for the rest. half of the operations are housekeeping in order to prepare for the next iteration.
the experienced developer would notice that if we would have done this on the jit output each one of the increments and decrements can be implemented with a single assembler operation using specific addressing mode. however, we shouldn’t underestimate the impact of memory loads and stores.
how would the following loop look in pseudo-assembler?
:start load address of lhs into register load address of r into raddr-inregister move value of lhs into r load byte into a 32 bits register (lhs-inregister) substract rhs-inregister, [raddr-inregister], load int32 from r into r-inregister jump :wearedone if non zero r-inregister, load address of lhs into lhsaddr-register add 4 into [lhsaddr-register] (immediate-mode) load address of rhs into rhsaddr-register add 4 into [rhsaddr-register] (immediate-mode) load address of n into naddr-register increment [naddr-register] load content of n into n-register jump :start if bigger than zero n-register push 0 return :wearedone push r-inregister return
as you can see the housekeeping keeps being a lot of operations. j
armed with this knowledge. how would you optimize this loop?
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.