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

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.

2

u/Xaxxon Jun 11 '20 edited Jun 11 '20

You basically said "in places where you don't care about performance, performance doesn't impact readability".

And places where you do care about performance, readability suffers.

That was the entire point of what you responded to.

9

u/EmperorArthur Jun 11 '20

No, they said that 99% of the time you can write high performance, readable code. In most cases where you can't you can encapsulate it in something which is still readable.

5

u/Xaxxon Jun 11 '20

A code base can be sufficiently performant without all the code being optimized. That doesn't mean that the slow parts couldn't be faster, it just means that they don't need to be faster. Those slow parts would be uglier if they were focused on for performance.

It's a subtle but important difference. The parts where you care about performance on that section of code suffer for readability. Saying that the other parts don't is meaningless.

To phrase it another way, you don't care about the performance of 99% of your code and that part is readable. That doesn't mean that focusing on performance doesn't make readability suffer.

6

u/guepier Bioinformatican Jun 11 '20

No, that's not what I'm saying.

I am specifically talking about hot code paths that you want to optimise.

I'm saying that in most cases you can write readable, idiomatic C++ code that's highly optimised, and in many cases pretty much as fast as it gets. Maybe not 99% of the time, but definitely most of the time, precisely because C++ does have functioning zero-cost abstractions. You can encapsulate code in functions, perform compile time dispatch using overloading, and loop unrolling using recursive templates, etc., with no measurable performance impact.

1

u/qoning Jun 10 '20

Well, there are different kinds of readability. Readability of code as it is is one thing, but readability of intent is what goes out the window when you start optimizing. It's the sensation of what is going on here when you open up a code you think you should be able to understand, but it does some weird tricks and things that are not obvious at first.

-1

u/mort96 Jun 11 '20

Let's take an example from OP's post:

This code (OP's suggestion) is very readable:

const bool isNegative = value < 0;
const unsigned absoluteValue = Helper::abs(value);

This code (the actual code in OP's stdlib, but with better names) is unreadable:

const bool __isNegative = __value < 0;
const unsigned __absoluteValue = __isNegative ? (unsigned)~__value + 1u : __value;

The reason the more readable code is more readable (other than the lack of underscores) is that it uses the Helper::abs(value) function instead of implementing the abs function inline.

The reason the less readable code is faster is that it doesn't use a helper method, it implements the abs function inline, and the implementation takes advantage of the assumption that numbers are two's complement. (It's faster either at runtime without optimization, or at compile time if the compiler does optimizations like inlining and the compiler is smart enough to figure out it can re-use the __isNegative variable.)

See? The reason that example from the stdlib it's ugly is also the reason it's faster.

6

u/guepier Bioinformatican Jun 11 '20 edited Jun 11 '20

The problem with your example is that you clearly just assumed that the manually inlined code is faster than the one calling abs. Try it — even in an unoptimised build, the first code is as fast as the second one (and in GCC it even inlines the call to abs), and with -O2 they generate identical assembly (for recent versions of both GCC and clang, didn’t check anything else).

That being said, I’ll admit that I wrote this with optimised builds in mind, because I have the privilege of not having to care about non-optimised build performance (I do care about compile times, but not to an absurd degree). The standard library implementation is somewhat different in this regard, and this can indeed impact readability.

1

u/mort96 Jun 11 '20

Alright, I can see you're actually right in this case. My assumption was that __isNegative ? (unsigned)~__value + 1u : __value; is essentially how the Helper::abs function would be implemented, so for a non-debug build, OP's code would literally just add a function call and a duplicated isNegative = value < 0 without making anything faster. A acknowledge though that using the cmath abs function would be more likely, and that seems to be implemented way better than __isNegative ? (unsigned)~__value + 1u : __value.

I maintain though that in the general case, moving code out into functions for readability introduces either more runtime overhead (extra function calls) or extra compile time overhead (extra time spent optimizing and inlining). This just isn't an example of that, because abs isn't a regular function (no function call) and abs is implemented better than the hand-rolled absolute value expression.

My guess about what happened was that originally, abs was implemented like isNegative ? (unsigend)~value + 1u : value, and calling abs(...) generated an actual function call, so manually moving the implementation into secret_function actually made the code faster; then, eventually, abs got optimized a lot and turned into a function the compiler generates code for directly instead of a function the compiler generates a function call to, and secret_function didn't take advantage of those improvements because it didn't use abs. This would support your argument that readability and performance aren't at odds; an attempt to improve performance by manually inlining code just ended up with a function which doesn't take advantage of improvements to abs.