Skip to content

Commit a144212

Browse files
dinordcopybara-github
authored andcommitted
internal/any_invocable: Use if constexpr instead of SFINAE in InitializeStorage
The `if constexpr` version generates a significantly shorter mangled name, which can matter in binaries that have many different callables (e.g. lambdas). See https://godbolt.org/z/ha87zKcTd. Besides, `if constexpr` is more maintainable than SFINAE. PiperOrigin-RevId: 724473717 Change-Id: I7be9d1f56a26e3a436aba402d0b47942ed08b7b4
1 parent 40cd2cf commit a144212

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

absl/functional/internal/any_invocable.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -543,28 +543,23 @@ class CoreImpl {
543543
}
544544

545545
// Use local (inline) storage for applicable target object types.
546-
template <class QualTRef, class... Args,
547-
typename = absl::enable_if_t<
548-
IsStoredLocally<RemoveCVRef<QualTRef>>()>>
549-
void InitializeStorage(Args&&... args) {
550-
using RawT = RemoveCVRef<QualTRef>;
551-
::new (static_cast<void*>(&state_.storage))
552-
RawT(std::forward<Args>(args)...);
553-
554-
invoker_ = LocalInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
555-
// We can simplify our manager if we know the type is trivially copyable.
556-
InitializeLocalManager<RawT>();
557-
}
558-
559-
// Use remote storage for target objects that cannot be stored locally.
560-
template <class QualTRef, class... Args,
561-
absl::enable_if_t<!IsStoredLocally<RemoveCVRef<QualTRef>>(),
562-
int> = 0>
546+
template <class QualTRef, class... Args>
563547
void InitializeStorage(Args&&... args) {
564-
InitializeRemoteManager<RemoveCVRef<QualTRef>>(std::forward<Args>(args)...);
565-
// This is set after everything else in case an exception is thrown in an
566-
// earlier step of the initialization.
567-
invoker_ = RemoteInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
548+
if constexpr (IsStoredLocally<RemoveCVRef<QualTRef>>()) {
549+
using RawT = RemoveCVRef<QualTRef>;
550+
::new (static_cast<void*>(&state_.storage))
551+
RawT(std::forward<Args>(args)...);
552+
553+
invoker_ = LocalInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
554+
// We can simplify our manager if we know the type is trivially copyable.
555+
InitializeLocalManager<RawT>();
556+
} else {
557+
InitializeRemoteManager<RemoveCVRef<QualTRef>>(
558+
std::forward<Args>(args)...);
559+
// This is set after everything else in case an exception is thrown in an
560+
// earlier step of the initialization.
561+
invoker_ = RemoteInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
562+
}
568563
}
569564

570565
template <class T,

0 commit comments

Comments
 (0)