RESOLVED FIXED 188146
Use static global variables instead of static NeverDestroyed inside function if possible
https://bugs.webkit.org/show_bug.cgi?id=188146
Summary Use static global variables instead of static NeverDestroyed inside function ...
Yusuke Suzuki
Reported 2018-07-29 06:56:32 PDT
Use constexpr constructors more instead of NeverDestroyed
Attachments
Patch (39.16 KB, patch)
2018-07-29 07:05 PDT, Yusuke Suzuki
darin: review+
Yusuke Suzuki
Comment 1 2018-07-29 07:05:18 PDT
Darin Adler
Comment 2 2018-07-29 10:43:09 PDT
Comment on attachment 346028 [details] Patch Change seems fine. Bug title is a bit inaccurate. Having a constexpr constructor does not affect whether NeverDestroyed is needed. It does affect whether we can have a global outside a function. If the constructor is not constexpr it needs to be inside a function so it gets run the first time the function is installed rather than at load time. What affects the need for NeverDestroyed is whether the class has a trivial destructor. If it does then NeverDestroyed is not needed. In all these cases in this patch we changed both things but they are independent. You can have a static without NeverDestroyed if the destructor is trivial. You can have a global outside a function that uses NeverDestroyed as long as the entire constructor is all constexpr. (Might need to fix NeverDestroyed implementation to be sure it’s constructors are constexpr.)
Darin Adler
Comment 3 2018-07-29 10:50:26 PDT
“Called” not “installed”
Yusuke Suzuki
Comment 4 2018-07-29 11:12:06 PDT
(In reply to Darin Adler from comment #2) > Comment on attachment 346028 [details] > Patch > > Change seems fine. > > Bug title is a bit inaccurate. > > Having a constexpr constructor does not affect whether NeverDestroyed is > needed. It does affect whether we can have a global outside a function. If > the constructor is not constexpr it needs to be inside a function so it gets > run the first time the function is installed rather than at load time. > > What affects the need for NeverDestroyed is whether the class has a trivial > destructor. If it does then NeverDestroyed is not needed. > > In all these cases in this patch we changed both things but they are > independent. Right, changed. > > You can have a static without NeverDestroyed if the destructor is trivial. > You can have a global outside a function that uses NeverDestroyed as long as > the entire constructor is all constexpr. (Might need to fix NeverDestroyed > implementation to be sure it’s constructors are constexpr.) Currently, we cannot have constexpr NeverDestroyed, since it allocates an instance on aligned storage with placement `new`. This type of code cannot be achieved inside constexpr constructor unfortunately :(
Yusuke Suzuki
Comment 5 2018-07-29 11:13:33 PDT
Radar WebKit Bug Importer
Comment 6 2018-07-29 11:14:17 PDT
Note You need to log in before you can comment on or make changes to this bug.