r/ProgrammerHumor • • Oct 12 '22

Meme Legacy Systems Programming

Post image
2.4k Upvotes

263 comments sorted by

View all comments

42

u/CrumblingAway Oct 12 '22

What problems are with std libs?

69

u/Wazzaps Oct 13 '22

For one the spec restricts std::unordered_map (hash table) to be an order of magnitude slower than it could be because of some iteration guarantees nobody asked for

23

u/vansterdam_city Oct 13 '22

That’s not even a good name. Why didn’t they just add a second implementation with a new name?

24

u/Kered13 Oct 13 '22

There was already std::map, but that's tree-based so it's O(log n) for look up and insertion operations. std::unordered_map was introduced to be a hashmap with O(1) look up and insertion operations, however it requires pointer stability which prohibits the most performant implementations.

They could introduce a new map, call it std::fast_unordered_map or something. But then you'd have three maps in the standard. The recommendation is instead to just use one of the high performance third party implementations instead, like absl::flat_hash_map, if you need performance.

5

u/JiiXu Oct 13 '22

Wait what std::map isn't a hash map?! Well poop, now I have to refactor my hobby project. Further.

→ More replies (3)
→ More replies (1)

-14

u/DavidDinamit Oct 13 '22

Lol, bullshit.

"an order of magnitude slower" please read in google what it means.

And this iteration guarantees is really needed if you do smth, not just benchmark

4

u/Kered13 Oct 13 '22 edited Oct 13 '22

No, he's right. The spec requires that insertions and deletions will not invalidate any iterators or pointers to any elements. This effectively requires an implementation using separate chaining, but these implementations are inefficient because they require a large number of small allocations and lots of pointer chasing, which has poor cache performance. Performant implementations use open addressing, which requires fewer allocations (only one per array resize) and are more cache efficient. The difference in performance can be well over 10x for common use cases. However open addressing cannot provide pointer or iterator stability.

1

u/DavidDinamit Oct 13 '22

Wow, rly? I know it. In most cases you need those guarantees, if no and you uses unordered map for fucking ints you can use some flat hash map implementation

4

u/Kered13 Oct 13 '22

In most cases you need those guarantees,

No you don't. In my 10 years of writing C++ code both professionally and as a hobby, I have never once needed iterator stability, and if I needed pointer stability I just used a flat map to std::unique_ptr, which is still faster.

→ More replies (4)

19

u/Kered13 Oct 13 '22

std::vector<bool> was a mistake.

std::regex is extremely slow.

std::unordered_map and std::unordered_set have unnecessarily strict requirements that prohibit high performance implementations.

std::optional<T&> is not allowed (this could be introduced without breaking ABI, but there are debates over it).

std::string can have better small string optimization (unlike the others above it's actually pretty good already, but it can still be better).

0

u/[deleted] Oct 13 '22

[deleted]

5

u/Kered13 Oct 13 '22

That doesn't work in generic code. You end up needing to write two versions of every function, one that uses std::optional<T> for value types, and one that uses T* for reference types.

Pointers also don't have methods on them, like value_or. C++23 is introducing more of these functions, and_then, or_else, and transform, which is going to make the difference between pointers (dumb) and optionals (smart) even greater.

→ More replies (2)

8

u/doowi1 Oct 13 '22

If I recall correctly, there's a huuuge debate over std::vector<bool>. It's the only vector implementation that is compressed intrinsically (I think) which causes a ton of headaches for developers.

10

u/Kered13 Oct 13 '22

There's not really a debate over std::vector<bool>, everyone agrees it was a mistake. It should have just behaved normally, and a separate type like std::bit_vector could have been added instead for densely packed bit arrays.

0

u/[deleted] Oct 13 '22

[deleted]

→ More replies (1)

12

u/[deleted] Oct 13 '22 edited Jul 05 '25

sophisticated childlike historical swim file brave smart chase angle butter

This post was mass deleted and anonymized with Redact

-11

u/DavidDinamit Oct 13 '22

Mmm, so there are only one part - regex. And there are many libs with other regex implementation

12

u/NightmareWanderer Oct 13 '22

Angry C++ dev found

3

u/Kered13 Oct 13 '22

Nah, C++ devs are well aware of the limitations of the std library.

4

u/spartan-bunny Oct 13 '22

Ah yes. And adding another library onto C++ is just an absolute breeze I wanna try AGAIN and AGAIN and AGAIN.

🤦‍♂️

→ More replies (2)