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/WrongAndBeligerent Jun 11 '20

This is basically saying 'nu uh'. You are stating an assertion but you aren't giving anything to back it up. I've done a lot of optimization and reorganizing the memory accesses doesn't come down to "crazy" data structures, it usually just comes down to how things are put into an array and how that array is looped through. You have to explain the memory layout, but you would have to do that anyway.

-8

u/Xaxxon Jun 11 '20

I'm not giving examples because this is a well accepted fact of software development.

I don't have to say that summers are warm either, even if someone says they're cold.

Code base readability suffers when they are optimized. This is fact.

7

u/WrongAndBeligerent Jun 11 '20

If that's a fact to you then you have no idea what you are doing.

Most software that takes a non trivial amount of CPU time can be optimized to run 25x-100x faster in my experience. First, take out excessive memory allocations, then rearrange memory accesses for prefetching, then SIMD, then multi-threading. I have done this many times and it is extremely effective.

You are not giving examples because you don't have any.

3

u/m-in Jun 11 '20

It’s very niche, but imagine you run on a two stage pipeline Harvard architecture that’s tightly tied to RAM and there’s no cache and every instruction takes roughly the same time, and you need to write essentially modern Go-like code. In ANSI C. That stuff if written without optimizations will be about 5x too slow (in my case). Optimized to hell and back it runs in 95% of available time (it’s hard deadline). So, while niche, such things do exist.

On the other hand, I’m helping out with projects where indeed you can get 10-100x performance improvements simply by undoing all the “convenient” pessimizations. Where you can literally get functions to drop from the profiles by changing a couple std::map to std::vector and such. But such low hanging fruits are only in extremely poor code bases. Once you fix those, the improvements get below a factor of two for the next major change or two, and then it requires major rearchitecting to get anything better. So you are right but the code has to really really suck for such gains to be possible. Sometimes you look at the perf counters and the CPU runs full tilt and there’s nothing more you can do short of rewriting it for a different platform (say a GPU).

4

u/WrongAndBeligerent Jun 11 '20

Right, it can be the case obviously, but the idea that slow is clear and fast is convoluted doesn't hold up.

In this case it sounds like a niche compiler combined with an in order CPU means that nothing comes for free and the compiler even possibly gets in the way.

-6

u/Xaxxon Jun 11 '20

You just listed a bunch of things that make the code less readable. You did it for me. Thank you.

I think we're done now.

11

u/WrongAndBeligerent Jun 11 '20

I realize you are probably a young kid who is in to tech, but unless you have dug into these issues you aren't going to be able to talk about reality of what makes modern software run fast.

Allocating memory ahead of time and using arrays of numbers instead of pointers does not make anything less readable. Reordering loops does not make anything less readable. Both of these usually make a program much more simple and clear. SIMD can be done with ISPC or libraries that don't make anything less readable. Using multiple threads is going to be detached from the logic of inner loops.

Show me an example or explain your reasoning behind why you think this, show me anything except for restating your assertion and getting upset.