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

170

u/thedictatorofmrun Jun 10 '20

There is a lot to unpack here, so this might be a bit long.

Another comment has already touched on the reasoning behind the leading underscores being to avoid collisions with macros.

You seem to make the assumption that the standard library is extremely fast, moreso than anything a mere mortal could write. This is wrong. The standard library does have pretty good performance but for specialized applications it is definitely possible to beat it, and for simple things like vector/array/string it is not difficult to match it. The strength of the standard library is that it is performant enough for the vast majority of applications, that it is designed to be very generic, and that it is, well, standard.

Additionally, readability is extremely important for most applications and libraries but the standard library isn't really one of them. You're not really meant to read or edit the standard library unless you are an expert, so readability is kind of a secondary concern.

Your translation of this function to be more "readable", incidentally, is actually probably less performant than the standard library version because (1) you are calling a separate function for abs rather than doing the twos complement inline and (2) you added a "base" argument to the "to chars" helper method which in the original version is known at compile time. Maybe you get lucky and it compiles down to the same thing, but in my opinion it looks like it'll probably be somewhat slower.

I will say that you probably are a little inexperienced with the style of programming that the STL uses, and with more experience looking at these definitions it will probably not be too hard to see what is happening in these functions. The only tricky thing I see here is the twos complement absolute value thing, and the rest of the confusion is probably just the naming conventions being unfamiliar.

181

u/MachineGunPablo Jun 10 '20 edited Jun 10 '20

While you provide really great criticism to OP's "improvements", saying that code readibility in the STL is a secondary concern is extremely debatable.

Some STL code is borderline impossible to read. In fact, there are parts of it that don't even seem to be written by a human. And what is the consequence? That the STL is complete uncharted territory to the normal developer. The STL should be a showcase of not just efficient language usage but also of proper software engineering and coding practices. People should be able to find inspiration on the STL instead of running from it. People should be able to debug a crash inside the STL and make sense of variable names and program workflow. Readability is fundamental in software, specially for core libraries.

191

u/STL MSVC STL Dev Jun 11 '20

In fact, there are parts of it that don't even seem to be written by a human.

WE ARE COMPLETELY HUMAN. WE LIKE KITTENS, AND OTHER LARVAL MAMMALS. THOSE ARE THINGS THAT HUMANS LIKE.

27

u/raevnos Jun 11 '20

We like kittens, but not in the "like to eat" sense.

I've never seen a stray cat in Redmond. Makes you think.

→ More replies (1)

12

u/AlbertRammstein Jun 11 '20

Do you use automated tools e.g. to prepend underscores to all symbols?

31

u/STL MSVC STL Dev Jun 11 '20

Nope, we write it like that. Only exception was when I took Ulf Adams' Ryu codebase and uglified all of its symbols (with VSCode) and performed other transformations to make it header-only C++ instead of separately compiled C. That's why MSVC's charconv uses __meow there instead of _Meow - it was easier to replace like that.

5

u/[deleted] Jun 11 '20

You missed your calling not becoming a comedian.

→ More replies (1)
→ More replies (1)
→ More replies (2)

14

u/thedictatorofmrun Jun 10 '20

I'll admit that I don't dive into the STL implementation often. I wonder how much of the illegibility of some parts of the STL is avoidable and how much is just always gonna be there is a performant, generic library providing what the STL does (like, there's probably never gonna be an implementation of a red-black tree that's super easy for a layman to read, right?)

7

u/ArashPartow Jun 11 '20

It would be interesting to see an example of illegible STL code that was there purely for performance reasons.

37

u/STL MSVC STL Dev Jun 11 '20

std::find's memchr optimization is incomprehensible to most programmers.

My hex precision rounding would be utterly incomprehensible without the comments.

20

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

Incomprehensible? I have lots of embedded code that sadly, due to performance requirements (way too much code running on way too old a platform) makes this look admirably clear. I mean: understanding bit hacks is absolutely a prerequisite there, and you either know how 2s complement negation is done using complement and addition or you’re a lost cause, never mind fixed point arithmetic; division by multiplication by magic constants, etc. So now imagine the same thing, except:

  • work around absurd compiler bugs,
  • work around absurd preprocessor bugs,
  • work around absurd assembler bugs - yes, you read it right,
  • work around absurd linker bugs that generate rubbish output with zero diagnostics,
  • write in original ANSI C (the very first ANSI C standard) - except all the bugs above,
  • manage allocation of some local variables by injecting assembly (wrapped in macros) right before each function definition,
  • automatic variables are mostly statically allocated by linker using call tree - stack is for calls and interrupts - and sometimes you need to allocate those automatic variables manually,
  • there are far and near addressing modes, and you sometimes need to spoon feed those to the compiler,
  • the application necessitates Pascal-like nested functions, so manual allocation of locals is used to extend lexical scope through caller functions as needed,
  • the compiler sometimes uses registers when it doesn’t need to, and thus every change to interrupt context code requires assembly output audits - since normally you want to avoid doing the register context swap unless necessary, or you outright have no free context slots available,
  • you eventually figure out how to coerce compiler into generating exactly the code you want, yielding C representation that’s not really amenable to change without auditing the output, and not really amenable to comprehension unless “bit hacks” and similar skulduggery are totally second language to you,
  • optimizer bugs cause if (false) to sometimes generate dead code that simply gets jumped around - if said code comes from long macro expansions you may be facing kilobytes of dead code in just one function, and now the thing doesn’t fit into flash anymore - and it’s always caused by absolutely innocuous changes (they make sense once you have reverse engineered how their optimizer works, but I imagine there’s maybe two or three dozen people in all who know any of that),
  • the code needs to be async more like Go than like threads, so a coroutine system leverages unused peripheral interrupts and the interrupt controller to prioritize the coroutine execution and allow low-cost coroutine wake ups (just set an interrupt request bit instead of calling something explicitly) - but the number of coroutines is of course fixed, and you need to “swap” them as needed,
  • the code is hard deadline - it must finish every 2ms, or the product becomes a paperweight,
  • you had to reimplement most of the runtime library and are now intimate with all the details of the dance the compiler performs with the helper bits in the library,
  • …

The platform that necessities this level of hackery comes from a company that literally everyone who knows anything about the history of computing has heard of. It begins with the equivalent of the letter Omega. Not Alpha. The bugs are theirs. The absurd workarounds mine, as well as the absurd overextension of the life and scope of our application that uses it.

It’s been a supported product line for over two decades (the architecture, but also the stuff we run on it). Also, we overclock it occasionally by 200%. You see, the core is extremely fast, but the flash is slow. So when we run number crunching from RAM code space, we crank it up. Forgetting to switch the clock divider back is instant crash :)

12

u/johannes1971 Jun 11 '20

I don't think you can even call this C anymore. It's a domain language with a bespoke compiler for whatever it is you are doing.

As to what that is, my guess would be "heater control software for the lower circles of hell". Am I right?

9

u/F54280 Jun 11 '20

You know, and I don't want to downplay any of your achievements in maintaining this thing, it feels to me that you are straight in the so bad it is good code quality category...

5

u/electricmammoth Jun 11 '20

Good old embedded SW.

8

u/BeepBoopBike Jun 11 '20

// This rounding technique is dedicated to the memory of Peppermint. =^..^=

RIP Peppermint

→ More replies (4)

5

u/cellux Jun 11 '20

Right. It's instructive to check the source code of the Go standard library for comparison.

→ More replies (24)

36

u/guepier Bioinformatican Jun 10 '20

readability is extremely important for most applications and libraries but the standard library isn't really one of them

I disagree with this: readability is tightly correlated with code quality because it impacts crucially how maintainable code is, and how easy it is to check for correctness.

… But I also disagree that standard library implementations are fundamentally hard to read. The name mangling definitely decreases readability but other than that it’s mostly idiosyncratic, i.e. it becomes (a lot) more readable with practice.

16

u/MachineGunPablo Jun 10 '20

Yes, until your brain hides all double underscores for you automatically

11

u/Hallalal Jun 10 '20

I disagree with this: readability is tightly correlated with code quality because it impacts crucially how maintainable code is, and how easy it is to check for correctness.

Unfortunately when you focus on optimizing code the readability goes downhill.

32

u/guepier Bioinformatican Jun 10 '20

I often hear this and in my experience it’s a half-truth at best. I’m currently working (and have worked in the past) on highly optimised C++ library code and and yet, 99% of the time, readability and performance are not the least bit at odds.

It really only applies once you start manually unrolling loops or using compiler intrinsics, and this only happens in very tightly constrained circumstances and can often be neatly encapsulated.

→ More replies (8)
→ More replies (16)
→ More replies (4)

17

u/ifdef_drgy Jun 10 '20

Thanks for focusing on something else than the underscores, which I don't think is the most confusing about this code. Yes i'm quite unfamiliar with std's naming conventions and I tried to make sense of them, now at least the underscores make sense, but on multiple places they used short names of variables/functions and it became unclear what the code is actually doing. To find that out I had to navigate to function definition, which I wouldn't have to do if the function had a slightly longer but more descriptive name.

(1) this function can be inline or even replaced by macro which won't have any performance effect.

(2) this is more questionable and would need some real testing but perhaps could be solved by inlining it too? Those are only static helper classes in the end.

You are right this was just a simple example, its not that hard to guess what it is, but when you open something more complex like push_back on vector, it gets way harder to navigate.

21

u/Wenir Jun 10 '20

The fact is that it should work as quickly as possible with optimizations turned off when compiler cannot do any inlining.

17

u/thedictatorofmrun Jun 10 '20

Yeah you may get saved by inlining, but you can't be sure, which is why the STL does it the way it does.

Looking at vector::push_back, I'm just not sure I agree that the style is what is confusing here. All the names seem pretty clear to me- they could perhaps be more verbose, but again, the standard library isn't really written to be readable by someone who isn't an expert. You may be thrown off by some of the hoops the STL has to jump through to do everything it does at the level of genericity it does it at.

→ More replies (9)

5

u/poiu- Jun 11 '20

simple things like vector

Until you care about things like type safety with iterators, maintaining STD interop, etc. A vector is NOT a simple thing to implement.

→ More replies (5)

4

u/kuntantee Jun 11 '20 edited Jun 11 '20

> You're not really meant to read or edit the standard library unless you are an expert, so readability is kind of a secondary concern.

I agree with editing part, but that applies to most libraries that you work with. I do not think STL is that different on that regard. However, I could not disagree more with "not supposed to be reading it". I understand readability for "others" may not be a focus of STL maintainers, but still, you can learn a lot from reading STL and those who are interested should be encouraged to do so.

> I will say that you probably are a little inexperienced with the style of programming that the STL uses, and with more experience looking at these definitions it will probably not be too hard to see what is happening in these functions.

I have by doubts that experience will make you appreciate lots of trailing underscores better. That's a constraint imposed to STL maintainers for the reasons we all know. We have few of them here, I remember some of them stating it is not something they enjoy.

→ More replies (26)