<i18n dev> RFR: 8345668: ZoneOffset.ofTotalSeconds performance regression [v3]
Shaojin Wen
swen at openjdk.org
Fri Dec 20 23:10:53 UTC 2024
On Fri, 20 Dec 2024 23:05:06 GMT, Shaojin Wen <swen at openjdk.org> wrote:
>> Naoto Sato has updated the pull request incrementally with one additional commit since the last revision:
>>
>> Fixed compile error
>
> src/java.base/share/classes/java/time/ZoneOffset.java line 428:
>
>> 426: if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) {
>> 427: Integer totalSecs = totalSeconds;
>> 428: ZoneOffset result = SECONDS_CACHE.get(totalSecs);
>
> 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.
For example:
static final class Cache {
static final AtomicReferenceArray<ZoneOffset> MINUTES_15_CACHE = new AtomicReferenceArray<>(18 * 2 * 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;
ZoneOffset result = Cache.MINUTES_15_CACHE.get(cacheIndex);
if (result == null) {
result = new ZoneOffset(totalSeconds);
if (!Cache.MINUTES_15_CACHE.compareAndSet(cacheIndex, null, result)) {
result = Cache.MINUTES_15_CACHE.get(minutes15Rem);
}
}
return result;
}
// ...
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/22854#discussion_r1894488013
More information about the i18n-dev
mailing list