[C++] Performance improvement in finally #3144
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before, to use
finally
, the compiler had to:std::function
that could allocate if it wants to;finaly()
;std::function
would call the lambda;Neither MSVC nor GCC could inline any of the functions of
std::function
, thefinally
function, nor the lambda, even with LTO.With this patch both MSVC and GCC can see through the object and inline the lambda directly into the function code, making it essentially free.
MSVC would occasionally still set up the
FinalAction
object even if it doesn't use it, but would inline the lambda anyway.Should we migrate to a non-movable
FinalAction
wrapper that wouldn't need thebool
field so MSVC would get the idea to not set up the object?Anyway, this should still be faster and generate smaller code because the simpler logic.
@mike-lischke