3 ms·
> Need to build up an intermediate list and then discard it? Easy! No worries! In Haskell efforts are under way to have your cake and eat it, too. Stream fusi
by eru 15y ago
> Need to build up an intermediate list and then discard it? Easy! No worries!
In Haskell efforts are under way to have your cake and eat it, too. Stream fusion (and more general deforestation techniques) allow you to have intermediate data structures at the level of source code logic, without them ever showing up in the compiler's output. Such tight loops don't produce any garbage.
- lubutu 15y agoThis is one of the examples I use to explain why higher-level functional languages can be more drastically optimised than lower-level imperative languages. These fusions are also sometimes called hylomorphisms.
- eru 15y agoYes. Though at the moment the stress on that sentence is mostly on the `can be', not `are', yet.
- SamReidHughes 15y agoAnd then your code is extremely fragile against changes and its performance is dramatically dependent on the compiler. I'd rather use the Common Lisp LOOP macro. (Or require that stream fusion be explicitly declared, in order for the compiler to use it.)
- eru 15y agoYou can actually add in your own rules for loop fusion. And the built-in rules are also expressed in the same programmer-readable format. (As far as I remember.) The rules aren't buried deep inside the compiler. The code also isn't too fragile. The rules are pretty easy to internalize. In general Haskell performance is pretty fragile anyway, i.e. sensitive to minimal changes in your code. Nobody has really figured out how to reason about laziness.