-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[ADT] Use "inline static" to initialize CallbacksHolder (NFC) #160003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADT] Use "inline static" to initialize CallbacksHolder (NFC) #160003
Conversation
In C++17, we can initialize a static member variable with "inline static" as part of the class definition. With this, we can eliminate the out-of-line static initializers that are a bit hard to decipher.
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesIn C++17, we can initialize a static member variable with Full diff: https://github.com/llvm/llvm-project/pull/160003.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/FunctionExtras.h b/llvm/include/llvm/ADT/FunctionExtras.h
index e45d507bfd4bf..1311452a17bb3 100644
--- a/llvm/include/llvm/ADT/FunctionExtras.h
+++ b/llvm/include/llvm/ADT/FunctionExtras.h
@@ -231,7 +231,6 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
// The pointers to call/move/destroy functions are determined for each
// callable type (and called-as type, which determines the overload chosen).
- // (definitions are out-of-line).
// By default, we need an object that contains all the different
// type erased behaviors needed. Create a static instance of the struct type
@@ -239,14 +238,15 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
// Wrap in a struct to avoid https://gcc.gnu.org/PR71954
template <typename CallableT, typename CalledAs, typename Enable = void>
struct CallbacksHolder {
- static NonTrivialCallbacks Callbacks;
+ inline static NonTrivialCallbacks Callbacks = {
+ &CallImpl<CalledAs>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
};
// See if we can create a trivial callback. We need the callable to be
// trivially moved and trivially destroyed so that we don't have to store
// type erased callbacks for those operations.
template <typename CallableT, typename CalledAs>
struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> {
- static TrivialCallback Callbacks;
+ inline static TrivialCallback Callbacks = {&CallImpl<CalledAs>};
};
// A simple tag type so the call-as type to be passed to the constructor.
@@ -344,19 +344,6 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
}
};
-template <typename R, typename... P>
-template <typename CallableT, typename CalledAsT, typename Enable>
-typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase<
- R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
- &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
-
-template <typename R, typename... P>
-template <typename CallableT, typename CalledAsT>
-typename UniqueFunctionBase<R, P...>::TrivialCallback
- UniqueFunctionBase<R, P...>::CallbacksHolder<
- CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
- &CallImpl<CalledAsT>};
-
} // namespace detail
template <typename R, typename... P>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this affect the number of relocations?
No, I checked on The text, data, and bss sizes stay the same although the actual executable magically shrinks by a few hundred bytes. |
…60003) In C++17, we can initialize a static member variable with "inline static" as part of the class definition. With this, we can eliminate the out-of-line static initializers that are a bit hard to decipher.
…60003) In C++17, we can initialize a static member variable with "inline static" as part of the class definition. With this, we can eliminate the out-of-line static initializers that are a bit hard to decipher.
In C++17, we can initialize a static member variable with
"inline static" as part of the class definition. With this, we can
eliminate the out-of-line static initializers that are a bit hard to
decipher.