From 52cc6cd063c167229f8883ba5a81be306c38a73c Mon Sep 17 00:00:00 2001 From: Andrey Turbanov Date: Thu, 21 Jul 2022 10:22:58 +0000 Subject: [PATCH] 8288723: Avoid redundant ConcurrentHashMap.get call in java.time Reviewed-by: attila, rriggs --- .../share/classes/java/time/ZoneOffset.java | 14 +++++--------- .../java/time/format/DateTimeTextProvider.java | 10 ++-------- .../classes/java/time/format/DecimalStyle.java | 10 ++-------- .../classes/java/time/temporal/WeekFields.java | 8 +++++--- 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/java.base/share/classes/java/time/ZoneOffset.java b/src/java.base/share/classes/java/time/ZoneOffset.java index 9dd647894095e..7cb4ce2d627a4 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, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022, 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,15 +424,11 @@ 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) { - 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); + return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> { + ZoneOffset result = new ZoneOffset(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 f9db0a10f65ea..5ee4c5c23c816 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, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022, 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,13 +309,7 @@ public Iterator> getTextIterator(Chronology chrono, Temporal private Object findStore(TemporalField field, Locale locale) { Entry key = createEntry(field, locale); - Object store = CACHE.get(key); - if (store == null) { - store = createStore(field, locale); - CACHE.putIfAbsent(key, store); - store = CACHE.get(key); - } - return store; + return CACHE.computeIfAbsent(key, e -> createStore(e.getKey(), e.getValue())); } 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 9440847c7c32d..b2513888790ee 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, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022, 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,13 +160,7 @@ public static DecimalStyle ofDefaultLocale() { */ public static DecimalStyle of(Locale locale) { Objects.requireNonNull(locale, "locale"); - DecimalStyle info = CACHE.get(locale); - if (info == null) { - info = create(locale); - CACHE.putIfAbsent(locale, info); - info = CACHE.get(locale); - } - return info; + return CACHE.computeIfAbsent(locale, DecimalStyle::create); } private static DecimalStyle create(Locale locale) { diff --git a/src/java.base/share/classes/java/time/temporal/WeekFields.java b/src/java.base/share/classes/java/time/temporal/WeekFields.java index 47d6bbd2f7a51..3c8d4a8bb387f 100644 --- a/src/java.base/share/classes/java/time/temporal/WeekFields.java +++ b/src/java.base/share/classes/java/time/temporal/WeekFields.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022, 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 @@ -330,8 +330,10 @@ public static WeekFields of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek WeekFields rules = CACHE.get(key); if (rules == null) { rules = new WeekFields(firstDayOfWeek, minimalDaysInFirstWeek); - CACHE.putIfAbsent(key, rules); - rules = CACHE.get(key); + WeekFields prev = CACHE.putIfAbsent(key, rules); + if (prev != null) { + rules = prev; + } } return rules; }