-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8356993: ArrayDeque should use Arrays.fill() instead of for() loops #25237
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
Conversation
|
👋 Welcome back acobbs! A progress list of the required criteria for merging this PR into |
|
❗ This change is not yet ready to be integrated. |
|
@archiecobbs The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
|
Are you planning to add some JMH benchmarks to go with this? |
I wasn't planning to, but I'm inferring from your question that you'd prefer to see one. Which also makes me curious. I'd be shocked if this were slower, but even if not, I wonder how much faster it would be. I will work on creating one. |
|
I'm curious to know whether C2 turns the loop into a vectorized operation. The Arrays.fill might be more expressive, but not necessarily faster. |
|
I added a benchmark to the PR (hopefully I did that right). It shows a decrease in performance. I have no idea why. I did this on my laptop so who knows, but if the effect is real then it kind of raises a lot of larger questions. |
| @Benchmark | ||
| @Measurement(iterations = 10) | ||
| @Warmup(iterations = 3) | ||
| public void fillAndClear() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to return the collection or send it to a BlackHole
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to return the collection or send it to a BlackHole
I'm fairly new to the benchmark game so I would not be surprised if this is broken.
Previously I was adding them to a list but that caused OOMs.
Can you clarify what you mean? By 'return' do you just mean returning the deque from the method? Also I don't konw what a BlackHole is.
Apologies for not knowing what I'm doing here. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are many exmaples on how to correctly use JMH.
A blackhole prevents the compiler to optimize away your code.
Thanks for the tip. FWIW after doing that the numbers came out about the same - which is not surprising given that Arrays.fill() is just the same for() loop...
#2 - After adding Blackhole
jdk-25+22-94-g0318e49500e (master):
Benchmark Mode Cnt Score Error Units
ArrayDeque.ClearBenchmarkTestJMH.fillAndClear thrpt 50 35.663 ± 0.163 ops/s
jdk-25+22-97-g9f0c5fe1f90 (JDK-8356993):
Benchmark Mode Cnt Score Error Units
ArrayDeque.ClearBenchmarkTestJMH.fillAndClear thrpt 50 35.112 ± 0.501 ops/s
|
Note that jdk/src/java.base/share/classes/java/util/Arrays.java Lines 3449 to 3453 in c59debb
|
Interesting... I was assuming that most of the "bulk" methods in If C2 is already able to automatically optimize this into the maximum possible hardware performance, then great! But is that actually the case? |
|
I'm closing the PR because it's gone into low-level optimization details that are beyond me. However I'm still unclear on whether bulk memory set operations are being fully optimized. By that I mean doing something like this on arm64 at least. Any insights from the experts would be appreciated. Thanks for the interesting discussion. |
Please review this small performance tweak
ArrayDeque.ArrayDequehas an invariant in which any unused elements in the array must be null. In a couple of places, the code is setting contiguous ranges of elements to null usingfor()loops. This can be both simplified and sped up by usingArrays.fill()instead.Progress
Issue
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25237/head:pull/25237$ git checkout pull/25237Update a local copy of the PR:
$ git checkout pull/25237$ git pull https://git.openjdk.org/jdk.git pull/25237/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 25237View PR using the GUI difftool:
$ git pr show -t 25237Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25237.diff
Using Webrev
Link to Webrev Comment