In our code we often have some iterable on which w...
# announcements
v
In our code we often have some iterable on which we do several
map
and
filter
. We have recently gone through the code wiht a profiler and this led us to use
asSequence
so that we have
myIterable.asSequence().map { .. }.map { .. }
. Why the compiler cannot make this optimization automatically? It’s quite obious that I’m not using the intermediate object.
s
I suspect this could probably be done automatically, but this optimization can only be done when those map and filters are pure.
Sequence.map
and
Sequence.filter
return sequences, so they are evaluated lazily. If they are not pure then you could get end up with a completely different result from
Iterable.map
and
Iterable.filter
.
v
Good point. It would require having the compile check for purity (which shouldn’t be too hard?). Or having some way of annotating blocks as pure
f
This is not trivial and you can do so many nasty things in there, catching them all would be very hard. Using
asSequence
is the easiest route.
👍 1
s
@Fleshgrinder Can you elaborate on the additional side effects beyond the ones I mentioned above? I agree detecting if a function is pure is non-trivial. For instance, static methods of Java classes may or may not, themselves, be pure, so you’d have to do static code analysis of called methods too. Are there other problems besides this?
f
How do you proof this for a native function for instance (JNI).