Optimizing Immutable and Purity

bearophile bearophileHUGS at lycos.com
Mon Dec 22 14:51:12 PST 2008


Multiplication and pointer operations are both very common operation in C code, so this code shows well why I don't like them to be represented by the same symbol. Pascal uses ^ for poinetr operation (and ^ isn't used for the bitwise operation):

int foo(int* a, int* b)
{   int i = *a * *b;
    int j = *a * *b;
    return i + j;
}


>By the very definition of purity, you won't be able to to ever notice the absent call unless you are willing to place a precise thermal probe on the processor and notice a drop in the heat produced.<

This sounds a little silly :-]


>one rule that is fairly consistent is that fewer instructions execute faster.<

Generally this isn't much true anymore.


>On a real program, how much faster is my code going to get, if I maximize use of pure functions and immutable data? I don't know. I don't have any experience with it on a large program.<

Maybe some programmer already used to pure functional programming can give you some answer.

Is GCC able to perform similar optimizations (some of them, not all of them) when you use the "pure" attribute to functions?
See  int square (int) __attribute__ ((pure));  in this page:
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

A very readable article, but I don't have much other comments to add on-topic.

Bye,
bearophile



More information about the Digitalmars-d mailing list