Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply __attribute__((const)) where applicable #1982

Open
albinahlback opened this issue May 17, 2024 · 5 comments
Open

Apply __attribute__((const)) where applicable #1982

albinahlback opened this issue May 17, 2024 · 5 comments

Comments

@albinahlback
Copy link
Collaborator

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.

@math-gout
Copy link
Contributor

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

@albinahlback
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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.

@fredrik-johansson
Copy link
Collaborator

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 :-)

@albinahlback
Copy link
Collaborator Author

That's true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants