Live data from Hacker News

C++: Zero-cost static initialization

cofault.com

21–30 of 60 posts

Re: C++: Zero-cost static initialization

#21
FTA:

“Dynamic initialization of a block-scope variable with static storage duration or thread storage duration is performed the first time control passes through its declaration

[…]

this would initialise everything correctly: by the time foo() is called, its b has already been initialised.”

I guess that means this trick can change program behavior, especially if the function containing the static is never called in a program’s execution.

Re: C++: Zero-cost static initialization

#22
post #3

That's a nice trick, but contrary to function statics, it is susceptible to SIOF. This kind of optimization is useful only on extraordinarily hot paths, so I wouldn't generally recommend it. > On ARM, such atomic load incurs a memory barrier---a fairly expensive operation. Not quite, it is just a load-acquire, which is almost as cheap as a normal load. And on x86 there's no difference. One thing where both GCC and Cl…

FDO/PGO seem to really improve optimizations for hot/cold functions. I wonder if it does the kind of thing you're suggesting.

Not with any of the Clang versions I tried, but last time I checked it was a couple of years ago.

Re: C++: Zero-cost static initialization

#23
post #20
post #3

That's a nice trick, but contrary to function statics, it is susceptible to SIOF. This kind of optimization is useful only on extraordinarily hot paths, so I wouldn't generally recommend it. > On ARM, such atomic load incurs a memory barrier---a fairly expensive operation. Not quite, it is just a load-acquire, which is almost as cheap as a normal load. And on x86 there's no difference. One thing where both GCC and Cl…

> That's a nice trick, but contrary to function statics, it is susceptible to SIOF. For those (like me) who don’t recognize that abbreviation, “The static initialization order fiasco (ISO C++ FAQ) refers to the ambiguity in the order that objects with static storage duration in different translation units are initialized in” ( https://en.cppreference.com/w/cpp/language/siof.html )

Yes, thanks for the clarification, what I probably should have said is that the trick is basically syntactic sugar to declare a scoped global static variable, and as such it inherits all the problems of global static variables.

Re: C++: Zero-cost static initialization

#24
post #7

Looks similar to absl::NoDestructor https://github.com/abseil/abseil-cpp/blob/master/absl/base/n... Which is basically the only usage of std::launder I have seen

`NoDestructor` just ensures that the destructor is not called on the wrapped object, but you still need to manage the lifetime. If you look at the example, its recommended usage is with a function static. In other words, it's a utility to implement leaky Meyers' singletons.

std::launder is a bit weird here. Technically it should be used every time you use placement new but access the object by casting the pointer to its storage (which NoDestructor does). However, very little code actually uses it. For example, every implementation of std::optional should use it? But when you do, it actually prevents important compiler optimizations that make std::optional a zero-cost abstraction (or it did last time I looked into this).

Re: C++: Zero-cost static initialization

#25
post #11

> For this we need a certain old, but little-known feature of UNIX linkers STOP WRITING NON-PORTABLE CODE YOU BASTARDS. The correct answer is, as always, “stop using mutable global variables you bastard”. Signed: someone who is endlessly annoyed with people who incorrectly think Unix is the only platform their code will run on. Write standard C/C++ that doesn’t rely on obscure tricks. Your co-workers will hate you le…

I tell my coworkers, "Hey, we need this coded up as a Windows service!" and I get crickets. So I spin up a Debian VM and POSIX the hell out of it. If they dare to complain, I tell 'em to do their damn jobs and not leave all the hard stuff to the guy that only programs on UNIX.

To be fair to your coworkers, coding a windows service and setting up logging for it is surprisingly complicated. I'm the only person at my place of work that can do it, and even then only if I can use a compile-to-native language or .NET.

Re: C++: Zero-cost static initialization

#26
The way block scope statics are handled in C++ is a mistake. Block scope statics that don't depend on any non-static local variables should be initialized when the program starts up. E.g.:

  void fun(int arg)
  {
     static obj foo(arg);   // delayed until function called (dependency on arg)
     static obj bar();      // inited at program start (no dependency on arg)
  }
In other words, any static that can be inited at program startup should be, leaving only the troublesome cases that depend on run-time context.

Re: C++: Zero-cost static initialization

#27

The way block scope statics are handled in C++ is a mistake. Block scope statics that don't depend on any non-static local variables should be initialized when the program starts up. E.g.: void fun(int arg) { static obj foo(arg); // delayed until function called (dependency on arg) static obj bar(); // inited at program start (no dependency on arg) } In other words, any static that can be inited at program startup sh…

[deleted]

Re: C++: Zero-cost static initialization

#28

The way block scope statics are handled in C++ is a mistake. Block scope statics that don't depend on any non-static local variables should be initialized when the program starts up. E.g.: void fun(int arg) { static obj foo(arg); // delayed until function called (dependency on arg) static obj bar(); // inited at program start (no dependency on arg) } In other words, any static that can be inited at program startup sh…

Why not just use constexpr in the second case?

Re: C++: Zero-cost static initialization

#29
post #16

Earlier quoted context omitted.

Every time someone ships successful code that's hard to port to Windows the world becomes a better place.

> Every time someone ships successful code that's hard to port to Windows Until your boss tells you to port your so-far Linux-only code to Windows, and you run that struggle. Signed, someone who spent the past year or so porting Linux code to Windows and macOS because the business direction changed and the company saw what was the money-maker. P.S. Not the parent commenter, because I just realised they, too, had a pa…

Can you setup Windows to install WSL if it isn't there yet and then set it up, in a Windows installer?

Re: C++: Zero-cost static initialization

#30

The way block scope statics are handled in C++ is a mistake. Block scope statics that don't depend on any non-static local variables should be initialized when the program starts up. E.g.: void fun(int arg) { static obj foo(arg); // delayed until function called (dependency on arg) static obj bar(); // inited at program start (no dependency on arg) } In other words, any static that can be inited at program startup sh…

In theory I agree, but in practice this just increases compile time by a lot, and we need to be able to manually toggle the code that gets executed at compile time.
Post reply on HN