chore: optimize lazy val with power of two. - #22428
Conversation
| private val base: Int = { | ||
| val processors = java.lang.Runtime.getRuntime.nn.availableProcessors() | ||
| 8 * processors * processors | ||
| val rawSize = 8 * processors * processors |
There was a problem hiding this comment.
not sure why it's 8 here, Some CPU has 384 cores now, maybe 2 * 2 is better than 8.
The current implementation may use a little more memory than it was, but as many server just has 64 cores, which is the same.
There was a problem hiding this comment.
I see from blame that it is here for some time. But availableProcessors gives the number of logical cores, so no relation to CPU count, just the actual cores. And I don't think this code is actually used in the new lazy vals impl?
|
|
||
| if (id < 0) id += base | ||
| monitors(id) | ||
| monitors((java.lang.System.identityHashCode(obj) + fieldId) & mask) |
|
@szymon-rd friendly ping, would you like to take a look at this, thanks |
|
I believe the new lazy vals don't use those monitors, the implementation I did worked on a CountDownLatch per val. Correct me if I am wrong though. |
2c50caf to
19faf61
Compare
|
Needs a rebase to get the new CI setup. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes the lazy val implementation by using bitwise AND instead of modulo for monitor selection, improving performance at the cost of slightly increased memory usage.
Changes:
- Modified
basecalculation to round up to the next power of 2 - Introduced
maskvariable for bitwise AND operations - Replaced modulo operation with bitwise AND in
getMonitor
ðĄ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Motivation: Use `&` instead of `%`, which is faster. [Cherry-picked 965809d][modified]

Motivation:
Use
&instead of%, which is faster.