[fpc-devel] Pure function Wiki page

J. Gareth Moreton gareth at moreton-family.com
Mon Jul 9 13:00:53 CEST 2018


Besides being able to assign the function 
result to constants, the main benefit is 
that, for constant inputs, the result is 
deterministic and so the compiler can 
calculate this beforehand and replace a 
function call with an assignment. For 
example, if tan was a regular function, x 
:= tan(Pi / 2) might look like this in 
assembly:

MOVAPD XMM0, Pi
DIVSD XMM0, TwoConstant
CALL tan ; Result is now in XMM0, which is 
1

It won't be exactly that, but you get the 
idea. If tan is defined as a pure 
function, then the result can be pre-
calculated and the entire block changed to 
a simple assignment:

MOVAPD XMM0, OneConstant ; value of tan(Pi 
/ 2)

It doesn't take a genius to know that that 
is faster by several orders of magnitude.

More subtly, if it turns out that the 
function is never called with variable 
arguments, the entire function can be 
removed from the compiled binary, reducing 
its size.

Gareth aka. Kit

On Mon 09/07/18 10:25 , Sven Barth via 
fpc-devel fpc-devel at lists.freepascal.org 
sent:
> Ryan Joseph  schrieb am Mo., 9. Juli 
2018, 08:45:
> 
> > On Jul 8, 2018, at 8:50 AM, J. Gareth 
Moreton  wrote:
> > 
> > With some blessing from Florian on the 
concept, I've set up a Wiki page
> discussing the design proposals for the 
support of pure functions, as well
> as some explanation on what they 
actually are.
> > 
> 
> What are the performance benefits? It 
sounds like this is a proposal for a
> compiler optimization which we can 
explicitly opt in to, but what exactly
> is the optimization?
> 
> If nothing else I like the idea as a way 
to enforce a function is not
> accessing global state. Kind of like 
const for functions.
> 
> It would allow you to declare constants 
that use those functions with the
> compiler evaluating them at compile 
time. Also the compiler might be able
> to optimize such functions better 
especially if they're inlined. *shrugs*
> Regards,  Sven   
__________________________________________
_____
> fpc-devel maillist - fpc-
devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-
bin/mailman/listinfo/fpc-devel [1]
> 
> 
> 
> Links:
> ------
> [1]
> http://secureweb.fast.net.uk/parse.php?
redirect=http://lists.freepascal.org
> /cgi-bin/mailman/listinfo/fpc-devel
> 




More information about the fpc-devel mailing list