<functional>
should avoid including <memory>
#2998
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.
Reported by: https://www.reddit.com/r/cpp/comments/wgb8xo/fun_times_with_msvc_functional/
Related to: #2996
<functional>
was including<memory>
in C++17 mode forunique_ptr
andalign()
used by Boyer-Moore and_Ebco_base
used by_Not_fn
.We can deal with
align()
and_Ebco_base
by moving them up from<memory>
to<xmemory>
. This means that more code will drag them in, but they're quite small so this should have minimal impact.For
unique_ptr
, we can replace it with a cut-down_Mini_ptr
. I've added a comment mentioning that this has stripped away some ofunique_ptr
's misuse-resistance (e.g. it doesn't attempt to defend againstDerived*
being converted toBase*
as a constructor parameter and then being array-deleted).I've uglified
_Get()
and_Release()
, and marked_Release()
as_NODISCARD
(which we've intentionally refrained from doing forunique_ptr
since 10% of usage discards without leaking; here we control all usage).For TUs (or Standard Library Modules) that include both
<functional>
and<memory>
, the addition of_Mini_ptr
is an added cost, but it should be quite small because the class is so cut-down. The savings for TUs that include<functional>
without<memory>
are substantial. Measuring#include <functional>
withcl /EHsc /nologo /W4 /MTd /std:c++latest /P /showIncludes meow.cpp
, I found that the preprocessed file shrinks:main
: 2,157,990 bytesmain
, this PR saves 275,171 bytes, which is 12.8% smaller. We avoid<memory>
and<atomic>
but still drag in<iosfwd>
through the<xstring>
path.<xhash>
should avoid including<xstring>
#2996: 1,945,700 bytes<xhash>
should avoid including<xstring>
#2996: 1,660,715 bytes<xhash>
should avoid including<xstring>
#2996 alone, adding this PR saves 284,985 bytes, which is 14.6% smaller. The absolute improvement increases because we're able to fully avoid<iosfwd>
.main
, both PRs together save 497,275 bytes, which is 23.0% smaller.