Skip to content

Commit 9a60f44

Browse files
committed
8345668: ZoneOffset.ofTotalSeconds performance regression
Reviewed-by: rriggs, aturbanov
1 parent 12700cb commit 9a60f44

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/java.base/share/classes/java/time/ZoneOffset.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -424,11 +424,17 @@ public static ZoneOffset ofTotalSeconds(int totalSeconds) {
424424
throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00");
425425
}
426426
if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) {
427-
return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> {
428-
ZoneOffset result = new ZoneOffset(totalSecs);
427+
Integer totalSecs = totalSeconds;
428+
ZoneOffset result = SECONDS_CACHE.get(totalSecs);
429+
if (result == null) {
430+
result = new ZoneOffset(totalSeconds);
431+
var existing = SECONDS_CACHE.putIfAbsent(totalSecs, result);
432+
if (existing != null) {
433+
result = existing;
434+
}
429435
ID_CACHE.putIfAbsent(result.getId(), result);
430-
return result;
431-
});
436+
}
437+
return result;
432438
} else {
433439
return new ZoneOffset(totalSeconds);
434440
}

src/java.base/share/classes/java/time/format/DateTimeTextProvider.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -309,7 +309,15 @@ public Iterator<Entry<String, Long>> getTextIterator(Chronology chrono, Temporal
309309

310310
private Object findStore(TemporalField field, Locale locale) {
311311
Entry<TemporalField, Locale> key = createEntry(field, locale);
312-
return CACHE.computeIfAbsent(key, e -> createStore(e.getKey(), e.getValue()));
312+
Object store = CACHE.get(key);
313+
if (store == null) {
314+
store = createStore(field, locale);
315+
var existing = CACHE.putIfAbsent(key, store);
316+
if (existing != null) {
317+
store = existing;
318+
}
319+
}
320+
return store;
313321
}
314322

315323
private static int toWeekDay(int calWeekDay) {

src/java.base/share/classes/java/time/format/DecimalStyle.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -160,7 +160,15 @@ public static DecimalStyle ofDefaultLocale() {
160160
*/
161161
public static DecimalStyle of(Locale locale) {
162162
Objects.requireNonNull(locale, "locale");
163-
return CACHE.computeIfAbsent(locale, DecimalStyle::create);
163+
DecimalStyle info = CACHE.get(locale);
164+
if (info == null) {
165+
info = create(locale);
166+
var existing = CACHE.putIfAbsent(locale, info);
167+
if (existing != null) {
168+
info = existing;
169+
}
170+
}
171+
return info;
164172
}
165173

166174
private static DecimalStyle create(Locale locale) {

0 commit comments

Comments
 (0)