Is there any real reason to use "const"?

Paul Backus snarwin at
Mon Jan 24 23:43:23 UTC 2022

On Monday, 24 January 2022 at 20:09:46 UTC, H. S. Teoh wrote:
> Makes me curious: how feasible is it to have functions with 
> identical bodies merge together as well?

Cursory reseach suggests that the main obstacle to this is 
function pointers. Specifically, if two different functions with 
identical bodies both have their addresses taken, and the 
resulting pointers are compared with ==, the result must be 
`false`. Unless the compiler (or linker, in the case of LTO) can 
prove that a comparison like this never happens, the optimization 
is invalid.

An easier optimization is to "factor out" the bodies of identical 
functions into a single new function, and have the original 
functions simply forward to the new one. For example, when 
presented with

     int f(int x, int y) { return x^^2 - y + 42; }
     int g(int x, int y) { return x^^2 - y + 42; }

...the compiler (or linker) could emit something like

     int f(int x, int y) { return __generated(x, y); }
     int g(int x, int y) { return __generated(x, y); }

     int __generated(int x, int y) { return x^^2 - y + 42; }

With tail-call optimization, the overhead of the additional 
function call is extremely small.

More information about the Digitalmars-d mailing list