tags:

views:

583

answers:

5

Hi,

I'm thinking of using pure/const functions more heavily in my C++ code. (pure/const attribute in GCC)

However, I am curious how strict I should be about it and what could possibly break.

The most obvious case are debug outputs (in whatever form, could be on cout, in some file or in some custom debug class). I probably will have a lot of functions, which don't have any side effects despite this sort of debug output. No matter if the debug output is made or not, this will absolutely have no effect on the rest of my application.

Or another case I'm thinking of is the use of some SmartPointer class which may do some extra stuff in global memory when being in debug mode. If I use such an object in a pure/const function, it does have some slight side effects (in the sense that some memory probably will be different) which should not have any real side effects though (in the sense that the behaviour is in any way different).

Similar also for mutexes and other stuff. I can think of many complex cases where it has some side effects (in the sense of that some memory will be different, maybe even some threads are created, some filesystem manipulation is made, etc) but has no computational difference (all those side effects could very well be left out and I would even prefer that).

So, to summarize, I want to mark functions as pure/const which are not pure/const in a strict sense. An easy example:

int foo(int) __attribute__((const));

int bar(int x) {
   int sum = 0;
   for(int i = 0; i < 100; ++i)
       sum += foo(x);
   return sum;
}

int foo_callcounter = 0;

int main() {
   cout << "bar 42 = " << bar(42) << endl;
   cout << "foo callcounter = " << foo_callcounter << endl;
}

int foo(int x) {
   cout << "DEBUG: foo(" << x << ")" << endl;
   foo_callcounter++;
   return x; // or whatever
}

Note that the function foo is not const in a strict sense. Though, it doesn't matter what *foo_callcounter* is in the end. It also doesn't matter if the debug statement is not made (in case the function is not called).

I would expect the output:

DEBUG: foo(42)
bar 42 = 4200
foo callcounter = 1

And without optimisation:

DEBUG: foo(42) (100 times)
bar 42 = 4200
foo callcounter = 100

Both cases are totally fine because what only matters for my usecase is the return value of bar(42).

How does it work out in practice? If I mark such functions as pure/const, could it break anything (considering that the code is all correct)?


Note that I know that some compilers might not support this attribute at all. (Btw., I am collecting them here.) I also know how to make use of thes attributes in a way that the code stays portable (via #defines). Also, all compilers which are interesting to me support it in some way; so I don't care about if my code runs slower with compilers which do not.

I also know that the optimised code probably will look different depending on the compiler and even the compiler version.


Now, this question has become already a bit old and I still have not gotten an answer for my question itself. And I am still curious about it.

+3  A: 

You could definitely break the portability of your code. And why would you want to implement your own smart pointer - learning experience apart? Aren't there enough of them available for you in (near) standard libraries?

anon
How could I break the compatibility, given that I would only use it for cases where it would have any difference in behaviour / the computational result?The SmartPointer mainly was coded to be able to add custom debugging code when needed. Also, I needed a possiblity for some C-structs to run a custom cleanup function instead of just a 'delete'. (Ofc could have done that also via some wrapper class.) And there was no smartptr in any of the libs we used at that point. We already are migrating to boost::shared_ptr right now.It doesnt matter that much for my question which smartptr is used.
Albert
@Albert I didn't say "compatibility" I said "portability" - attributes are not a Standard C++ feature. If you compile the code with another compiler, the chances are it won't recognise the __attribute__ tag, or if it does will not treat the specific attribute in the same way that GCC does.
anon
@Neil If you're targeting other compilers you could just #define __attribute__(x) and have the code compile so it won't be that bad. Also, sometimes portability is less important then performance.
Jasper Bekkers
@Jasper In my experience, the quality of the compiler emitted code, even with no optimisations is almost never (never in any commercial code I've written) the performance bottleneck. Making ones code unreadable with macros and conditional compilation for the sake of shaving off a few microseconds (or probably not doing so) seems like a bad ROI to me.
anon
Yes I know that there might be compilers that don't support it. And I also know that the optimised code might look somewhat different in each compiler (even compiler version).
Albert
+18  A: 

I'm thinking of using pure/const functions more heavily in my C++ code.

That’s a slippery slope. These attributes are non-standard and their benefit is restricted mostly to micro-optimizations.

That’s not a good trade-off. Write clean code instead, don’t apply such micro-optimizations unless you’ve profiled carefully and there’s no way around it. Or not at all.

Notice that in principle these attributes are quite nice because they state implied assumptions of the functions explicitly for both the compiler and the programmer. That’s good. However, there are other methods of making similar assumptions explicit (including documentation). But since these attributes are non-standard, they have no place in normal code. They should be restricted to very judicious use in performance-critical libraries where the author tries to emit best code for every compiler. That is, the writer is aware of the fact that only GCC can use these attributes, and has made different choices for other compilers.

Konrad Rudolph
That's what `#define` is for, so you can document these facets of the code for future programmers, and also let the compiler in on the secret where supported.
Ben Voigt
I see the nitwit downvoters are out again - upvoted.
anon
I know that these attributes are GCC only. Whereby probably some other compilers have similar flags. (See here: http://stackoverflow.com/questions/2798188/pure-const-function-attributes-in-different-compilers )I can think of many cases where it would have some noteable performance difference.Also, in principle, the code is cleaner if you leave the optimisation to the compiler and don't do it by hand. And I'm quite sure that I would not find all code by hand where such optimisation would be good.
Albert
@Albert: to be honest, I can’t think of many cases where such attributes would lead to noticeable better performance. Take `strlen` – better solutions abound. Either use something else entirely (`std::string`) or cache the value. That’s just as explicit and efficient, and much more portable. And yes, you’re right: leave optimizations to the compiler, don’t do them by hand. Using such attributes *is* essentially doing it by hand. The compiler can figure out side-effect freeness all by itself, where it matters. In fact, repeated calls to `strlen` can be elided by compilers in quite a few cases.
Konrad Rudolph
The compiler *cannot* figure out side-effect freeness in all cases. Esp if the code is in some other cpp file and you don't use linker optimisation (whereby I am not sure if linker optimisation even does this).
Albert
@Albert If the compiler can't figure it out, neither can you - reliably.
anon
@Neil: Consider the case that your code is not only in a single cpp file.
Albert
@Albert I have, thanks. My point is that you can easily tag something as pure that isn't. The compiler is much less likely to make such a mistake. And if the compiler can make that mistake, the chances of you doing so are even greater. Avoiding these kinds of mistake are why we use compilers in the first place.
anon
There are cases that I still want to mark something as pure/const that really is not pure/const in a strict sense. See my initial description. That is what this whole stackoverflow question is about. I want to do that and the question is if that could break something (and what exactly).
Albert
A: 

I think nobody knows this (with the exception of gcc programmers), simply because you rely on undefined and undocumented behaviour, which can change version from version. But how about something like this:


#ifdef NDEBUG \
    #define safe_pure __attribute__((pure)) \
#else \
    #define safe_pure \
#endif

I know it's not exactly you want, but now you can use pure attribute without breaking the rules.
If you do want to know the answer, you may ask in gcc forum (mailing list, whatever), they sould be able to give you the exact answer.
Meaning of the code: When NDEBUG (symbol used in assert macros) is defined, we don't debug, have no side effects, can use pure attribute. When it is defined, we have side effects, so won't use pure attribute.

Dadam
pure/const is very clearly defined. The behavior of the optimisation can differ but that doesn't matter at all (because of the definition of pure/const). I am also not really asking here about how to `#ifdef` my code for different compilers. I am asking about if there can be any problems with pure/const.
Albert
Yes, pure/const are clearly defined, but if their error behaviour (by error I mean using pure/const on non-pure/non-const function) was also defined, you wouldn't have to ask here. Also I'm not giving you #ifdef for different compiler, I'm giving you #ifdef for different configuration (debug and release).
Dadam
Of course it is also defined: The compiler does handle it just in the same way (because it will not know at that point if your const/pure attribute is really correct; it will assume that because you put the attribute).
Albert
You could also just go with `-O0` or so to disable such optimisations. We test our code in any way with many different compilers so it is very unlikely that we will not see an error anymore because of it.
Albert
+1  A: 

I would expect the output:

I would expect the input:

int bar(int x) {
   return foo(x) * 100;
}

Your code actually looks strange for me. As a maintainer I would think that either foo actually has side effects or more likely rewrite it immediately to the above function.

How does it work out in practice? If I mark such functions as pure/const, could it break anything (considering that the code is all correct)?

If the code is all correct then no. But the chances that your code is correct are small. If your code is incorrect then this feature can mask out bugs:

int foo(int x) {
    globalmutex.lock();
    // complicated calculation code
        return -1;
    // more complicated calculation
    globalmutex.unlock();
    return x;
}

Now given the bar from above:

int main() {
    cout << bar(-1);
}

This terminates with __attribute__((const)) but deadlocks otherwise.

It also highly depends on the implementation. For example:

void f() {
    for(;;) 
    {
        globalmutex.unlock();
        cout << foo(42) << '\n';
        globalmutex.lock();
    }
}

Where the compiler should move the call foo(42)? Is it allowed to optimize this code? Not in general! So unless the loop is really trivial you have no benefits of your feature. But if your loop is trivial you can easily optimize it yourself.

EDIT: as Albert requested a less obvious situation, here it comes: F or example if you implement operator << for an ostream, you use the ostream::sentry which locks the stream buffer. Suppose you call pure/const f after you released or before you locked it. Someone uses this operator cout << YourType() and f also uses cout << "debug info". According to you the compiler is free to put the invocation of f into the critical section. Deadlock occurs.

ybungalobill
About your example: The compiler can move the code whereever it likes because you told him that it does not depend in any way on everything else in your code. Which is wrong of course because it depends on the globalmutex. -- Of course, using the keyword in a wrong/invalid way can break things but that is not the thing I am asking.
Albert
@Albert: Your questions sounds like this: "Can I break a code by marking a function pure/const except those situations where it's wrong to use pure/const?" What answer are you expecting?
ybungalobill
No, that's not what I was asking. I was asking: Can I break something by marking functions pure/const where it should be valid/correct to do so? I.e. are there any non-obvious cases or will it always work?
Albert
@Albert: I don't see the difference. Anyway updated my answer for a less obvious situation.
ybungalobill
Yea, that example is much better! However, very unlikely that I run into it. But maybe that is one first answer to my question: Don't mark functions which access probably shared mutexes with the calling code as pure/const. Do you know other cases except `ostream` s where this can happen? I don't use `cout` anyway for debugging information. But maybe the general advise: Don't use `cout`/`cerr` in such functions.
Albert
@Albert: maybe allocation functions and debug logs. I don't think that there exists something else you may think it doesn't affect global state but it actually does. Even the above two are unlikely to have circular dependencies.
ybungalobill
A: 

I would examine the generated asm to see what difference they make. (My guess would be that switching from C++ streams to something else would yield more of a real benefit, see: http://typethinker.blogspot.com/2010/05/are-c-iostreams-really-slow.html )

KSchmidt
@KSchmidt: you missed the question goal entirely, the stream is only here in debug build to help analyze what's going on...
Matthieu M.
@Matthieu M. How is suggesting to look at the generated asm missing the point? Also, the question indicates using attributes on debug functions themselves; I don't see any mention that the stream usage is only in debug builds. I was simply trying to highlight that stream usage would likely overshadow any benefit attribute usage would provide.
KSchmidt
@KSchmidt: I readily admit that (unfortunately) the IO Stream library is slowish, but the text is about the use of pure/const attributes in debug mode and the 3rd paragraph begin with "The most obvious case are debug output". I also agree that having a look at the generated asm could help, though if using a smart compiler it should not be necessary to get down so much. Common Subexpression Elimination can be witnessed at higher level in both gcc / llvm framework.
Matthieu M.