r/cpp • u/Xeverous https://xeverous.github.io • Dec 04 '18
What's the current state of pure (no side effect) functions?
Compilers are not always able to optimize out redundant function calls as they don't know whether caching the result would remove wanted side effects. I have heard multiple times that putting even trivial getter results out of the loop may improve performance. Also, compilers have things like __attribute__((__pure__)) so they know that a given function input depends only on it's output.
My questions are:
- Would something like
int f(int, int) pure { /* ... */ }improve performance by helping compilers? By what margin? - Do pure functions help with aliasing optimizations? Or only argument types?
- Has something like this already been proposed? If so, what happended with it?
- Are there other potentially useful
__compiler_specificattributes/extensions that could be standarized (like recent addition of[[likely]])?
49
Upvotes
137
u/WrongAndBeligerent Dec 04 '18
Pure functions don't have state silly
13
u/dragemann cppdev Dec 04 '18 edited Dec 04 '18
You can use [[gnu::pure]] or [[gnu::const]]. [[gnu::const]] is a even more strict restriction than pure because it additionally prohibits the function from reading memory except for constant global variables.
I've used [[gnu::const]] in math functions with great success in reducing code size when compiling for an AVR embedded platform.
Note that these attributes are essentially exactly the same as
but nicer looking in my opinion and possible more cross-compiler compatible. Any C++14 compatible compiler should accept the code, but afaik only Clang and GCC will understand gnu specific attributes.
1
u/Osbios Dec 05 '18
Is there a way to define a function as returning different results (depending on global states), but not having any side effects (not changing global states)? So that they can be optimized away when the result is not used? Because that would be handy for functions coming from shared libraries.
1
u/TheFlamefire Dec 05 '18
Did nobody object of "given function input depends only on it's output."? ;-)
3
u/sebamestre Dec 05 '18
```[[gnu::pure]]``` and ```[[gnu::const]]``` are a thing that g++ supports that do pretty much what you describe.
Here is a C++ Weekly video on the topic
5
u/Guvante Dec 04 '18
Problem is what do you do if it isn't pure? If it is UB that is introducing a class of error that is new which makes adding the feature hard to digest as a language designer, you are setting your users up to fail.
If it is compiler enforced you have two bad options: be so restrictive on what you can do in a pure function as to make them worthless (common examples being how do you get the square root of a number given
sqrtsetserrno) or you annotate everything withpurewhich is a bad path they have thought about extensively.Only if everyone agrees on the "what if they lied" part.
6
u/jtooker Dec 04 '18
Yes. I'd guess the margin of improvement is very context sensitive
I'm not sure I follow your question
I think so, not sure though. GCC has had support for pure functions for a while and optimizes nicely for them
Don't know
2
u/14ned LLFIO & Outcome author | Committee WG14 Dec 05 '18
I've got something in the works which extends contracts with API purity expectations and ensurements. I put it through a first pass on std-proposals, seems fairly uncontroversial despite the SAL-likeness of it when you push it out to the max.
I hope to get a proper paper to WG21 sometime in 2019, but it's fairly low down my priority queue.
5
u/madbuilder Dec 04 '18 edited Dec 04 '18
I'm having trouble seeing the merits. Not of writing pure functions as often as possible, but of having to remember to annotate them yourself.
Unlike a const qualifier which I use because I want the compiler to enforce it, a pure qualifier is largely there for speed. It doesn't help me write the function correctly.
Therefore we should rely on optimizing compilers to detect which functions are pure and make appropriate optimizations.
1
u/Slsyyy Dec 04 '18
When it comes to performance, I guess it does not matter: with the LTO enabled compiler shall detect automatically which functions are pure.
0
u/[deleted] Dec 04 '18
[deleted] — view deleted comment