Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda with dynamic storage duration

Tags:

c++

c++11

lambda

According to cppreference.com, C++11 lambda literal syntax is only legal to use in direct initialization. There doesn't seem to be a way to use the lambda syntax directly with the new operator.

I need to store a lambda function in the heap so that I can call it at some later point from a different thread. It's easy enough to make a copy of the lambda, but is there a simple way to allocate the lambda directly in the heap (dynamic storage duration) without first allocating it on the stack (automatic storage duration) and making a copy?

Here's a simple example:

#include <cstdio>
#include <cassert>

struct MyObj {
    int value;
    int copies;
    int moves;

    MyObj(int v): value(v), copies(0), moves(0) {
        printf("Created object with value %d.\n", value);
    }

    MyObj(const MyObj &other): value(other.value),
    copies(other.copies+1), moves(other.moves) { }

    MyObj(const MyObj &&other): value(other.value),
    copies(other.copies), moves(other.moves+1) { }
};

int main (int argc, char **argv) {
    MyObj o { 5 };
    // Create lambda on stack (automatic storage duration)
    auto f = [o] {
        printf("Object value is %d\n", o.value);
        printf("%d copies, %d moves...\n", o.copies, o.moves);
    };
    // Copy lambda to heap (dynamic storage duration)
    decltype(f) *g = new decltype(f)(f);
    // Call the copy
    (*g)();
    return 0;
}

The above program makes 2 copies of o (one in the capture, and another when the lambda is copied into the heap). Ideally, there would only be one copy or move, which would happen when the heap-allocated lambda captures a copy of o.

like image 653
DaoWen Avatar asked Jun 20 '16 14:06

DaoWen


People also ask

How long can a Lambda function run?

Q: How long can an AWS Lambda function execute? AWS Lambda functions can be configured to run up to 15 minutes per execution. You can set the timeout to any value between 1 second and 15 minutes.

How long does AWS Lambda stay warm?

AWS needs a ready supply of containers to spin up when functions are invoked. That means that functions are kept warm for a limited amount of time (usually 30 – 45 minutes) after executing, before being spun down so that container is ready for any new function to be invoked.

Does Lambda have persistent storage?

Lambda Cloud Storage is now in open beta: a high speed filesystem for our GPU instances. After a period of closed beta, persistent storage for Lambda GPU Cloud is now available for all A6000 and V100 instances.

How do I reduce Lambda Execution time?

If we need to reduce the Lambda execution time, we can try increasing memory (and by extension, CPU) to process it faster. However, when we try to increase the memory for a function past a certain limit, it won't improve the execution time as AWS currently offers a maximum of 2 cores CPU.


1 Answers

In C++11, a lambda expression will always result in some form of automatic object, whether a stack variable or an unnamed temporary. There's nothing you can do to change that.

In C++17, guaranteed elision gives us the ability to do this:

new auto(<lambda>)

This uses the memory allocated by new to store the result of that expression. There would be no temporary lambda object created here, nor would any copy/move constructor for the lambda be invoked. And most importantly, the language would not require that the lambda type have copy/move constructors that could be invoked.

You need guaranteed elision to ensure this however. Without that guarantee, then you're banking on the compiler to optimize it. The standard permits such cases to elide the copy. And yes, any compiler worth using probably would elide such copies.

With guaranteed elision, you could capture immobile types and this would still work without copying anything. Pre-C++17, your lambda would still need to have a copy or move constructor, even though the call to it is elided.

like image 142
Nicol Bolas Avatar answered Oct 31 '22 22:10

Nicol Bolas