flint icon indicating copy to clipboard operation
flint copied to clipboard

Apply `__attribute__((const))` where applicable

Open albinahlback opened this issue 1 year ago • 5 comments

I've seen functions which does something like

for (j = 0; j < n_pow(g, n); j++)

Assuming that g and n does not change, GCC cannot optimize this as it doesn't know if the output will be different each time. Pushing __attribute__((const)) will make it understand it.

This should be pushed onto every function where it applies.

See GCC's documentation for more information.

albinahlback avatar May 17 '24 14:05 albinahlback

Is there a reason for not doing m = n_pow(g, n) and then for (j = 0; j < m; j++) ?

math-gout avatar May 18 '24 13:05 math-gout

It is cheap compared to searching for all such instances as they don't necessarily have to be contained within for-loops. It can also lead to more readable code.

albinahlback avatar May 18 '24 18:05 albinahlback

Personally, I am a big fan of attributes since that hints the compiler that it can assume even more things. Of course, one should still try to optimize the code as much as possible, as this is only valid for GCC-compatible compilers.

albinahlback avatar May 18 '24 18:05 albinahlback

I don't think there are too many of these.

One of the drawbacks of this attribute is that the compiler can start to optimize out benchmarked function calls from benchmarking loops :-)

fredrik-johansson avatar May 20 '24 22:05 fredrik-johansson

That's true.

albinahlback avatar May 20 '24 22:05 albinahlback