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

Chunk - Add optimised traverse_ for StackSafeMonad. #3191

Merged
merged 1 commit into from
Mar 27, 2023

Conversation

diesalbla
Copy link
Contributor

@diesalbla diesalbla commented Mar 26, 2023

If our Applicative F is actually an instance of StackSafeMonad, we can avoid the default implementation of traverse_, which is based on a foldRight method of Foldable and has to allocate a set of Eval objects. Instead, we can just loop on the monad itself.

Note Given that in practice Streams are mostly used with IO-like datatypes which are stack-safe, this should have some impact.

@armanbilge
Copy link
Member

Linking to:

Actually, if we made this optimization in Cats, we probably wouldn't need to repeat it here?

@diesalbla
Copy link
Contributor Author

diesalbla commented Mar 26, 2023

Actually, if we made this optimization in Cats, we probably wouldn't need to repeat it here?

As it happens I was taking a look at the cats implementation, but the Foldable type class seems to provide little. With Chunk we can optimise it right away. Note that the ticket refers to the traverse method, not the procedural traverse_. It is so inconvenient we never got around to renaming it typelevel/cats#1723.

@armanbilge
Copy link
Member

Note that the ticket refers to the traverse method, not the procedural traverse_

Yes indeed, but we can make the described optimizations for both traverse and traverse_ upstream in Cats.

@diesalbla
Copy link
Contributor Author

Yes indeed, but we can make the described optimizations for both traverse and traverse_ upstream in Cats.

It may take a while for those to get released, if and when they are done (the link is to an issue). This PR is ready as of now.

If our Applicative F is actually a StackSafeMonad, we can avoid
the default implementation of traverse_, based on the foldRight
method of Foldatble with Eval, and use instead a loop.
@armanbilge
Copy link
Member

Sorry, I didn't mean it as a blocker here 🙂 rather, to further justify landing these changes in Cats.

@mpilquist mpilquist merged commit 4d95770 into typelevel:main Mar 27, 2023
@diesalbla diesalbla deleted the stacksafe_traverse_ branch March 30, 2023 15:28
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.

3 participants