Repository navigation
Overloaded Stream methods for pure/effectful transformations - #1045
Conversation
|
With the changes to |
|
Exciting! Thanks for adding the new benchmark as well 🙏 |
| .filter(_ % 2 == 0) | ||
| .map(_ + 1) | ||
| .runFold(0)(_ + _) | ||
| .eval |
There was a problem hiding this comment.
I hadn't noticed that the eval is not needed here 👍
| Stream[V2, S](ArrowEffect.handleState(t1, (), emit)( | ||
| [C] => | ||
| (input, _, cont) => | ||
| val c = input.map(f) |
There was a problem hiding this comment.
I wonder if we can specialize Stream with some inlining, then use specialized Chunk methods for a potentially fully unboxed Stream encoding...
There was a problem hiding this comment.
The challenge is the size of the inlined tree but it might be possible do something like:
inline def map[V2](inline f: V => V2)(using ...) =
_map(new Mapper(f))
def _map[V2](mapper: Mapper[V, V2]) = ... Essentially stage only the portion of the code that needs specialization. Let's follow up on this separately, though.
|
|
||
| The `Stream` effect is useful for processing large amounts of data in a memory-efficient manner, as it allows for lazy evaluation and only keeps a small portion of the data in memory at any given time. It's also composable, allowing you to build complex data processing pipelines by chaining stream operations. | ||
|
|
||
| Note that a number of `Stream` methods (e.g., `map`, `filter`, `mapChunk`) are overloaded to provide different implementations for pure vs effectful transformations. This can make a big difference for performance, so take care that the functions you pass to these methods are typed to return pure values if they do not include effects. Unncecessarily lifting them to return `A < Any` will result in perfomance loss. |
There was a problem hiding this comment.
I suspect this isn't necessary to mention, though I could be wrong.
There was a problem hiding this comment.
It makes a substantial difference for performance and would be easy to miss, since it seems like the same function. @fwbrasil what do you think?
There was a problem hiding this comment.
My general point is not that this doesn't matter (it does). I just suspect few people expose methods on streams that accept functions. If they do, they are likely going to be effectful or not...
There was a problem hiding this comment.
My concern is that Kyo generally permits you to treat A and A < Any interchangeably without having to worry about performance like you do you in ZIO. Users are likely, therefore, to treat the following two scenarios as equivalent and might very easily miss the overload:
val values1 = stream.map(v => Var.run[Int](0)(someVarMethod(v)).eval).run
val values2 = stream.map(v => Var.run[Int](0)(someVarMethod(v))).runIn this case, I presume, values1 will be evaluated more performantly than values2.
There was a problem hiding this comment.
I think it's good to mention it since the performance different can be surprising
| using | ||
| tagV: Tag[Emit[Chunk[V]]], | ||
| tagV2: Tag[Emit[Chunk[V2]]], | ||
| discr: Flat[Boolean], |
There was a problem hiding this comment.
It might be worth adding a dummy evidence specifically for this purpose in the future.
There was a problem hiding this comment.
Added Dummy to object Stream to make it clearer to developers that this Flat isn't needed. If we need to use it elsewhere we can move it to kyo-data
…o into stream-sep-pure-effect
|
Apologies for the radio silence the past few days. I was down with flu but I'm getting better now. I hope the project will eventually continue moving forward without me :) We only need an approval of one core contributor to merge |
Fixes #1026
Problem
Stream transformation methods like
map,filter, etc. currently use Kyo's effectful traverse utilities to support effectful transformations across chunks. This approach is not optimal for pure transformations, however, where each chunk could be more performantly transformed using Chunk's own methods.Solution
This PR overloads a number of Stream methods to provide separate implementations for pure and effectful transformations. The performance improvement is reflected in
StreamBench.Notes
runFold, a separaterunFoldKyomethod was added because overloadingrunFoldinterfered with type inference on the folding function.map), this evidence is (as far as I can tell) trivial.Checklist