C++ lambdas more expensive than blocks?
C++ lambdas more expensive than blocks?
- Subject: C++ lambdas more expensive than blocks?
- From: Jens Alfke <email@hidden>
- Date: Sun, 23 Oct 2016 13:53:45 -0700
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
* 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.”
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden