3 ms·
Yet, my very first C++ program was to declare an object that contained a byte, had a constructor to initialize that byte, and a method to return it. My 'main' d
by JoeAltmaier 10y ago
Yet, my very first C++ program was to declare an object that contained a byte, had a constructor to initialize that byte, and a method to return it. My 'main' declared an instance initialized with 'true' and the code simply returned the accessor method's value.
The generated code? "mov ax, 255; ret"
That was pretty good optimization, seeing completely through the abstraction and flattening it to the simplest possible implementation. I was impressed.
Anyway there're lots of further abstractions these days, harder to see through. I imagine a clever human, with domain knowledge, can write a loop to do what a high-level library used to do.
- dahart 10y agoThis is a great example of something compilers definitely do better than humans writing assembly. Inlining code, especially with multiple passes, is something humans don't do. I still modularize in assembly and write functions, factor code used in multiple places into a single spot. The compiler is free to expand the same function inline a thousand places, and then use the expanded code to collapse to fewer instructions. The flattening can definitely be cool to see. So in your example, what would you have done if you'd written the assembly yourself? Do you feel like the compiler did something you wouldn't have?
- JoeAltmaier 10y agoMany programmers would have declared the object, set its members, and written main to fetch and return the data member. Not unreasonable, since humans tend to write abstractions pretty much as they're stated.