-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
8345668: ZoneOffset.ofTotalSeconds performance regression #22854
base: master
Are you sure you want to change the base?
8345668: ZoneOffset.ofTotalSeconds performance regression #22854
Conversation
👋 Welcome back naoto! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
Webrevs
|
Co-authored-by: Roger Riggs <[email protected]>
return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> { | ||
ZoneOffset result = new ZoneOffset(totalSecs); | ||
Integer totalSecs = totalSeconds; | ||
ZoneOffset result = SECONDS_CACHE.get(totalSecs); |
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, each call may allocate an Integer object. The maximum number of ZoneOffsets that need to be cached here is only 148. Using AtomicReferenceArray is better than AtomicConcurrentHashMap.
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.
For example:
static final AtomicReferenceArray<ZoneOffset> MINUTES_15_CACHE = new AtomicReferenceArray<>(37 * 4);
public static ZoneOffset ofTotalSeconds(int totalSeconds) {
// ...
int minutes15Rem = totalSeconds / (15 * SECONDS_PER_MINUTE);
if (totalSeconds - minutes15Rem * 15 * SECONDS_PER_MINUTE == 0) {
int cacheIndex = minutes15Rem + 18 * 4;
ZoneOffset result = MINUTES_15_CACHE.get(cacheIndex);
if (result == null) {
result = new ZoneOffset(totalSeconds);
if (!MINUTES_15_CACHE.compareAndSet(cacheIndex, null, result)) {
result = MINUTES_15_CACHE.get(minutes15Rem);
}
}
return result;
}
// ...
}
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.
Hi Shaojin,
Thanks for the suggestion, but I am not planning to improve the code more than backing out the offending fix at this time. (btw, cache size would be 149 as 18:00 and -18:00 are inclusive)
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.
Can I submit a PR to make this improvement?
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.
@wenshao I agree with your proposal. Also for this part:
ZoneOffset result = MINUTES_15_CACHE.get(cacheIndex);
if (result == null) {
result = new ZoneOffset(totalSeconds);
if (!MINUTES_15_CACHE.compareAndSet(cacheIndex, null, result)) {
result = MINUTES_15_CACHE.get(minutes15Rem);
}
}
I recommend a rewrite:
ZoneOffset result = MINUTES_15_CACHE.getPlain(cacheIndex);
if (result == null) {
result = new ZoneOffset(totalSeconds);
ZoneOffset existing = MINUTES_15_CACHE.compareAndExchange(cacheIndex, null, result);
return existing == null ? result : existing;
}
The getPlain
is safe because ZoneOffset
is thread safe, so you can use the object when you can observe a ZoneOffset
object reference. Also compareAndExchange
avoids extra operations if we failed to racily set the computed ZoneOffset
.
@Benchmark | ||
public void ofTotalSeconds() { | ||
for (int i = 0; i < 1_000; i++) { | ||
ZoneOffset.ofTotalSeconds(0); |
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.
This benchmark method should accept a Blackhole
, and the return value of ofTotalSeconds
must be sent to the Blackhole.consume
method.
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.
This benchmark currently works probably because the cache interactions in ofTotalSeconds
, which means JIT compilation cannot prove it is side-effect free. Had it been as simple as a decimal computation or if the cache becomes a stable map, JIT compilation can eliminate the static factory method call entirely, and the benchmark would be measuring the performance of no-op invocation.
The |
The change made in JDK-8288723 seems innocuous, but it caused this performance regression. Partially reverting the change (ones that involve
computeIfAbsent()
) to the original. Provided a benchmark that iterates the call toZoneOffset.ofTotalSeconds(0)
1,000 times, which improves the operation time from 3,946ns to 2,241ns.Progress
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/22854/head:pull/22854
$ git checkout pull/22854
Update a local copy of the PR:
$ git checkout pull/22854
$ git pull https://git.openjdk.org/jdk.git pull/22854/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 22854
View PR using the GUI difftool:
$ git pr show -t 22854
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/22854.diff
Using Webrev
Link to Webrev Comment