dataobjects-net icon indicating copy to clipboard operation
dataobjects-net copied to clipboard

Optimize code generated by Weaver using `.tail` prefix

Open SergeiPavlov opened this issue 1 year ago • 0 comments

Assembly code generated by JIT for Entity.Field getter:

before optimization (without .tail):

push        rbp  
sub         rsp,20h  
lea         rbp,[rsp+20h]  
mov         qword ptr [rbp+10h],rcx  
mov         r8,1914C813828h  
mov         r8,qword ptr [r8]  
mov         rcx,qword ptr [rbp+10h]  
mov         rdx,7FFA24FE6D40h  
call        qword ptr [CLRStub[MethodDescPrestub]@00007FFA24FCB078 (07FFA24FCB078h)]  ; GetFieldValue<>()
nop  
add         rsp,20h  
pop         rbp  
ret 

After optimization (with .tail):

mov         r8,1F7CFC13828h  
mov         r8,qword ptr [r8]  
mov         rdx,7FFA24FE20B0h  
jmp         qword ptr [CLRStub[MethodDescPrestub]@00007FFA25001FA8 (07FFA25001FA8h)]   ; GetFieldValue<>()

The same effect for setters (SetFieldValue())

The assembly was build with enabled optimization.

The JIT itself does not always apply this kind of optimization see https://stackoverflow.com/questions/491376/why-doesnt-net-c-optimize-for-tail-call-recursion https://github.com/dotnet/csharplang/issues/2304

SergeiPavlov avatar Dec 28 '23 23:12 SergeiPavlov