Open kunyavskiy opened 1 year ago
@kunyavskiy Do you mean something like https://doc.akka.io/docs/akka/current/stream/operators/Source-or-Flow/conflate.html ?
@kunyavskiy Do you mean something like https://doc.akka.io/docs/akka/current/stream/operators/Source-or-Flow/conflate.html ?
Yes, thanks for the nice example of the already existing API in another project.
See also #3316
Use case
I have some data, which is represented as flow of updates. And I need to calculate SateFlow of some derived data, which is essentially can be thought as Map<Int, SomeOtherData>.
Basic implementation would look like the following:
Unfortunately, this code works too slowly. I have two optimizations I want to implement.
conflate
call before slow computation to avoid some recomputation.The problem is I see no way of implementing both of them simultaneously, as after implementing the second optimization, I can't drop intermediate recalculations.
The Shape of the API
I think it would be nice to have a function like
fun <T> Flow<T>.conflate(merge : (T, T) -> T)
with semantics "If a new element comes before the old one is received downstream, they are both replaced with merge(old, new)".In my case, this would allow merging a bunch of updates that came while
slowCompuatation
runs in one update, avoiding intermediate recalculations, but at the same time recalculating only data affected by this bunch.I failed to implement exactly this function based on conflate implementation, as I don't really understand what is
ChannelFlowOperator
. But I have a prototype of conflate + map based on based on StateFlowBut it has a lot of issues. For example, it doesn't propagate the end of flow correctly, doesn't correctly handle nullable types, and I'm not sure if works correctly if merge or process will throw. It sounds that implementing it fully correctly would require a deeper understanding of flow invariants than I have.