Was there an issue to implement loop merging for c...
# language-proposals
Was there an issue to implement loop merging for chains of collection transformations (
, etc)? I remember some discussions going, but I can't find the issue.
loop fusion
. But I still can't find an issue or a forum discussion
I think I remember @abreslav explaining why it was hard to implement. Was it documented somewhere?
A chain of operations cannot be fused into a loop readily because it would change the operation execution order.
However recently it was proposed that the chain of sequence operations like
could be fused as it won't change the order of operations.
@ilya.gorbunov my idea is to fuse normal
by re-useing the intermediate list that they create. I am running some benchmarks now, but preliminary results show 30% reduction in allocated memory, and this is for relatively uncomplicated examples.
`xs.map(f).filter(g)`: f f f g g g For-loop: f g f g f g Unless we can prove that such transformation is equivalent (or at least that f is "pure"), we can't do that automatically.
On HotSpot, call chain in fact has mostly similar performance with the 'for', because intermediate containers are rather short-lived, and gc cleans them fast enough. Unfortunately, that's not true on Android. We know that, and we are considering some options (loop fusion, collection comprehensions, intrinsics for sequences,...). Not in a short-term, though.
@dmitry.petrov my solution preserves the order. Looping is kept the same, but instead of several intermediate lists it creates only one, then mutates it.
Sounds good. Feel free to submit a pr.