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

Optimize Function0 allocations #2366

Merged
merged 2 commits into from
Aug 19, 2024
Merged

Optimize Function0 allocations #2366

merged 2 commits into from
Aug 19, 2024

Conversation

kyri-petrou
Copy link
Collaborator

This PR optimizes (mostly) Function0 allocations that originate from map.getOrElse(key, ???). In Scala 2, the optimization is fairly straight-forward; we just need to store the default value in a Function0. Since => A compiles to () => A, this means that invocations of the getOrElse(key, DefaultValueFn()) don't create a Function0.

In Scala 3 however, it's slightly more complex. For some reason (might be a bug, I'll open an issue in Dotty about it), this works only if the val is annotated with @static, which compiles to a final static field. To avoid having to do this everywhere, I added a package-private syntax object with the getOrElseNull method.

I've also noticed some small optimizations that could be done here and there so I did them as part of this PR

@kyri-petrou kyri-petrou merged commit c0bc120 into series/2.x Aug 19, 2024
10 checks passed
@kyri-petrou kyri-petrou deleted the optimize-allocations branch August 19, 2024 08:57
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.

2 participants