News for hackers. Stuff that matters.
Re:Best Performance of a C++ Singleton by halayli on Sunday March 08, 2026 @12:47AM (#47294829)
The performance observation is real but the two approaches are not equivalent, and the article doesn't mention what you're actually trading away, which is the part that matters.

The C++11 threadsafety guarantee on static initialization is explicitly scoped to block local statics. That's not an implementation detail, that's the guarantee.

The __cxa_guard_acquire/release machinery in the assembly is the standard fulfilling that contract. Move to a private static data member and you're outside that guarantee entirely. You've quietly handed that responsibility back to yourself.

Then there's the static initialization order fiasco, which is the whole reason the meyers singleton with a local static became canonical. Block local static initializes on first use, lazily, deterministically, thread safely. A static data member initializes at startup in an order that is undefined across translation units. If anything touches Instance() during its own static initialization from a different TU, you're in UB territory. The article doesn't mention this.

Real world singleton designs also need: deferred/configuration-driven initialization, optional instantiation, state recycling, controlled teardown. A block local static keeps those doors open. A static data member initializes unconditionally at startup, you've lost lazy-init, you've lost the option to not initialize it, and configuration based instantiation becomes awkward by design.

Honestly, if you're bottlenecking on singleton access, that's design smell worth addressing, not the guard variable.

Re:Best Performance of a C++ Singleton by menaerus on Sunday March 08, 2026 @03:30AM (#47295350)
> Honestly, if you're bottlenecking on singleton access, that's design smell worth addressing, not the guard variable.

There's a large group of engineers who are totally unaware of Amdahl's law and they are consequently obsessed with the performance implications of what are usually most non-important parts of the codebase.

I learned that being in the opposite group of people became (or maybe has been always) somewhat unpopular because it breaks many of the myths that we have been taught for years, and on top of which many people have built their careers. This article may or may not be an example of that. I am not reading too much into it but profiling and identifying the actual bottlenecks seems like a scarce skill nowadays.

Re:Best Performance of a C++ Singleton by PacificSpecific on Sunday March 08, 2026 @05:22AM (#47295826)
You leveled up past a point a surprising number of people get stuck on essentially.

I feel likethe mindset you are describing is kind of this intermediate senior level. Sadly a lot of programmers can get stuck there for their whole career. Even worse when they get promoted to staff/principal level and start spreading dogma.

I 100 percent agree. If you can't show me a real world performance difference you are just spinning your wheels and wasting time.

Re:Best Performance of a C++ Singleton by menaerus on Monday March 09, 2026 @06:02AM (#47306955)
Yes, I agree, and my experience is the same - there's just too many folks getting stuck in that mindset and never leaving it. Looking into the history I think software engineering domain has a lot of cargo-cult, which is somewhat surprising given that people who are naturally attracted to this domain are supposed to be critical thinkers. It turns out that this may not be true for most of the time. I know that I was also afoul of that but I learned my lesson.
Re:Best Performance of a C++ Singleton by pjmlp on Sunday March 08, 2026 @07:02AM (#47296344)
Worse, while shipping Electron crap is the other extreme, not everything needs to be written to fit into 64 KB or 16ms rendering frame.

Many times taking a few extra ms, or God forbid 1s, is more than acceptable when there are humans in the loop.

Re:Best Performance of a C++ Singleton by halayli on Sunday March 08, 2026 @08:02AM (#47296650)
agreed. Strong emphasis on "profiling and identifying the actual bottleneck". Every benchmark will show a nested stack of performance offenders, but a solid interpretation requires a much deeper understanding of systems in general. My biggest aha moment yrs ago was when I realized that removing the function I was trying to optimize will still result in a benchmark output that shows top offenders and without going into too many details that minor perspective shift ended up paying dividends as it helped me rebuild my perspective on what benchmarks tell us.
Re:Best Performance of a C++ Singleton by menaerus on Monday March 09, 2026 @05:52AM (#47306860)
Yeah ... and so it happens that this particular function in the profile is just a symptom, merely being an observation (single) data point of system behavior under given workload, and not the root cause for, let's say, load instruction burning 90% of the CPU cycles by waiting on some data from the memory, and consequently giving you a wrong clue about the actual code creating that memory bus contention.

I have to say that up until I grasped a pretty good understanding of CPU internals, memory subsystem, kernel, and generally the hardware, reading into the perf profiles was just a fun exercise giving me almost no meaningful results.

Re:Best Performance of a C++ Singleton by halayli on Tuesday March 10, 2026 @12:52AM (#47319199)
Totally. I always found joy solving critical performance problems because it naturally pave a path forward to peel the layers and untangle the system interactions which feeds my curiosity, and is highly rewarding.
Re:Best Performance of a C++ Singleton by amluto on Sunday March 08, 2026 @11:12AM (#47297973)
On the flip side, it’s easy to get a bit stuck down the road by the mere fact that you have a singleton. Maybe you have amazing performance and very carefully managed safety, but you still have a single object that is inherently shared by all users in the same process, and it’s very very easy to end up regretting the semantic results. Been there, done that.
Re:Best Performance of a C++ Singleton by alex_dev42 on Sunday March 08, 2026 @01:12AM (#47294985)
Excellent points about the initialization order fiasco. I've been bitten by this in embedded systems where startup timing is critical.

One thing I'd add: the guard overhead can actually matter in high-frequency scenarios. I once profiled a logging singleton that was called millions of times per second in a real-time system - the atomic check was showing up as ~3% CPU. But your point stands: if you're hitting that bottleneck, you probably want to reconsider the architecture entirely.

The lazy initialization guarantee is usually worth more than the performance gain, especially since most singletons aren't accessed in tight loops. The static member approach feels like premature optimization unless you've actually measured the guard overhead in your specific use case.

Re:Best Performance of a C++ Singleton by halayli on Sunday March 08, 2026 @01:38AM (#47295122)
Yes definitely not dismissing the lock overhead, but I wanted to bring attention to the implicit false equivalence made in the post. That said, I am surprised the lock check was showing up and not the logging/formatting functions.
Re:Best Performance of a C++ Singleton by csegaults on Sunday March 08, 2026 @01:09AM (#47294970)
Err how does the static approach suffer from thread safety issues when the initialization happens before main even runs?

I might be responding to a llm so...

Re:Best Performance of a C++ Singleton by halayli on Sunday March 08, 2026 @01:28AM (#47295076)
a real human. threads can exist before main() starts. for example, you can include another tu which happens to launch a thread and call instance(). Singletons used to be a headache before C++11 and it was common(maybe still is) to see macros in projects that expand to a singleton class definition to avoid common pitfalls.
Re:Best Performance of a C++ Singleton by MaulingMonkey on Sunday March 08, 2026 @04:46AM (#47295689)
In fact, Windows 10+ now uses a thread pool during process init well before main is reached.

https://web.archive.org/web/20200920132133/https://blogs.bla...

Re:Best Performance of a C++ Singleton by platinumrad on Sunday March 08, 2026 @01:14AM (#47294993)
It's a bit contrived, but a global with a nontrivial constructor can spawn a thread that uses another global, and without synchronization the thread can see an uninitialized or partially initialized value.
Re:Best Performance of a C++ Singleton by jibal on Sunday March 08, 2026 @03:47AM (#47295425)
@dang There should be an HN guideline against such accusations.
Re:Best Performance of a C++ Singleton by Rexxar on Sunday March 08, 2026 @08:40AM (#47296853)
The fact that he calls the generated code good/bad without discussing the semantic differences tells that the original author doesn't really know what he is talking about. That seems problematic to me as he is selling c++ online course.
Re:Best Performance of a C++ Singleton by cv5005 on Sunday March 08, 2026 @09:21AM (#47297119)
>Then there's the static initialization order fiasco

One of the reasons I hate constructors and destructors.

Explicit init()/deinit() functions are much better.