[Article Submission] Have Your Efficiency, and Flexibility Too
Timon Gehr
timon.gehr at gmx.ch
Tue May 31 06:42:10 PDT 2011
> Timon Gehr:
>
>> Nice, but isSpinnable is always checked twice with your approach.
>
> Right. But isn't the compiler able to optimize away this inefficiency?
Nope. You don't get a guarantee from the language that your code will be optimized
in a certain way.
That said, dmd/gdc are _not_ able to do it sensibly for the most trivial case:
if(x) writeln("x");
if(!x) writeln("!x");
results in the following when compiled with dmd -O -release -inline.
cmp dword ptr [ebp-8], 0 ; first check
jz short loc_8096042 ; first check
push dword ptr ds:_TMP1+4
push dword ptr ds:_TMP1
call _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv
loc_8096042:
cmp dword ptr [ebp-8], 0 ; second check
jnz short loc_8096059 ; second check
push dword ptr ds:_TMP2+4
push dword ptr ds:_TMP2
call _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv
loc_8096059:
; function epilogue comes here.
With gdmd -O -release -inline:
mov ecx, [esp+20h+var_4] ; first check
test ecx, ecx ; first check
jnz short loc_804AE98 ; first check
loc_804AE7D:
mov [esp+20h+var_20], 2
mov [esp+20h+var_1C], offset unk_80A3448 ;"!x"
call _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv
; function epilogue comes here
loc_804AE98:
mov [esp+20h+var_20], 1
mov [esp+20h+var_1C], offset asc_80A3446 ; "x"
call _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv
mov edx, [esp+20h+var_4] ; second check (nonsense)
test edx, edx ; second check (nonsense)
jz short loc_804AE7D ; second check (nonsense)
; function epilogue comes here
This is _really_ silly oO. gdc was almost there. It clearly had to understand that
x and !x are mutually exclusive to generate such code (it _reordered_ the two
checks =DD), but then it created 3 totally nonsensical instructions for a totally
redundant second check nevertheless. It still gets some credit for duplicating the
function epilogue instead of adding another jump.
Compilers are not magical, they are stupid programs that cannot replace your
insight. ;)
>
>
>> I would like an explicit "static foreach" better though.
>
> See:
> http://d.puremagic.com/issues/show_bug.cgi?id=4085
>
> Bye,
> bearophile
Thanks for the information. I already suspected it to be a matter of implementation.
Timon
More information about the Digitalmars-d
mailing list