Boost logo

Boost :

Subject: Re: [boost] Interest check: memoization
From: James Porter (porterj_at_[hidden])
Date: 2009-01-25 20:47:26

vicente.botet wrote:
> The implementation is very elegant and the use of the C++0x facilities reduce the code to the minimum.

Thanks! I certainly don't relish writing this in C++03, though removing
the need to address rvalue references will simplify some of the code.

> We can memoize only unary functions with flyweight and have flyweight values with memoize(identity function). So which one perform better.

I'll obviously have to run some tests to see which one has better
performance, but for simple cases, I'd expect performance to be
approximately the same. Your example code with the memoizer does
highlight one current limitation of my code, however: without support
for function objects, recursive functions can only be memoized with a
global memoizer.

> If I understand memoizer can be used only with pure functions, isn't it? Is there a way to check for a pure function in C++0X? Is there a way to constraint the template parameter to only pure functions?

This is more-or-less true. You can allow functions with side effects,
but it requires considerable care to use them properly.

> Should the following compile?
> memoizer<int&& (int,int)> memo(my_func); // move result on calling
> int ret = memo(i,j);

This probably should compile, and move-construct the return value into
the map. The return value would then be a const-reference.

> Should the following compile?
> memoizer<int (int&,int)> memo(my_func); // allows to modify the first argument
> int ret = memo(i,j);

Perhaps not.

> Some minor changes in
> typedef const Ret & result_reference;
> will be needed to return references.
> memoizer<int& (int,int)> memo(my_func);
> int& ret = memo(i,j);

Definitely. I also have to examine the behavior with expensive-to-copy
return values. This is still very much a work-in-progress. I suppose
this is just an example of the law of library writing: generic,
efficient, and simple; choose 2. :)

- Jim

Boost list run by bdawes at, gregod at, cpdaniel at, john at