More efficient and movable Defer - #11005
Open
jschmidt-icinga wants to merge 1 commit into
Open
Conversation
jschmidt-icinga
force-pushed
the
defer-without-allocation
branch
9 times, most recently
from
August 26, 2026 09:22
7226921 to
d5951f1
Compare
The refactor makes `Defer` without `std::function` and thus without all the overhead (like allocation and type erasure) that comes with it. I've also enabled the move constructor and assignment operators, whose lack is what got me started on this refactor in the first place. There are only two call-sites that needed to be adapted, one in ApiListener, because the `SetFunc()` method has been removed. And one in/around `ConfigPackageUtility` because that needs the type declared in the header which gets (slightly) more complicated now.
jschmidt-icinga
force-pushed
the
defer-without-allocation
branch
from
August 26, 2026 09:26
d5951f1 to
fc6a2fa
Compare
jschmidt-icinga
marked this pull request as ready for review
August 26, 2026 10:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This refactor makes
Deferwork withoutstd::functionby default and thus without all the overhead (like allocation and type erasure) that comes with it. It also makesDefer-objects movable.std::function<void()>is still supported by explicit instantiation along withvoid(*)()pointers when a fixed function type is needed with and without capture.Other Changes
There are only two call-sites that needed to be adapted,
ApiListener::ReplayLog()gets an explicitly instantiatedDefer<std::function<void()>>so the code can keep usingDefer::SetFunc(). And the use aroundConfigPackageUtilitygets an explicitDefer<void(*)()>because the type needs to be available in the header and thus can't be deduced.Reasoning
Aside from the obvious efficiency gains the reason I started on this refactor was because I wanted to move a
Deferobject into a lambda-capture and noticed that it had the move constructor disabled. I then looked into what possible reason there could be for the disabled move constructor and didn't find any, but then noticed that by templatingDeferand adding a deduction guide to astd::optional<lambda>,std::functioncould be avoided entirely. So this is a little more than the bare minimum I needed, but still it's a nice improvement, I think.Additional Options
The changes in
ConfigPackageUtilitycurrently minimize the diff, but theShared<Defer>::Ptrthere could easily be avoided as well. This would however mean slight changes in the potential lifetime of the defer which should be evaluated carefully. I can do that in this PR if anyone wants me to.