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 allocations for Monoid instances where possible #4309

Merged
merged 1 commit into from
Oct 9, 2022

Conversation

bplommer
Copy link
Contributor

Prompted by http4s/http4s#6712, which locally caches a particular Monoid[List[*]] instance - the issue is a generic one so it seems better to apply this optimisation here.

There are other cases where this kind of change could be made too but I wanted to get feedback before doing any more work on it.


object ListMonoid {
@nowarn("msg=deprecated")
private val singleton: Monoid[List[Any]] = new ListMonoid[Any]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private val singleton: Monoid[List[Any]] = new ListMonoid[Any]
private[this] val singleton: Monoid[List[Any]] = new ListMonoid[Any]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought private[this] was in line for deprecation in Scala 3?

Copy link
Member

Choose a reason for hiding this comment

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

private[this] and private are effectively the same in Scala 3. But it still means in something in Scala 2.

Comment on lines +1417 to +1418
private val singleton: Monoid[Chain[Any]] = new Monoid[Chain[Any]] {
def empty: Chain[Any] = Chain.nil
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need an object for this? can we just put it alongside the implicit def.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not - I did it for consistency with the other cases but happy to change it.

That also raises the question of whether to go ahead with adding the companion objects in the other cases. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

That's a good question. In those cases the monoid class was publicly exposed. In this case the object is private, seems to be created unnecessary indirection

Tbh I just don't think it's that valuable—if users directly want the monoid instance can't they just grab it from the implicit def instances or whatever?

Copy link
Member

Choose a reason for hiding this comment

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

I think you run into bincompat issues if you put it on the trait. Or at least I did in #4313

Copy link
Member

Choose a reason for hiding this comment

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

You are right, but should be fine if either the trait is private/sealed or it's actually an (abstract) class 😛

Copy link
Member

Choose a reason for hiding this comment

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

Ah gotcha 👍 Whereas ListInstances is a public trait

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, for those we will have to store the instance elsewhere 👍

@armanbilge armanbilge added this to the 2.9.0 milestone Sep 26, 2022
@TimWSpence
Copy link
Member

OK I've made the changes above. How do I actually add the commits to this PR? 😅 Do I need push access to Ben's fork or can I push to some magical branch on the typelevel repo?

@armanbilge
Copy link
Member

@TimWSpence you can just open a new PR based on this branch.

@armanbilge armanbilge merged commit 4e27585 into typelevel:main Oct 9, 2022
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.

3 participants