3 ms·
The ability to figure out what you're doing and then just do that well, rather than attempting to optimise the code you wrote is called Idiom Recognition and ha
by tialaramex 4y ago
The ability to figure out what you're doing and then just do that well, rather than attempting to optimise the code you wrote is called Idiom Recognition and has been around for a very long time, like, 1970s or maybe before.
A relatively modern example was people writing code to count set bits in a value, because older languages don't provide an intrinsic to do that and it's often useful, these days your CPU probably has a CPU instruction specifically for that, it's called "popcount" (sometimes spelled popcnt) and so it's valuable to recognise what's going on in the say six lines of C++ in your program and emit a single POPCOUNT instruction instead. If your fancy method to count bits isn't recognised your program goes slower.
The memcpy case is special because C is weird in actually trying to define this as a function, copying memory is so commonly necessary that compilers need to have a pre-baked implementation for the rest of what they do anyway, it's not a surprise that idiom recognition results in your custom memcpy being turned into the built-in memcpy, although in the case you linked that's indeed a bug.