Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda function performance impact of capture

Tags:

c++

lambda

I just wrote a pretty big capture:

[this, &newIndex, &indexedDirs, &filters, &flags, &indexRecursion](){...

I use this lambda (indexRecursion) for a recursion with thoudands of elements and asked myself, if it would be more efficient to use the "global" capture [&]. Since I have no clue of the implementation of the capture I need some explanation. Please with background too.

like image 751
ManuelSchneid3r Avatar asked Jan 07 '23 11:01

ManuelSchneid3r


1 Answers

Usually you can think of a lambda as equivalent to this:

class ANON {
  int data;
  public:
    void operator ()(void) const {
      cout << data << endl;
    }
} lambda;
// auto lambda = [data]() {cout << data << endl;}

This should give you an idea of how capture is implemented. Capture all (be it by copy = or reference &) will probably be no more than syntactic sugar for specifying all used/available variables for capture in the current scope.

But since ...

[..] An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing: [..] the size and/or alignment of the closure type [..]

[N4431 §5.1.2/3]

... it would be legal for an implementation to use some sort of "black magic" for capture all by reference lambdas and just use a pointer to the captured stack frame, rewriting accesses to the variables as accesses to some offset of that pointer:

class ANON {
  void * stack_frame;
  public:
    void operator ()(void) const {
      cout << *static_cast<int *>(stack_frame + 8) << endl;
    }
} lambda;

So using & might (some day) be more efficient, but as already said this is implementation defined and this nothing to be relied upon.

like image 114
Daniel Jour Avatar answered Jan 10 '23 01:01

Daniel Jour