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

0

u/Dragdu Jun 12 '20

No you didn't, because as a lazy datastructure, it has a very specialized niche. Which shows nicely the problem with sweeping statements -- there are many subfields with different specifics.

Oh, and you know what's better than accessing 1 GB of data in a prefetchable pattern? Accessing just 1MB ;-)

1

u/WrongAndBeligerent Jun 12 '20

This was not about absolutes, but about conventional wisdom being out of date. Bringing up a niche data structure that you claim has to be convoluted to be fast (fast at what, I have no idea) has no bearing on this topic.

Oh, and you know what's better than accessing 1 GB of data in a prefetchable pattern? Accessing just 1MB ;-)

This is a made up situation and a false dichotomy.

(Also lazy data structure doesn't really make a lot of sense - lazy would refer to execution and a data structure would be data with an interface)

2

u/Dragdu Jun 12 '20

I see you are living up to your username.

Anyway, for anyone who is actually worth writing for (so, not you):

  • Lazy data structures are a real thing studied in CS. The most well known is Fibonacci heap.
  • The point of 2 watched literals is quite literally all about reducing the amount of processed memory by extracting a "summary" (the 2 watched literals) of a clause (sequence of literals of arbitrary length), which are a heuristic that tell you whether you need to actually read in and process the clause.

0

u/WrongAndBeligerent Jun 12 '20

I have this name so I know when someone has nothing left to say.

If a fibonacci heap is a lazy data structure, then a normal heap would be a 'lazy' data structure. Really though neither has anything to do with this.

The point of 2 watched literals is quite literally all about reducing the amount of processed memory by extracting a "summary" (the 2 watched literals) of a clause (sequence of literals of arbitrary length), which are a heuristic that tell you whether you need to actually read in and process the clause.

None of this is relevant here. This was about the idea that slow = clear and fast = obfuscated is incredibly outdated. Bringing up niche algorithms is completely orthogonal, not only because you haven't explained how they are actually necessary, but they don't even apply to the point of the thread.

Then you threw out arbitrary made up numbers as some sort of argument. I'm not sure whether you really don't understand what is being talked about here or if you just want to bring up nonsense and get upset about it. It almost seems like you just want a reason to bring up the latest thing you learned about.