Skip to content

Overloaded Stream methods for pure/effectful transformations - #1045

Merged
fwbrasil merged 16 commits into
getkyo:mainfrom
johnhungerford:stream-sep-pure-effect
Jan 31, 2025
Merged

fwbrasil merged 16 commits into
getkyo:mainfrom
johnhungerford:stream-sep-pure-effect

Conversation

@johnhungerford

Copy link
Copy Markdown
Collaborator

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

  • In one case, runFold, a separate runFoldKyo method was added because overloading runFold interfered with type inference on the folding function.
  • In other cases, overloading required adding implicit evidence. In several of these cases (all except map), this evidence is (as far as I can tell) trivial.

Checklist

  • Unit test all changes
  • Update scaladocs if needed
  • Update the README if needed

@johnhungerford
johnhungerford marked this pull request as draft January 22, 2025 15:04
@johnhungerford

Copy link
Copy Markdown
Collaborator Author

With the changes to Stream, Kyo is faster than ZStream and close to fs2 in performance for both sync and fork in the existing StreamBench when running on my laptop. I added a new StreamIOBench benchmark that uses suspended IO effects when filtering and mapping. For the new benchmark, Kyo is faster than fs2 and ZIO by more than a factor of two on my laptop.

@fwbrasil

Copy link
Copy Markdown
Collaborator

Exciting! Thanks for adding the new benchmark as well 🙏

.filter(_ % 2 == 0)
.map(_ + 1)
.runFold(0)(_ + _)
.eval

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't noticed that the eval is not needed here 👍

@johnhungerford
johnhungerford marked this pull request as ready for review January 22, 2025 18:43
Stream[V2, S](ArrowEffect.handleState(t1, (), emit)(
[C] =>
(input, _, cont) =>
val c = input.map(f)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can specialize Stream with some inlining, then use specialized Chunk methods for a potentially fully unboxed Stream encoding...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread README.md

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this isn't necessary to mention, though I could be wrong.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

@johnhungerford johnhungerford Jan 31, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))).run

In this case, I presume, values1 will be evaluated more performantly than values2.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding a dummy evidence specifically for this purpose in the future.

@johnhungerford johnhungerford Jan 30, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fwbrasil

fwbrasil commented Jan 31, 2025 •

Copy link
Copy Markdown
Collaborator

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

@fwbrasil
fwbrasil merged commit 2c4288b into getkyo:main Jan 31, 2025
@johnhungerford
johnhungerford deleted the stream-sep-pure-effect branch February 1, 2025 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StreamBench is not fair

3 participants