Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid introduction of scopes for pure combinators #1055

Closed
mpilquist opened this issue Jan 6, 2018 · 0 comments
Closed

Avoid introduction of scopes for pure combinators #1055

mpilquist opened this issue Jan 6, 2018 · 0 comments
Milestone

Comments

@mpilquist
Copy link
Member

Consider the definition of Stream#map:

  def map[O2](f: O => O2): Stream[F, O2] =
    this.repeatPull(_.uncons.flatMap {
      case None           => Pull.pure(None);
      case Some((hd, tl)) => Pull.output(hd.map(f)).as(Some(tl))
    })

where repeatPull is:

    def repeatPull[O2](
        using: Stream.ToPull[F, O] => Pull[F, O2, Option[Stream[F, O]]]): Stream[F, O2] =
      Pull.loop(using.andThen(_.map(_.map(_.pull))))(self.pull).stream

This is implemented via the pull API, which results in the introduction of a scope when converting the pull back to a stream (hidden inside of repeatPull in this case). However, we know that the pull in this case isn't doing any resource allocation so we can avoid the scope introduction and hence avoid the performance penalty entailed with a new scope.

We already have a way to convert from Pull to Stream without scope introduction (.streamNoScope). We could use this in our built-in pure combinators and we could introduce repeatPureNoScope defined as:

    def repeatPullNoScope[O2](
        using: Stream.ToPull[F, O] => Pull[F, O2, Option[Stream[F, O]]]): Stream[F, O2] =
      Pull.loop(using.andThen(_.map(_.map(_.pull))))(self.pull).streamNoScope

I suspect we'd get a significant performance boost from this. Thoughts?

@mpilquist mpilquist added this to the 0.10 milestone Jan 6, 2018
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

No branches or pull requests

1 participant