From 3df7e9f14c39b2579f4e667c85a9ac981dbc2147 Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Fri, 20 Dec 2024 11:11:32 -0800 Subject: [PATCH 1/3] initial commit --- .../share/classes/java/time/ZoneOffset.java | 14 +++-- .../time/format/DateTimeTextProvider.java | 10 +++- .../java/time/format/DecimalStyle.java | 10 +++- .../bench/java/time/ZoneOffsetBench.java | 53 +++++++++++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 test/micro/org/openjdk/bench/java/time/ZoneOffsetBench.java diff --git a/src/java.base/share/classes/java/time/ZoneOffset.java b/src/java.base/share/classes/java/time/ZoneOffset.java index 14ac5fcfb6ba1..69bd8d6c1d03c 100644 --- a/src/java.base/share/classes/java/time/ZoneOffset.java +++ b/src/java.base/share/classes/java/time/ZoneOffset.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -424,11 +424,15 @@ public static ZoneOffset ofTotalSeconds(int totalSeconds) { throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00"); } if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) { - return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> { - ZoneOffset result = new ZoneOffset(totalSecs); + Integer totalSecs = totalSeconds; + ZoneOffset result = SECONDS_CACHE.get(totalSecs); + if (result == null) { + result = new ZoneOffset(totalSeconds); + SECONDS_CACHE.putIfAbsent(totalSecs, result); + result = SECONDS_CACHE.get(totalSecs); ID_CACHE.putIfAbsent(result.getId(), result); - return result; - }); + } + return result; } else { return new ZoneOffset(totalSeconds); } diff --git a/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java b/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java index 5ee4c5c23c816..70ad69b03b89c 100644 --- a/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java +++ b/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -309,7 +309,13 @@ public Iterator> getTextIterator(Chronology chrono, Temporal private Object findStore(TemporalField field, Locale locale) { Entry key = createEntry(field, locale); - return CACHE.computeIfAbsent(key, e -> createStore(e.getKey(), e.getValue())); + Object store = CACHE.get(key); + if (store == null) { + store = createStore(field, locale); + CACHE.putIfAbsent(key, store); + store = CACHE.get(key); + } + return store; } private static int toWeekDay(int calWeekDay) { diff --git a/src/java.base/share/classes/java/time/format/DecimalStyle.java b/src/java.base/share/classes/java/time/format/DecimalStyle.java index b2513888790ee..98dd52ba021be 100644 --- a/src/java.base/share/classes/java/time/format/DecimalStyle.java +++ b/src/java.base/share/classes/java/time/format/DecimalStyle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -160,7 +160,13 @@ public static DecimalStyle ofDefaultLocale() { */ public static DecimalStyle of(Locale locale) { Objects.requireNonNull(locale, "locale"); - return CACHE.computeIfAbsent(locale, DecimalStyle::create); + DecimalStyle info = CACHE.get(locale); + if (info == null) { + info = create(locale); + CACHE.putIfAbsent(locale, info); + info = CACHE.get(locale); + } + return info; } private static DecimalStyle create(Locale locale) { diff --git a/test/micro/org/openjdk/bench/java/time/ZoneOffsetBench.java b/test/micro/org/openjdk/bench/java/time/ZoneOffsetBench.java new file mode 100644 index 0000000000000..247584093a16d --- /dev/null +++ b/test/micro/org/openjdk/bench/java/time/ZoneOffsetBench.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.bench.java.time; + +import java.time.ZoneOffset; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Benchmark) +@Fork(1) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 2) +@Measurement(iterations = 5) +public class ZoneOffsetBench { + + @Benchmark + public void ofTotalSeconds() { + for (int i = 0; i < 1_000; i++) { + ZoneOffset.ofTotalSeconds(0); + } + } +} + From 1e09b8be9748b23484209f90f1914e4d7139c934 Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Fri, 20 Dec 2024 12:54:31 -0800 Subject: [PATCH 2/3] Update src/java.base/share/classes/java/time/ZoneOffset.java Co-authored-by: Roger Riggs --- src/java.base/share/classes/java/time/ZoneOffset.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/java/time/ZoneOffset.java b/src/java.base/share/classes/java/time/ZoneOffset.java index 69bd8d6c1d03c..b35979133103a 100644 --- a/src/java.base/share/classes/java/time/ZoneOffset.java +++ b/src/java.base/share/classes/java/time/ZoneOffset.java @@ -428,8 +428,8 @@ public static ZoneOffset ofTotalSeconds(int totalSeconds) { ZoneOffset result = SECONDS_CACHE.get(totalSecs); if (result == null) { result = new ZoneOffset(totalSeconds); - SECONDS_CACHE.putIfAbsent(totalSecs, result); - result = SECONDS_CACHE.get(totalSecs); + var existing = SECONDS_CACHE.putIfAbsent(totalSecs, result); + return (existing != null) ? existing : result; ID_CACHE.putIfAbsent(result.getId(), result); } return result; From 8dca103a506408fad7c147b432a0c757c43ecc8b Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Fri, 20 Dec 2024 13:02:00 -0800 Subject: [PATCH 3/3] Fixed compile error --- src/java.base/share/classes/java/time/ZoneOffset.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/java.base/share/classes/java/time/ZoneOffset.java b/src/java.base/share/classes/java/time/ZoneOffset.java index b35979133103a..520a0e0b9a194 100644 --- a/src/java.base/share/classes/java/time/ZoneOffset.java +++ b/src/java.base/share/classes/java/time/ZoneOffset.java @@ -429,7 +429,9 @@ public static ZoneOffset ofTotalSeconds(int totalSeconds) { if (result == null) { result = new ZoneOffset(totalSeconds); var existing = SECONDS_CACHE.putIfAbsent(totalSecs, result); - return (existing != null) ? existing : result; + if (existing != null) { + result = existing; + } ID_CACHE.putIfAbsent(result.getId(), result); } return result;