frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

C++: Zero-cost static initialization

https://cofault.com/zero-cost-static.html
47•oecumena•3d ago

Comments

pbsd•6h ago
>Even after the static variable has been initialised, the overhead of accessing it is still considerable: a function call to __cxa_guard_acquire(), plus atomic_load_explicit(&__b_guard, memory_order::acquire) in __cxa_guard_acquire().

No. The lock calls are only done during initialization, in case two threads run the initialization concurrently while the guard variable is 0. Once the variable is initialized, this will always be skipped by "je .L3".

ot•6h ago
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 Clang seem to be quite bad at is code layout: even in the example in the article, the slow path is largely inlined. It would be much better to have just a load, a compare, and a jump to the slow path in a cold section. In my experience, in some rare cases reimplementing the lazy initialization explicitly (especially when it's possible to use a sentinel value, thus doing a single load for both value and guard) did produce a noticeable win.

surajrmal•14m ago
FDO/PGO seem to really improve optimizations for hot/cold functions. I wonder if it does the kind of thing you're suggesting.
TuxSH•6h ago
TIL about encapsulation symbols.

Why not just use constinit (iff applicable), construct_at, or lessen the cost with -fno-threadsafe-statics?

forrestthewoods•6h ago
> 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 less.

spauldo•1h ago
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.

forrestthewoods•20m ago
Their tasks would be less hard if the UNIX guy would stop writing non-portable POSIX code =P
foota•5h ago
Funny enough I recently wrote my own hack using this linker feature in C, to implement an array of static counter definitions that can be declared anywhere and then written out (e.g., to prometheus) in one place.

Note that as I later found out, this doesn't work with Mac OS's linker, so you need to use some separate incantations for Mac OS.

haberman•5h ago
I wrote a portable abstraction for this that works across Linux, MacOS, and Windows: https://github.com/protocolbuffers/protobuf/blob/4917ec250d3...

I call them "linker arrays". They are great when you need to globally register a set of things and the order between them isn't significant.

rockwotj•5h ago
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

jandrewrogers•3h ago
std::launder should probably be used more than it is in low-level code if you care about correctness, even though it doesn’t always bite you in the ass. It is a logical no-op. std::launder is a hint to the compiler to forget everything it thinks it knows about the type instance, sort of like marking it “volatile” only for a specific moment in time.

The use of std::launder should be more common than it is, I’ve seen a few bugs in optimized builds when not used, but compilers have been somewhat forgiving about not using it in places you should because it hasn’t always existed. Rigorous code should be using it instead of relying on the leniency of the compiler.

In database engine code it definitely gets used in the storage layers.

naruhodo•1h ago
The rabbit hole I just went down is called C/C++ Statement Expressions [1] which are a GCC extension:

    #define FAST_STATIC(T) \
      *({ \
          \                                // statements separated by semicolons
          reinterpret_cast<T *>(ph.buf); \ // the value of the macro as a statement
       })
The reinterpret_cast<T*>(...) statement is a conventional C++ Expression Statement, but when enclosed in ({}), GCC considers the whole kit and kaboodle a Statement Expression that propagates a value.

There is no C equivalent, but in in C++, since C++11 you can achieve the same effect with lambdas:

    auto value = [](){ return 12345; }();
As noted in the linked SO discussion, this is analogous to a JS Immediately-Invoked Function Expression (IIFE).

[1] https://stackoverflow.com/questions/76890861/what-is-called-...

viralsink•11m ago
Lambdas are not fully equivalent since return statements in statement expressions will return at the function level, whereas return statements in lambdas will only return at the lambda level.

My Self-Hosting Setup

https://codecaptured.com/blog/my-ultimate-self-hosting-setup/
215•mirdaki•5h ago•80 comments

How to write Rust in the Linux kernel: part 3

https://lwn.net/SubscriberLink/1026694/3413f4b43c862629/
158•chmaynard•9h ago•0 comments

Valve confirms credit card companies pressured it to delist certain adult games

https://www.pcgamer.com/software/platforms/valve-confirms-credit-card-companies-pressured-it-to-delist-certain-adult-games-from-steam/
441•freedomben•16h ago•458 comments

Advertising Without Signal: The Rise of the Grifter Equilibrium

https://www.gojiberries.io/advertising-without-signal-whe-amazon-ads-confuse-more-than-they-clarify/
51•neehao•5h ago•19 comments

Asynchrony is not concurrency

https://kristoff.it/blog/asynchrony-is-not-concurrency/
225•kristoff_it•12h ago•149 comments

Bun adds pnpm-style isolated installation mode

https://github.com/oven-sh/bun/pull/20440
60•nateb2022•7h ago•5 comments

Meta says it won’t sign Europe AI agreement, calling it an overreach

https://www.cnbc.com/2025/07/18/meta-europe-ai-code.html
199•rntn•14h ago•225 comments

Debcraft – Easiest way to modify and build Debian packages

https://optimizedbyotto.com/post/debcraft-easy-debian-packaging/
45•pabs3•7h ago•7 comments

Silence Is a Commons by Ivan Illich (1983)

http://www.davidtinapple.com/illich/1983_silence_commons.html
141•entaloneralie•10h ago•27 comments

Mr Browser – Macintosh Repository file downloader that runs directly on 68k Macs

https://www.macintoshrepository.org/44146-mr-browser
60•zdw•7h ago•12 comments

The Bitter Lessons Behind Kimi Researcher's Taste

https://medium.com/@xinyijin715/maker-story-the-bitter-lessons-behind-kimi-researchers-ui-6654ec66662c
3•pr337h4m•3d ago•0 comments

C++: Zero-cost static initialization

https://cofault.com/zero-cost-static.html
47•oecumena•3d ago•13 comments

Zig's New Writer

https://www.openmymind.net/Zigs-New-Writer/
40•Bogdanp•1d ago•2 comments

Microsoft Office is using an artificially complex XML schema as a lock-in tool

https://blog.documentfoundation.org/blog/2025/07/18/artificially-complex-xml-schema-as-lock-in-tool/
63•firexcy•3h ago•18 comments

EPA says it will eliminate its scientific research arm

https://www.nytimes.com/2025/07/18/climate/epa-firings-scientific-research.html
178•anigbrowl•8h ago•142 comments

Broadcom to discontinue free Bitnami Helm charts

https://github.com/bitnami/charts/issues/35164
141•mmoogle•12h ago•81 comments

Show HN: OrioleDB Beta12 Features and Benchmarks

https://www.orioledb.com/blog/orioledb-beta12-benchmarks
29•akorotkov•3d ago•3 comments

Wii U SDBoot1 Exploit “paid the beak”

https://consolebytes.com/wii-u-sdboot1-exploit-paid-the-beak/
131•sjuut•11h ago•20 comments

Hyatt Hotels are using algorithmic Rest “smoking detectors”

https://twitter.com/_ZachGriff/status/1945959030851035223
95•RebeccaTheDev•3h ago•40 comments

Ccusage: A CLI tool for analyzing Claude Code usage from local JSONL files

https://github.com/ryoppippi/ccusage
50•kristianp•8h ago•27 comments

Multiplatform Matrix Multiplication Kernels

https://burn.dev/blog/sota-multiplatform-matmul/
69•homarp•11h ago•24 comments

The Halo Effect

https://kwokchain.com/2025/07/15/the-halo-effect/
18•iamwil•3d ago•8 comments

Cats as Horror Movie Villains

https://gwern.net/cat-horror
15•mparramon•3d ago•10 comments

Trying Guix: A Nixer's impressions

https://tazj.in/blog/trying-guix
178•todsacerdoti•3d ago•56 comments

The Great Unracking: Saying goodbye to the servers at our physical datacenter

https://stackoverflow.blog/2025/07/16/the-great-unracking-saying-goodbye-to-the-servers-at-our-physical-datacenter/
18•treve•2h ago•11 comments

AI capex is so big that it's affecting economic statistics

https://paulkedrosky.com/honey-ai-capex-ate-the-economy/
286•throw0101c•12h ago•281 comments

Show HN: Molab, a cloud-hosted Marimo notebook workspace

https://molab.marimo.io/notebooks
93•akshayka•13h ago•12 comments

A New Geometry for Einstein's Theory of Relativity

https://www.quantamagazine.org/a-new-geometry-for-einsteins-theory-of-relativity-20250716/
104•jandrewrogers•16h ago•5 comments

Show HN: Mochi Invaders – Like Space Invaders but for Practicing Japanese Kana

https://xenodium.com/mochi-invaders-now-on-the-app-store
10•xenodium•3d ago•0 comments

The year of peak might and magic

https://www.filfre.net/2025/07/the-year-of-peak-might-and-magic/
99•cybersoyuz•14h ago•52 comments