frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Best Performance of a C++ Singleton

https://andreasfertig.com/blog/2026/03/best-performance-of-a-cpp-singleton/
17•jandeboevrie•1d ago

Comments

silverstream•1h ago
Honestly the guard overhead is a non-issue in practice — it's one atomic check after first init. The real problem with the static data member approach is initialization order across translation units. If singleton A touches singleton B during startup you get fun segfaults that only show up in release builds with a different link order.

I ended up using std::call_once for those cases. More boilerplate but at least you're not debugging init order at 2am.

csegaults•12m ago
Came here to say the same thing. Static is OK as long as the object has no dependencies but as soon as it does you're asking for trouble. Second the call_once approach. Another approach is an explicit initialization order system that ensures dependencies are set up in the right order, but that's more complex and only works for binaries you control.
m-schuetz•1h ago
I liked using singletons back in the day, but now I simply make a struct with static members which serves the same purpose with less verbose code. Initialization order doesn't matter if you add one explicit (and also static) init function, or a lazy initialization check.
signa11•38m ago
i am not sure why this entire article is warranted :o) just use `std::call_once` and you are all set.
swaminarayan•31m ago
Nice breakdown. I’m curious how often the guard check for a function-local static actually shows up in real profiles. In most codebases Instance() isn’t called in tight loops, so the safety of lazy initialization might matter more than a few extra instructions. Has anyone run into this being a real bottleneck in practice?
platinumrad•13m ago
I haven't written C++ in a long time, but isn't the issue here that the initialization order of globals in different translation units is unspecified? Lazy initialization avoids that problem at very modest cost.
halayli•10m ago
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.

Cloud VM benchmarks 2026

https://devblog.ecuadors.net/cloud-vm-benchmarks-2026-performance-price-1i1m.html
143•dkechag•5h ago•67 comments

CasNum

https://github.com/0x0mer/CasNum
239•aebtebeten•9h ago•33 comments

"Warn about PyPy being unmaintained"

https://github.com/astral-sh/uv/pull/17643
63•networked•4h ago•13 comments

MonoGame: A .NET framework for making cross-platform games

https://github.com/MonoGame/MonoGame
32•azhenley•3h ago•10 comments

A decade of Docker containers

https://cacm.acm.org/research/a-decade-of-docker-containers/
269•zacwest•13h ago•190 comments

I don't know if my job will still exist in ten years

https://www.seangoedecke.com/will-my-job-still-exist/
64•nomdep•5h ago•33 comments

Dumping Lego NXT firmware off of an existing brick (2025)

https://arcanenibble.github.io/dumping-lego-nxt-firmware-off-of-an-existing-brick.html
175•theblazehen•1d ago•11 comments

Emacs internals: Deconstructing Lisp_Object in C (Part 2)

https://thecloudlet.github.io/blog/project/emacs-02/
42•thecloudlet•2d ago•0 comments

Show HN: A weird thing that detects your pulse from the browser video

https://pulsefeedback.io/
49•kilroy123•3d ago•25 comments

Yoghurt delivery women combatting loneliness in Japan

https://www.bbc.com/travel/article/20260302-the-yoghurt-delivery-women-combatting-loneliness-in-j...
237•ranit•16h ago•137 comments

Autoresearch: Agents researching on single-GPU nanochat training automatically

https://github.com/karpathy/autoresearch
82•simonpure•9h ago•22 comments

Best Performance of a C++ Singleton

https://andreasfertig.com/blog/2026/03/best-performance-of-a-cpp-singleton/
17•jandeboevrie•1d ago•7 comments

In 1985 Maxell built a bunch of life-size robots for its bad floppy ad

https://buttondown.com/suchbadtechads/archive/maxell-life-size-robots/
83•rfarley04•3d ago•10 comments

The surprising whimsy of the Time Zone Database

https://muddy.jprs.me/links/2026-03-06-the-surprising-whimsy-of-the-time-zone-database/
78•jprs•11h ago•20 comments

FLASH radiotherapy's bold approach to cancer treatment

https://spectrum.ieee.org/flash-radiotherapy
195•marc__1•14h ago•61 comments

macOS code injection for fun and no profit (2024)

https://mariozechner.at/posts/2024-07-20-macos-code-injection-fun/
85•jstrieb•3d ago•15 comments

The stagnancy of publishing and the disappearance of the midlist

https://www.honest-broker.com/p/the-day-ny-publishing-lost-its-soul
73•wallflower•8h ago•50 comments

Lisp-style C++ template meta programming

https://github.com/mistivia/lmp
34•mistivia•7h ago•2 comments

How important was the Battle of Hastings?

https://www.historytoday.com/archive/head-head/how-important-was-battle-hastings
19•benbreen•3d ago•20 comments

Ten Years of Deploying to Production

https://brandonvin.github.io/2026/03/04/ten-years-of-deploying-to-production.html
3•mooreds•2d ago•0 comments

How to run Qwen 3.5 locally

https://unsloth.ai/docs/models/qwen3.5
26•Curiositry•6h ago•4 comments

Compiling Prolog to Forth [pdf]

https://vfxforth.com/flag/jfar/vol4/no4/article4.pdf
103•PaulHoule•4d ago•9 comments

Overheads (2023)

https://blog.xoria.org/hidden-overheads/
16•surprisetalk•1d ago•3 comments

Re-creating the complex cuisine of prehistoric Europeans

https://arstechnica.com/science/2026/03/recreating-the-complex-cuisine-of-prehistoric-europeans/
71•apollinaire•1d ago•31 comments

LLM Writing Tropes.md

https://tropes.fyi/tropes-md
138•walterbell•8h ago•51 comments

Files are the interface humans and agents interact with

https://madalitso.me/notes/why-everyone-is-talking-about-filesystems/
197•malgamves•19h ago•115 comments

The influence of anxiety: Harold Bloom and literary inheritance

https://thepointmag.com/examined-life/the-influence-of-anxiety/
22•apollinaire•3d ago•2 comments

Bourdieu's theory of taste: a grumbling abrégé (2023)

https://dynomight.net/bourdieu/
46•sebg•2d ago•16 comments

SigNoz (YC W21) is hiring for engineering, growth and product roles

https://signoz.io/careers
1•pranay01•12h ago

Ki Editor - an editor that operates on the AST

https://ki-editor.org/
392•ravenical•19h ago•137 comments