views:

611

answers:

4

I have one costly function that gets called many times and there is a very limited set of possible values for the parameter.
Function return code depends only on arguments so the obvious way to speed things up is to keep a static cache within the function for possible arguments and corresponding return codes, so for every combination of the parameters, the costly operation will be performed only once.
I always use this approach in such situations and it works fine but it just occurred to me that GCC function attributes const or pure probably can help me with this.

Does anybody have experience with this? How GCC uses pure and const attributes - only at compile time or at runtime as well?
Can I rely on GCC to be smart enough to call a function, declared as

int foo(int) __attribute__ ((pure))

just once for the same parameter value, or there is no guarantee whatsoever and I better stick to caching approach?

EDIT: My question is not about caching/memoization/lookup tables, but GCC function atributes.

+13  A: 

I think you are confusing the GCC pure attribute with memoization.

The GCC pure attribute allows the compiler to reduce the number of times the function is called in certain circumstances (such as loop unrolling). However it makes no guarantees that it will do so, only if it think it's appropriate.

What you appear to be looking for is memoization of your function. Memoization is an optimization where calculations for the same input should not be repeated. Instead the previous result should be returned. The GCC pure attribute does not make a function work in this way. You would have to hand implement this.

JaredPar
Ok, I didn't know the term - memorization, I used to call it caching and that is what I've implemented. I just hoped there is other way.
qrdl
@qrdl, memoization is a very specific form of caching. I avoid using jargon words where possible because I don't feel it always adds value to a conversation. In this case knowing the word does add value because are lots of memoization libraries and articles out there. It's a fun topic.
JaredPar
Thank you, next time I'd check libraries rather than writing memorization code from the scratch.
qrdl
Note that it's called memoization, without the letter r.
Stephan202
+2  A: 

I have one costly function that gets called many times and there is very limited set of possible values for the parameter.

Why not use a static constant map then (the arguments' can be hashed to generate a key, the return code the value)?

dirkgently
Return values depend on database which doesn't change during program run but may change between runs so I need to call the function at least once for every possible parameter value.
qrdl
Hm.You can still have a map as long as the data doesn't change within a single run. Lookup tables are always sweet.
dirkgently
That's what I do and what I called caching. JaredPar said that it is called memorization.
qrdl
@diskgently: i agree, he could just read the relevant parts of the DB at program startup and create a lookup table. Then use that during runtime.
Evan Teran
+1  A: 

This sounds like it might be solved with a template function. If all if the known parameters and return values are known at compile-time, you could perhaps generate a template instance of the function for each possible parameter. Essentially you'd be calling a different instance of the function for each possible parameter. Not sure it would be any easier than the static cache you've already implemented, but might be worth exploring.

Check out template metaprogramming. The concepts are similar to 'memoization', suggested by JaredPar, even using the same introductory example of a factorial function. It might be appropriate to say that these kinds of templates are compile-time implementations of memoization.

veefu
Parameters and return values are not known at compile time. But can you please elaborate on template function? I never heard of this approach.
qrdl
I've added a reference above. I don't have enough practical experience to write up an example suited to your problem. It may not be useful for your current problem, but it is certainly interesting to explore what can be done with the C++ template engine.
veefu
I just realized you may be working with strict C language only. My apologies for the distraction if this is the case.
veefu
@veefu: the question is about C; template metaprogramming is C++. Also, in general, templates are for dealing with different types, rather than different values of the same type.
Jonathan Leffler
A: 

I dont like to reopen old threads, but there was a particularly offensive comment here:

"templates are for dealing with different types, rather than different values of the same type"

Now, take a simple template factorial implementation:

template<int n> struct Factorial {
    static const int value = n * Factorial<n-1>::value;
};

template<> struct Factorial<0> {
    static const int value = 1;
};

The template parameter here is an integer, not a typename.

foobar