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

Make string building a bit more efficient for NonEmptySeq #4326

Merged
merged 2 commits into from
Oct 27, 2022

Conversation

danicheg
Copy link
Member

String building from Iterator is about 5% more efficient than from Seq. Benchmarked on 2.13, it should be the same on 2.12, I suppose.

@armanbilge armanbilge added this to the 2.9.0 milestone Oct 27, 2022
@johnynek
Copy link
Contributor

I really appreciate PRs like this.

I'd love to see if we can search the code (or even potentially eventually use wart remover or something) to look for cases when we have .map that builds a new structure that is thrown away.

The pattern of seq.iterator.( ops ).(build result) is often a better pattern than seq.(ops).

It would be awesome if we could somehow use scala 3 metaprogramming to communicate these kinds of rewrites to the compiler.

Co-authored-by: Sergey Torgashov <satorg@users.noreply.github.com>
@armanbilge armanbilge merged commit f2d871c into typelevel:main Oct 27, 2022
@danicheg danicheg deleted the string-building-from-seq branch October 27, 2022 19:27
@satorg
Copy link
Contributor

satorg commented Oct 27, 2022

It is not a concern regarding this PR – I'm pretty sure it is good to go, thank you @danicheg !
But rather an attempt to spin-off more generic discussion.

@johnynek ,

I'd love to see if we can search the code (or even potentially eventually use wart remover or something) to look for cases when we have .map that builds a new structure that is thrown away.

The pattern of seq.iterator.( ops ).(build result) is often a better pattern than seq.(ops).

Agree that it would be really nice, but that raises a question in regards to non-empty collections. Should we consider a new abstraction of non-empty iterator for that? For example:

val nel: NonEmptyList[Int] = ???

// works because NonEmptyList#Map returns NonEmptyList
nel.map(<whatever>).reduce(<whatever>) 

// does not work because `NonEmptyList#iterator` returns just `Iterator`
// thus, we lose the non-emptiness guarantee here.
nel.iterator(<whatever>).toList.reduce(<whatever>) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants