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

optimization: avoid NotGiven allocations #17090

Merged
merged 1 commit into from
Mar 15, 2023
Merged

Conversation

fwbrasil
Copy link
Contributor

Problem

I've been working on a library that uses NotGiven in the hot path, which ends up producing an extra allocation on every use. I thought JIT compilation would be able to optimize it away but that isn't the case in some benchmarks. For example:

Benchmark: https://github.com/fwbrasil/kyo/blob/main/kyo-bench/src/main/scala/kyo/bench/NarrowBindMapBench.scala
Flamegraph: https://getkyo.io/dev/profile/4990c20/kyo.bench.NarrowBindMapBench.forkKyo-Throughput/flame-cpu-forward.html

Solution

Make NotGiven.value a val instead of def.

Notes

  • I'm assuming the identity of a NotGiven object isn't relevant.
  • Should I use an internal field and keep NotGiven.value as def to avoid binary compatibility issues?

@fwbrasil
Copy link
Contributor Author

I've just signed the CLA

@prolativ prolativ requested a review from smarter March 13, 2023 10:50
@Kordyjan Kordyjan added the needs-minor-release This PR cannot be merged until the next minor release label Mar 13, 2023
@Kordyjan Kordyjan added this to the 3.4.0 milestone Mar 13, 2023
@prolativ prolativ assigned nicolasstucki and unassigned smarter Mar 13, 2023
@prolativ prolativ requested review from nicolasstucki and removed request for smarter March 13, 2023 15:22
@smarter
Copy link
Member

smarter commented Mar 14, 2023

Does it actually need a minor release? This seems to be source/tasty/binary-compatible /cc @sjrd.

@dwijnand
Copy link
Member

We discussed this yesterday. It's not forwards tasty compatible, is the concern.

@smarter
Copy link
Member

smarter commented Mar 14, 2023

Ah, maybe tasty deserialization could be enhanced to make that kind of change actually compatible?

@sjrd
Copy link
Member

sjrd commented Mar 14, 2023

It's fundamentally incompatible to go from a val to a def, because it makes it unstable. There's no "making it compatible". So going the other direction is not forwards compatible.

But you can make it compatible in all directions by storing the instance in a private val, and reading that val in the public def.

@smarter
Copy link
Member

smarter commented Mar 14, 2023

Ah yes, I didn't think of stability, I agree that a def pointing to a private val is the way to go.

@fwbrasil
Copy link
Contributor Author

Thank you for the feedback! I've just updated the PR to use a private value

@smarter smarter removed the needs-minor-release This PR cannot be merged until the next minor release label Mar 15, 2023
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

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

Thanks!

@smarter smarter merged commit 18259db into scala:main Mar 15, 2023
@dwijnand
Copy link
Member

We also discussed going this way on Monday and had decided to just wait for the next minor and make it a val....

@dwijnand
Copy link
Member

For these cases (in this instance it's not a big deal) but also personally, I'd love to see you both at the Monday team meetings.

@sjrd
Copy link
Member

sjrd commented Mar 15, 2023

Yes, I should come more often.

@fwbrasil fwbrasil deleted the cached-not-given branch March 15, 2023 11:32
@Kordyjan Kordyjan modified the milestones: 3.4.0, 3.3.1-RC1 Apr 3, 2023
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.

6 participants