[cfe-dev] "You don't pay for what you don't use" principle violation?

Mehdi Amini mehdi.amini at silkan.com
Wed May 7 12:15:13 PDT 2014


On 5/4/14, 3:08 PM, Fernando Pelliccioni wrote:
> Hi all,
>
> Is there any reason why the following code produces the attached 
> assembly code?
>
>     struct X { int v; };
>
>     int func( X** x, int n )
>     {
>         int sum = 0;
>
>         while ( n > 0 )
>         {
>             sum += (*x)->v;
>             ++x;
>             --n;
>         }
>         return sum;
>     }
>
>     int main() {}
>
> The "func" function is not used, I think it should be omitted by the 
> compiler.
>

Did you think that "You don't pay for what you don't use" is about dead 
code? It is not about "use" like "using a function", but about language 
features.
You are indeed "using" a feature here, and you are paying for (by 
increasing your code size only). The feature of the language you are 
using is "I want my function to be externally visible".
Even the linker cannot always optimise these cases (think of a shared 
library later loaded with dlopen).

Best,

Mehdi

PS: by the way I'm pretty sure an even more simple example with a one 
line function was possible :)




More information about the cfe-dev mailing list