I’ve started using C++11 lambdas in my code. A few of them are in 
performance-sensitive areas, so I was looking at (optimized) assembly code of 
some of my methods to see what goes on under the hood.

I don’t entirely understand the code, but the code on the calling side (i.e. 
the part that creates the lambda and passes it as a function parameter) it 
looks larger and slower than similar code using Clang blocks. It looks as 
though there’s overhead in converting the lambda itself into a std::function 
object … especially because I see a call to a std::function method that makes a 
heap allocation. :'( The equivalent block-based code just fills out a small 
struct on the stack and passes its address.

Is there a more efficient way to pass a lambda to a function in C++? As far as 
I can tell, a lambda is of an anonymous type that can’t be named*, so the only 
way to pass it as a parameter is to wrap it in a std::function, which is 
comparatively expensive.

(My code has to be cross-platform, but I’m thinking of writing some macros that 
can expand into either lambda or block expressions, depending on the compiler. 
That way it’ll at least be more efficient when built with Clang.)

—Jens

PS: This is with Xcode 8, clang-800.0.38

* Ironically, my copy of the Tao Te Ching just arrived from Amazon while I was 
writing this email, and it starts right off with the famous statement “The way 
you can go isn’t the true way. The name you can say isn’t the true name.” 
_______________________________________________
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users

Reply via email to