<i18n dev> RFR: 8345668: ZoneOffset.ofTotalSeconds performance regression

Roger Riggs rriggs at openjdk.org
Fri Dec 20 20:29:34 UTC 2024


On Fri, 20 Dec 2024 19:55:06 GMT, Naoto Sato <naoto at openjdk.org> wrote:

> The change made in [JDK-8288723](https://bugs.openjdk.org/browse/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 to `ZoneOffset.ofTotalSeconds(0)` 1,000 times, which improves the operation time from 3,946ns to 2,241ns.

src/java.base/share/classes/java/time/ZoneOffset.java line 432:

> 430:                 result = new ZoneOffset(totalSeconds);
> 431:                 SECONDS_CACHE.putIfAbsent(totalSecs, result);
> 432:                 result = SECONDS_CACHE.get(totalSecs);

putIfAbsent() returns the existing value (if any), the 2nd get can be avoided by using the non-null value if returned or returning the result just created.
Suggestion:

                var existing = SECONDS_CACHE.putIfAbsent(totalSecs, result);
                return (existing != null) ? existing : result;

It should perform better than doing the `get` again.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/22854#discussion_r1894362437


More information about the i18n-dev mailing list