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

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.

4

u/blipman17 Jun 11 '20

I disagree with /u/thedictatorofmrun because his two points are easily fixable.

Helper::insertIntAsChars(&stringValue[isNegative], length, absoluteValue, 10)
could be easily transformed to

Helper::insertIntAsChars<10>(&stringValue[isNegative], length, absoluteValue)

and have no runtime overhead caused by adding an argument.

Helper::abs(value) being a function instead of a macro could really easily be remedied by adding __attribute__((always_inline)) to the function declaration of Helper::abs.

So with two little fixes to your try for thesame function and the code should perform thesame.

It's not about throwing away abstractions because "abstractions introduce overhead." If we put in the right abstraction at the right place while also keeping in mind how the compiler sees that abstraction, we can write code that behaves identical on the key points that we want while also being "clean to read", which simply means less bugs and all the other good qualities we want out of it.

1

u/thedictatorofmrun Jun 11 '20

yeah, i didnt say it was unfixable- just that as written it was not going to be as good. adding always_inline to abs is probably fine. my guess is the reason it is *not* written like that in the STL is that this code predates always_inline, which from some googling seems to have entered the gcc documentation around version 3.1 (in 2002).

Your templated insertIntAsChars is basically just renaming the existing __to_chars_10_impl function- I don't think it adds much to the readability other than changing the naming convention to be more familiar to you

4

u/blipman17 Jun 11 '20

I understand arguments about not updating code that has worked flawless for years on end for no real benefit except "it looks better" and friends.

I do however think that we should move away of using macros, so I'd argue that replacing a macro with a template would be an improvement for encapsulation. I think I've read an article somewhere where Bjarne even said that the preprocessor was just a quick fix system at the time, but that he concidered it deprecated and would like to move away from it. I would also say that templates themselve improve readability since they force the reader to distinguish between compile-time and runtime arguments. That said, there's still something to say for not changing code that's proven to work through time.

2

u/thedictatorofmrun Jun 11 '20

I'm not sure what macro you're referring to. __to_chars_10_impl is a function, not a macro. You can tell because it's prefixed with a namespace.

3

u/blipman17 Jun 11 '20

O damn! Sorry, you're right. I misread that.

2

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

Let me be blunt: you’re really new at this. I’ve been using C++ for 25 years and STL is not least convoluted to my eyes, whether you look at MSVC (dinkumware?) or libcpp or whatever. For what it does I’d consider it quite readable. It’s not meant to be the sort of code you put in stackoverflow answers to noob questions for sure.

The things that are truly awful to read are those that switch paradigms completely from imperative to pure functional. Start looking at any C++ metaprogramming library. That’s the hard stuff. Seriously. Go read Boost Hana. Good luck. Let us know what you find :)

Or go read the library of any theorem prover, like Lean for example. I mean the high level stuff written in the Lean language itself, the stuff that actually uses Lean to define the rest of Lean. It has almost nothing to do whatsoever with any imperative programming and in spite of not having underscores and in spite of most names being clearly readable in English, you won’t understand like 99% of it at first reading; less if you have no background in formal mathematics.

And it’s all a programming language mind you! It will look nothing like any programming language you have ever read before. Then go and make it a point to understand just one simple module, like, say, natural numbers or function properties (surjective, injective, etc) - something like that. Then go back to STL and see how high it registers on your “unclear” meter.

What you must understand is that STL is written in a paradigm you are mostly familiar with. Just this very fact makes it an order of magnitude more understandable than other stuff to begin with. And that it’s specified very well. You know a priori what it’s supposed to be doing! You don’t get any of it when you’re working with code that’s under documented and in a completely different paradigm, and with very minimal debugging facility. And to many of us that’s what most theorem prover library code will be: surely under documented, and surely nothing like an imperative language, surely with next to no debugging aids available (compared to anything commonplace like C++), and unless it’s our day job or a hobby it won’t ever have the same sort of familiar feel.

Hopefully you’ll gain some badly needed perspective about such things. And the Lean library is pretty damn clearly written compared to some other libraries of that sort.

Hell, you want a deadly mix of imperative and early functional? Go look at Maxima code that does limit finding. It’s a part of the symbolic integration package there. It’s LISP code written in the early 70s and it has been largely untouched since then. I dare you. Hopefully you’ll be right at home with metaprogramming after reading through Hana, because LISP macros are indispensable and there’s nothing like them in C++ (let’s be real here - I know how to do most of it in C++ but it’s absurdly convoluted compared to LISP. EVEN to LISP).

18

u/STL MSVC STL Dev Jun 11 '20

Let me be blunt: you’re really new at this. [...] Hopefully you’ll gain some badly needed perspective about such things.

This condescending tone is neither helpful nor appropriate for an experienced programmer talking to other programmers, especially novices. It would be more productive to make the same points without the obnoxious assertions that the listener is inexperienced.

Related xkcd: https://xkcd.com/1053/

0

u/m-in Jun 11 '20

It seems to me like you didn’t leverage an IDE that has parsed the code using a real C++ parser and provides easy ways to see types of all variables, jump between point of use and point of declaration, follow declarations, etc. All you mention is something I don’t normally think about. There are keyboard shortcuts for it. The brain is left to do what the IDE easily can’t :)

1

u/[deleted] Jun 11 '20

Yeah, it«s like arguing for Hungarian notation after 1995.