July 13, 2019 if parameter never used, is ldc smart enough to remove it ? | ||||
---|---|---|---|---|
| ||||
for example: void enforce(bool val, string file = __FILE__, size_t line = __LINE__){ if( !val ) { debug printf("%s, from line %d\n", file.ptr, line); abort(); } } __gshared int base = 10; int somefn(int val, string file = __FILE__, size_t line = __LINE__) { enforce(val > 0, file, line); return val + base ; } void main(){ int i = somefn(1); } when build for release, will the parameter reduce into 1 for function somefn && enforce ? and how to verify this ? |
July 16, 2019 Re: if parameter never used, is ldc smart enough to remove it ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Newbie2019 | On Saturday, 13 July 2019 at 06:33:35 UTC, Newbie2019 wrote: > for example: > > void enforce(bool val, string file = __FILE__, size_t line = __LINE__){ > if( !val ) { > debug printf("%s, from line %d\n", file.ptr, line); > abort(); > } > } > > __gshared int base = 10; > int somefn(int val, string file = __FILE__, size_t line = __LINE__) { > enforce(val > 0, file, line); > return val + base ; > } > > void main(){ > int i = somefn(1); > } > > when build for release, will the parameter reduce into 1 for function somefn && enforce ? Well I put it into http://d.godbolt.org and it seems to be smart enough to remove all the code from main entirely. https://d.godbolt.org/z/o-pKBL Literally compiling your main to :- _Dmain: xor eax, eax ret If I add a fake function for the value passed in so that it can't just determine at compile time that main needs to do nothing I get this :- https://d.godbolt.org/z/ypgBK2 It has inlined the functions and removed anything to do with the parameters that are not used. |
Copyright © 1999-2021 by the D Language Foundation