C++ lambdas more expensive than blocks?

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. :cry: 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.”

Yes, std::function is a fairly inefficient class. It's useful for
storing a lambda but not so much for passing it around. You can pass
around lambdas as a template argument (requires all callees to be
templates) or as a function pointer if there are no captures. If you
have captures a helper class such as llvm::function_ref[1] might come
in handy, which acts like a function pointer but also allows captures
to be passed around.

[1] https://github.com/llvm-mirror/llvm/blob/master/include/llvm/ADT/STLExtras.h#L80

Thanks. Is that class something I can just copy-and-paste into my code? Will it work with other compilers like GCC or MSVC?
(Sorry if these are naive questions; I’m not knowledgeable enough about templates to be able to understand that source code.)

—Jens

Copying function_ref will work, it's independent of the rest of LLVM
and works with all C++11 compilers. I also expect common C++ libraries
to have an equivalent class, but I haven't checked.