Peter Ruderman
Peter Ruderman

Reputation: 12485

How can I make the storage of C++ lambda objects more efficient?

I've been thinking about storing C++ lambda's lately. The standard advice you see on the Internet is to store the lambda in a std::function object. However, none of this advice ever considers the storage implications. It occurred to me that there must be some serious voodoo going on behind the scenes to make this work. Consider the following class that stores an integer value:

class Simple {
public:
    Simple( int value ) { puts( "Constructing simple!" ); this->value = value; }
    Simple( const Simple& rhs ) { puts( "Copying simple!" ); this->value = rhs.value; }
    Simple( Simple&& rhs ) { puts( "Moving simple!" ); this->value = rhs.value; }
    ~Simple() { puts( "Destroying simple!" ); }
    int Get() const { return this->value; }

private:
    int value;
};

Now, consider this simple program:

int main()
{
    Simple test( 5 );

    std::function<int ()> f =
        [test] ()
        {
            return test.Get();
        };

    printf( "%d\n", f() );
}

This is the output I would hope to see from this program:

Constructing simple!
Copying simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

First, we create the value test. We create a local copy on the stack for the temporary lambda object. We then move the temporary lambda object into memory allocated by std::function. We destroy the temporary lambda. We print our output. We destroy the std::function. And finally, we destroy the test object.

Needless to say, this is not what I see. When I compile this on Visual C++ 2010 (release or debug mode), I get this output:

Constructing simple!
Copying simple!
Copying simple!
Copying simple!
Copying simple!
Destroying simple!
Destroying simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

Holy crap that's inefficient! Not only did the compiler fail to use my move constructor, but it generated and destroyed two apparently superfluous copies of the lambda during the assignment.

So, here finally are the questions: (1) Is all this copying really necessary? (2) Is there some way to coerce the compiler into generating better code? Thanks for reading!

Upvotes: 21

Views: 8191

Answers (5)

BobK
BobK

Reputation: 31

Using C++14, you can avoid the copies altogether:

int main(){
    Simple test( 5 );
    std::function<int ()> f =[test = std::move(test)](){
        return test.Get();
    };
    printf( "%d\n", f() );
}

To produce the output:

Constructing simple!
Moving simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

Note the following line:

[test = std::move(test)] 

Here, the first appearance of "test" is in a different scope than the second.

Upvotes: 3

Nicol Bolas
Nicol Bolas

Reputation: 474066

The standard advice you see on the Internet is to store the lambda in a std::function object. However, none of this advice ever considers the storage implications.

That's because it doesn't matter. You do not have access to the typename of the lambda. So while you can store it in its native type with auto initially, it's not leaving that scope with that type. You can't return it as that type. You can only stick it in something else. And the only "something else" C++11 provides is std::function.

So you have a choice: temporarily hold on to it with auto, locked within that scope. Or stick it in a std::function for long-term storage.

Is all this copying really necessary?

Technically? No, it is not necessary for what std::function does.

Is there some way to coerce the compiler into generating better code?

No. This isn't your compiler's fault; that's just how this particular implementation of std::function works. It could do less copying; it shouldn't have to copy more than twice (and depending on how the compiler generates the lambda, probably only once). But it does.

Upvotes: 10

Arzar
Arzar

Reputation: 14317

I noticed the same performance problem a while ago with MSVC10 and filed a bug report at microsoft connect :
https://connect.microsoft.com/VisualStudio/feedback/details/649268/std-bind-and-std-function-generate-a-crazy-number-of-copy#details

The bug is closed as "fixed". With MSVC11 developer preview your code now indeed print :

Constructing simple!
Copying simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

Upvotes: 9

Peter Alexander
Peter Alexander

Reputation: 54290

Your first problem is simply that MSVC's implementation of std::function is inefficient. With g++ 4.5.1 I get:

Constructing simple!
Copying simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!

That's still creating an extra copy though. The problem is that your lambda is capturing test by value, which is why you have all the copies. Try:

int main()
{
    Simple test( 5 );

    std::function<int ()> f =
        [&test] ()               // <-- Note added &
        {
            return test.Get();
        };

    printf( "%d\n", f() );
}

Again with g++, I now get:

Constructing simple!
5
Destroying simple!

Note that if you capture by reference then you have to ensure that test remains alive for the duration of f's lifetime, otherwise you'll be using a reference to a destroyed object, which provokes undefined behaviour. If f needs to outlive test then you have to use the pass by value version.

Upvotes: 6

ronag
ronag

Reputation: 51263

The problem is that std::function doesn't use move semantics and copies the lambda around while initializating. It's a bad implementation by MS.

Here is a little trick you can use to get around the problem.

template<typename T>
class move_lambda
{
    T func_;

public:
    move_lambda(T&& func) : func_(std::move(func)){}            
    move_lambda(const move_lambda& other) : func_(std::move(other.func_)){} // move on copy 
    auto operator()() -> decltype(static_cast<T>(0)()){return func_();}
};

template <typename T>
move_lambda<T> make_move_lambda(T&& func)
{
    return move_lambda<T>(std::move(func));
}

usage:

int main()
{
    Simple test( 5 );

    std::function<int ()> f(make_move_lambda(
        [test] ()
        {
            return test.Get();
        }));

    printf( "%d\n", f() );
}

Upvotes: 0

Related Questions