Was there an issue to implement loop merging for c...
# language-proposals
v
Was there an issue to implement loop merging for chains of collection transformations (
map
,
filter
, etc)? I remember some discussions going, but I can't find the issue.
A.k.a
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?
i
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
coll.asSequence().map{...}.filter{...}.toList()
could be fused as it won't change the order of operations.
v
@ilya.gorbunov my idea is to fuse normal
coll.map{}.filter{}
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.
d
`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.
v
@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.
d
Sounds good. Feel free to submit a pr.
v