3 ms·
One of my favorite system programming tricks is to never believe that a "zero cost abstraction" lives up to the name.
by notacoward 7y ago
One of my favorite system programming tricks is to never believe that a "zero cost abstraction" lives up to the name.
- hinkley 7y agoSometimes programmer advice needs disclaimers like prescription medicines. † Offer only valid in the contiguous United States. Offer cannot be used in combination with any other offers. See stores for details. ‡ When not in use, return Happy Fun Ball™ to its protective lead case.
- jjuhl 7y agoModern optimizing C++ compilers (especially with Link Time Optimization enabled) are pretty amazing and can very often actually achieve that abstraction collapsing.. But, of course, always measure.
- notacoward 7y agoWhile it's true that modern compilers are wondrous things, checking whether they're clever enough to optimize away a particular construct - and to do so correctly, and to continue doing so in the next release - still takes time. If the same optimization can be done at a higher level, such that it will apply for any correct (but not necessarily clever) compiler, that's preferable. In my experience that's practically all the time. The best compiler optimizations IMO are the ones that can't be easily done at the source level.
- vardump 7y agoYeah, don't believe in dogmas, measure. Zero cost abstractions sometimes are anything but.
- hinkley 7y agoAnd sometimes the surprises can be in the other direction. Cache thrashing is a thing. And perf analysis tools have bookkeeping errors.
- saagarjha 7y agoThe abstraction necessary to make tagged pointers platform-dependent is quite small and trivially removed by any optimizing compiler.
- ajross 7y agoYeah, but an even more important system programming trick is to measure what you're doing every time. Performance optimization at this level is never about just trusting tools. If you aren't willing to be reading generated machine code and checking perf counters, you aren't really going to get much benefit. And if you're willing to check your optimizations by reading disassembly, tricks like stuffing tag bits into the bottom of aligned pointer values is pretty routine.