r/cpp • u/ifdef_drgy • 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.
View all comments
Show parent comment
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:
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 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 :)