Hacker News new | ask | show | jobs
by etrain 3487 days ago
This is pretty awesome. One key bit of information that the compiler has is that the coefficients are a constant array of length 12, which makes the loop unrolling possible and also means that the register magic is in play - it's seriously awesome that the compiler does this.

That said, I'd expect something similar to happen with a well-written C program. Would equivalent abstractions in C++1{1,4,7} be "costly"?

2 comments

All of the optimizations were done by llvm, so nothing stopping this from working in C++ with closures or C with functions annotated for inlining.

With C the lack of generics means that writing composable iterators is hard, though.

At least it's hard to write composable iterators that don't circumvent the type system completely.
Since ranges are not yet standard, the "equivalent" abstractions would look to be std::algorithms, but those are not zero-cost as they are not iterator adaptors, but consumers. This is extremely inefficient.

However, Boost offers iterator adapters, which although weaker than true iterator adapters (which C++ calls ranges), those will suffice in this case.

As for std::algorithms not being zero-cost. I have seen cases were std::foreach was noticably faster or much slower. I have seen it in benchmarks and it seems largely to be a quality of implementation issue.

Good implementations can cheat and use knowledge my code shouldn't have about parts of the underlying system and bad ones do extra useless crap.

Benchmarking and profiling seem to be the only real way to gain confidence in your results reliably.