News for hackers. Stuff that matters.
Re:Writing your own C++ standard library part 2 by platinumrad on Monday June 02, 2025 @02:07AM (#44156170)
<algorithm> is a very good header, but most of the containers are substandard at best. `std::unordered_map` is required to be node-based, `std::map` can't be a B-tree, MSVC's implementation of `std::deque` is infamously a glorified `std::list`, and so on.

Pretty much everything else (e.g. iostreams) is horrible.

Re:Writing your own C++ standard library part 2 by TuxSH on Monday June 02, 2025 @05:59AM (#44157246)
Don't forget <tuple> not mandated to be trivial when it can be (and in fact never being trivial with GCC's stdlib), std::print performance issues (see P3107R5, P3235R3), etc.

Heck, even std::atomic was designed with only x64 in mind (it clearly shows), and is unusable outside it. One is incentivized to write their own "atomic" class until P3330R0 is approved for RMW-centric platforms ISAs like Aarch32 and Aarch64.

And of course, Rust already has "fetch_update"...

Re:Writing your own C++ standard library part 2 by spacechild1 on Monday June 02, 2025 @07:02AM (#44157542)
> Heck, even std::atomic was designed with only x64 in mind (it clearly shows),

It certainly wasn't.

> and is unusable outside it

Total hyperbole. It's perfectly usable on ARM and other platforms.

P3330R0 looks a nice addition, though.

Re:Writing your own C++ standard library part 2 by TuxSH on Monday June 02, 2025 @08:21AM (#44158072)
> It certainly wasn't.

The idiomatic way to do RMW (outside simple stuff like fetch-increment) with std::atomic maps 1:1 with x64 assembly and since fetch_update isn't provided, it's the only way to do it. It's way too close for comfort. See [1] for a comparison

> Total hyperbole. It's perfectly usable on ARM and other platforms.

It's not hyperbole. std::atomic is portable, but that's all it is.

std::atomic is about 30% to 40% (with outlined atomics on, which is the default) slower than handrolled asm (or custom reimplementations that provide fetch_update -- same thing). See [2] for a benchmark.

[1] https://godbolt.org/z/EasxahTMP

[2] https://godbolt.org/z/Y9jvWbbWf

Re:Writing your own C++ standard library part 2 by spacechild1 on Monday June 02, 2025 @09:39AM (#44158741)
Yes, the design of std::atomic probably favors x64 in certain areas. However, you initially claimed that std::atomic has been designed with only x64 in mind. This is simply not true, which is easily proven by the fact that they explicitly support weak memory models.

> std::atomic is about 30% to 40% (with outlined atomics on, which is the default) slower than handrolled asm

Only for certain CAS operations. A 30% or 40% performance penalty doesn't sound too dramatic and certainly makes it "usable" in my book.

I appreciate your insight, but it could have been delivered with less hyperbole.

Re:Writing your own C++ standard library part 2 by TuxSH on Monday June 02, 2025 @10:36AM (#44159285)
Apologies for the style of my previous messages.

> they explicitly support weak memory models.

Sure, but memory ordering is orthogonal to LL/SC vs CAS.

To me, fetch_update not being present from std::atomic's inception is major design oversight as CAS can be emulated via LL/SC but not the other way round.

Furthermore, fetch_update code is easy to read and less awkward to write than CAS loops (which currently are the only way std::atomic offers, and this is what I'm complaining about)

> Only for certain CAS operations. A 30% or 40% performance penalty doesn't sound too dramatic and certainly makes it "usable" in my book.

I disagree. Atomic variables (atomic instructions) are usually used to implement synchronization primitives, and are thus often meant to be used in very hot paths. 30% perf drops are actually quite bad, in that regard.

Of course if one is restricting themselves to using only member methods (fetch_add, fetch_or, etc.), then all is fine because these methods are optimized.

All in all, C++'s stdlib (the parts that aren't just __builtin wrappers, to be precise) is actually quite fine for most use-cases, like PC applications. Indeed, it is when one has latency constraints and/or severe memory constraints (e.g. < 512 KiB) that the stdlib feels like a hindrance.

Re:Writing your own C++ standard library part 2 by spacechild1 on Monday June 02, 2025 @11:35AM (#44159923)
Thanks for the leveled response!

> Sure, but memory ordering is orthogonal to LL/SC vs CAS.

Sure, but your original claim was that std::atomic has been designed with only x64 in mind. That's what I meant to argue against.

I agree that the omission of something like fetch_update() has been an oversight and I hope that it will make it into the C++ standard!

As a side note, here's what the Rust docs say about fetch_update():

> This method is not magic; it is not provided by the hardware. It is implemented in terms of AtomicUsize::compare_exchange_weak, and suffers from the same drawbacks.

https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize...

So Rust's std::sync::atomic is equally "useless"? :)

Re:Writing your own C++ standard library part 2 by TuxSH on Monday June 02, 2025 @12:32PM (#44160513)
Heh, you're right, good catch: https://godbolt.org/z/3cEfbqM51

Looks like their (Rust) main motivator was readability. Whereas P3330R0 has that + performance on non-CAS hardware in mind. In any case, Rust's function could be optimized in the future, if they decide on it.

Re:Writing your own C++ standard library part 2 by monkeyelite on Monday June 02, 2025 @10:04AM (#44158996)
I agree with that. But which language has better containers?
Re:Writing your own C++ standard library part 2 by platinumrad on Monday June 02, 2025 @12:07PM (#44160289)
Boring answer, but Rust has better containers (and worse algorithms).
Re:Writing your own C++ standard library part 2 by tialaramex on Tuesday June 03, 2025 @05:00AM (#44167904)
Neither can be compared cleanly because the languages make such different choices but it's certainly true that C++ has too many garbage "never use this" container types in the standard library. Aria has a brief aside in her "Entirely Too Many Linked Lists" tutorial about why Rust has so few containers in the stdlib (Aria weeded out all the niche container types before Rust 1.0), and it sincerely feels as though 1995 era C++ could have done with an Aria.
Re:Writing your own C++ standard library part 2 by colejohnson66 on Monday June 02, 2025 @03:10PM (#44161998)
dotnet/C#?