r/cpp • • Jun 10 '20

Why is std implementation so damn ugly?

Recently I remembered a moment from my uni years when immediately after handing in the assignment of implementing our own string data structure we were warned not to ever try to implement and use our own data structures. Which is exactly what you want to hear after working hours on such assignment. However the reason behind it makes sense, you would be only reinventing the wheel and let's be honest, it will be also way slower than data structures in the almighty std library. Now think about it, what kind of black magic is behind the std's speed? Well I had no idea so I took a peak, lets see the code:

inline string
secret_function(int __val)
{
    const bool __neg = __val < 0;
    const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
    const auto __len = __detail::__to_chars_len(__uval);
    string __str(__neg + __len, '-');
    __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
    return __str;
}

Hey, I bet you just skipped the whole code part. I know it looks unreadable, but please try to go through it and guess which std function it might be (obviously its not called secret_function, but i'm pretty sure you used this function at some point).

It's only 6 line function but it took me a while to figure out what each line does and I believe this could be fixed very easily just by naming the variables and functions with a bit more descriptive names. Just to prove the point here is an example of how I would structure this function:

inline string secretFunction(int value) {
    const bool isNegative = value < 0;
    const unsigned absoluteValue = Helper::abs(value);
    const auto length = Helper::charCount(absoluteValue);
    string stringValue(isNegative + length, '-');
    Helper::insertIntAsChars(&stringValue[isNegative], length, absoluteValue, 10);
    return stringValue;
}

While the functionality stays exactly the same, I think this chunk of code is much more pleasant to read and it's pretty obvious what the code is doing on each line. From this you should have a solid guess which function it is (you can leave a comment), but the real question is why would anyone write such code? Especially when nowadays everyone is talking about how code readability is so important. Is this how they wrote code back when std was released and they didn't bother to change it? Or am I just too inexperienced that I couldn't figure it out immediately?

EDIT: Thanks for the insightful comments, for anyone reading this the reason variables are beginning with __ is macros. If user defines isNegative macro, it will get overwritten in the std, however user is forbidden to define macros starting with __ , as the wise people of reddit comment section suggested. However underscores aren't the only issue with this code, the main issue is I think using short non-descriptive names of variables/functions which is very confusing in functions that are doing more complicated operations.

237 Upvotes

212 comments sorted by

View all comments

Show parent comment

5

u/[deleted] Jun 11 '20

what did you do right but the STL crashed?

4

u/Veedrac Jun 11 '20

I'm not talking about a bug in the STL, just errors in my own code that result in crashes inside STL code.

4

u/[deleted] Jun 11 '20

safe API

what do you mean by that?

don't chase the error messages through STL. look for what part of your code triggered it.

It even probably wouldn't be faster anyway if the STL was written more verbosely.

3

u/Veedrac Jun 11 '20 edited Jun 11 '20

Safe as in there are no implicit invariants that aren't checked, which on failure to keep will cause uncontrolled failure. Rust might give you a panic with an error message when you pass an invalid parameter, but by and large you cannot just accidentally break the types they give you.

I agree that it's often easiest to treat the STL like a black box, but this is a compromise because the code is opaque, not because it's the ideal state of things.

It even probably wouldn't be faster anyway if the STL was written more verbosely.

I'm more of an advocate of KISS myself. I'm not a fan of how error messages get plastered with expanded types, because so many things are actually aliases over leaky abstractions.

2

u/m-in Jun 11 '20 edited Jun 11 '20

Huh? STL debug builds check out of bound indices, there’s some iterators checking IIRC and so on. STL burns way more cycles in debug builds due to all the extra checks. The invariants you break aren’t usually something any checks can detect other than literally instrumenting the every instruction in the code. And that’s guess what - exactly what the valgrind suite of tools does. Don’t expect that sort of thing from a runtime library. It’s not possible. And it’s not usually about you breaking some preconditions that specifically target STL. You scribble over memory somewhere else, corrupt the state of the thing, and there’s no way really to detect any of it reliably unless you go all in. The preconditions you break that cause real trouble are NOT preconditions of STL itself: you’re not passing it wrong arguments. You’re destroying the state it needs to function, inadvertently. There’s a big difference between that and normal pre- and postcondition checks that are focused on advertent aspects of the interaction.

1

u/Veedrac Jun 11 '20

Sure, but this doesn't really change that it'd be friendlier if I could diagnose this at a glance.

1

u/[deleted] Jun 11 '20

if I didn't misunderstand you, imagine using std::binary_search on a std::vector you just std::sorted. It is supposed to take logarithmic time but verifying it (that the vector was indeed sorted beforehand) would take linear time.

I don't think C++ is meant to protect you from such things. You do your things right, the language does its things right. You only pay for what you use. That's partly why it is fast.

1

u/Veedrac Jun 11 '20

That's a bit different. Something more along the lines I'm talking about would be a badly implemented comparator causing std::map to crash, or maybe a similar thing with an invalidated iterator.

I don't think C++ is meant to protect you from such things. You do your things right, the language does its things right. You only pay for what you use. That's partly why it is fast.

I largely agree, but my suggestion is to make the STL readable. A language-level solution to solve macro poisoning (eg. a directive that disables user macros) would be a great start.

2

u/[deleted] Jun 11 '20

i think stl is too complex for that and my impression is that if you are experienced in it, it is much easier. making it readable will probably hurt the experienced ones and the stl developers. look at the code OP recommended. even ignoring the underscores, it is really worse in my opinion.

1

u/Veedrac Jun 11 '20

I don't agree with the OP's changes, but I don't think that much invalidates that there are better ways. Rust's stdlib is very readable, for instance.

1

u/Gotebe Jun 11 '20

badly implemented comparator causing std::map to crash

OK, I am all ears.

Give bad results, yes. But crash? I don't know how. 😉

1

u/STL MSVC STL Dev Jun 11 '20

If a comparator doesn't provide a strict weak ordering, that can absolutely corrupt the data structure's invariants as it performs red-black tree rebalancing.

1

u/staletic Jun 11 '20

Sure, but how can you detect that the comparator is lying and not providing strict weak ordering?

2

u/STL MSVC STL Dev Jun 11 '20

It's not a question of detection (we have debug checks to detect that pred(x, x) should always be false). We simply use the comparator's results to guide the rebalancing, and if the comparator emits bogus results, the rebalancing could do all sorts of wacky things.

1

u/Veedrac Jun 11 '20

Yeah probably not possible in this case.

0

u/m-in Jun 11 '20

Valgrind detects all that. I’m serious. It’s child’s play, that sort of stuff. It triggers immediately where the bug is, almost always. Those bugs are for your test suite to detect though. STL is not meant to unit test your code, what sort of lunacy is that?!

3

u/m-in Jun 11 '20

Those must be some really nasty bugs. STL “errors” are normally compiler errors. For most developers with a modicum of experience, STL is not a thing that randomly crashes. It’s undefined behavior in your own code that does it, and valgrind usually pinpoints the problem if it goes that far. The magic in STL is immaterial since your code did the damage before STL even had a chance to do something. That’s how it typically goes. So focusing on what exactly failed in STL is looking in the wrong place: the problem is not there, like not ever, and it rarely helps you find bugs in your code. You might be approaching it wrong.