Generic pure functions memoization I like to create tools for memoization since it can sometimes be useful. I recently created a generic tool for memoizing the results of pure functions.

Here is an example of how it works:

// Dummy function
auto func(int a, bool b)
    -&gt; std::string
{
    // Some heavy computations
}

int main()
{
    auto memof_f = memoized(func);
    auto a = memo_f(5, true);
    auto b = memo_f(8, false);
    // ...
}


In this example, memo_f is a wrapper that calls func and memoizes its results. When called, it checks whether the result for the given arguments is in cache and computes it only if it is not.

Here is the implementation of memoized:

template&lt;typename Ret, typename... Args&gt;
class MemoizedFunction
{
    public:
        MemoizedFunction(const std::function&lt;Ret(Args...)&gt;&amp; func):
            _func(func)
        {}

        auto operator()(Args... args)
            -&gt; Ret
        {
            auto tuple_args = std::make_tuple(args...);
            if (not _memory.count(tuple_args))
            {
                auto res = _func(args...);
                _memory[tuple_args] = res;
                return res;
            }
            return _memory[tuple_args];
        }

    private:
        // Stored function
        std::function&lt;Ret(Args...)&gt; _func;
        // Map containing the pairs args/return
        std::unordered_map&lt;std::tuple&lt;Args...&gt;, Ret&gt; _memory;
};

template&lt;typename Ret, typename... Args&gt;
auto memoized(Ret (*func)(Args...))
    -&gt; MemoizedFunction&lt;Ret, Args...&gt;
{
    return { std::function&lt;Ret(Args...)&gt;(func) };
}


Also, it's not of utmost importance, but I need these little functions in order to hash a tuple:

template&lt;typename First, typename... Rest&gt;
auto hash_all(First first, Rest... rest)
    -&gt; std::size_t
{
    auto seed = std::hash&lt;First&gt;()(first);
    seed ^= hash_all(rest...);
    return seed;
}

template&lt;typename First, typename Second&gt;
auto hash_all(First first, Second second)
    -&gt; std::size_t
{
    return std::hash&lt;First&gt;()(first) ^ std::hash&lt;Second&gt;()(second);
}

namespace std
{
    // Hash function for tuples
    template&lt;typename... Args&gt;
    struct hash&lt;std::tuple&lt;Args...&gt;&gt;
    {
        auto operator()(const std::tuple&lt;Args...&gt;&amp; args) const
            -&gt; std::size_t
        {
            return apply(hash_all&lt;Args...&gt;, args);
        }
    };
}


Note: the function apply is a function that uses the elements of a tuple as parameters for a given function, as proposed in N3802.

My question is simple: since this a low-level tool, which could be used in several contexts, is there any way to improve its performance at least a little bit? And while we are at it, do not hesitate to give tips for that construct to be more versatile too :)

Note: this class is only intended to work for pure (or "well-behaved" as of N3744) functions.
Thanks to an answer to a similar question on StackOverflow (thanks @200_success), I improved the implementation of operator(). Using _memory.count is pretty inefficient compared to _memory.find since the latter will stop once the first corresponding key is found:

auto operator()(Args... args)
    -&gt; Ret
{
    const auto t_args = std::make_tuple(args...);
    auto it = _memory.find(t_args);
    if (it == _memory.cend())
    {
        using val_t = typename std::unordered_map&lt;std::tuple&lt;Args...&gt;, Ret&gt;::value_type;
        it = _memory.insert(val_t(t_args, _func(args...))).first;
    }
    return it-&gt;second;
}


While the solution above is indeed better than what I had, I managed to improve it even more: the results of _func should be directly constructed into _memory thanks to the piecewise construction of std::pair:

template&lt;typename Ret, typename... Args&gt;
auto MemoizedFunction&lt;Ret, Args...&gt;::operator()(Args... args)
    -&gt; Ret
{
    const auto t_args = std::make_tuple(args...);
    auto it = _memory.find(t_args);
    if (it == _memory.cend())
    {
        it = _memory.emplace(std::piecewise_construct,
                             std::forward_as_tuple(t_args),
                             std::forward_as_tuple(_func(args...))
                        ).first;
    }
    return it-&gt;second;
}


Now, operator() is far better than it originally was. I don't know whether it is still possible to improve it or not though.