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

后端 未结 5 1015
猫巷女王i
猫巷女王i 2021-02-05 07:16

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 adv

相关标签:
5条回答
  • 2021-02-05 07:25

    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.

    0 讨论(0)
  • 2021-02-05 07:28

    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() );
    }
    
    0 讨论(0)
  • 2021-02-05 07:35

    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!
    
    0 讨论(0)
  • 2021-02-05 07:41

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题