8288723: Avoid redundant ConcurrentHashMap.get call in java.time

Reviewed-by: attila, rriggs
This commit is contained in:
Andrey Turbanov 2022-07-21 10:22:58 +00:00
parent 3582fd9e93
commit 52cc6cd063
4 changed files with 14 additions and 28 deletions

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -424,15 +424,11 @@ public final class ZoneOffset
throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00"); throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00");
} }
if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) { if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) {
Integer totalSecs = totalSeconds; return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> {
ZoneOffset result = SECONDS_CACHE.get(totalSecs); ZoneOffset result = new ZoneOffset(totalSecs);
if (result == null) {
result = new ZoneOffset(totalSeconds);
SECONDS_CACHE.putIfAbsent(totalSecs, result);
result = SECONDS_CACHE.get(totalSecs);
ID_CACHE.putIfAbsent(result.getId(), result); ID_CACHE.putIfAbsent(result.getId(), result);
}
return result; return result;
});
} else { } else {
return new ZoneOffset(totalSeconds); return new ZoneOffset(totalSeconds);
} }

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -309,13 +309,7 @@ class DateTimeTextProvider {
private Object findStore(TemporalField field, Locale locale) { private Object findStore(TemporalField field, Locale locale) {
Entry<TemporalField, Locale> key = createEntry(field, locale); Entry<TemporalField, Locale> key = createEntry(field, locale);
Object store = CACHE.get(key); return CACHE.computeIfAbsent(key, e -> createStore(e.getKey(), e.getValue()));
if (store == null) {
store = createStore(field, locale);
CACHE.putIfAbsent(key, store);
store = CACHE.get(key);
}
return store;
} }
private static int toWeekDay(int calWeekDay) { private static int toWeekDay(int calWeekDay) {

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -160,13 +160,7 @@ public final class DecimalStyle {
*/ */
public static DecimalStyle of(Locale locale) { public static DecimalStyle of(Locale locale) {
Objects.requireNonNull(locale, "locale"); Objects.requireNonNull(locale, "locale");
DecimalStyle info = CACHE.get(locale); return CACHE.computeIfAbsent(locale, DecimalStyle::create);
if (info == null) {
info = create(locale);
CACHE.putIfAbsent(locale, info);
info = CACHE.get(locale);
}
return info;
} }
private static DecimalStyle create(Locale locale) { private static DecimalStyle create(Locale locale) {

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -330,8 +330,10 @@ public final class WeekFields implements Serializable {
WeekFields rules = CACHE.get(key); WeekFields rules = CACHE.get(key);
if (rules == null) { if (rules == null) {
rules = new WeekFields(firstDayOfWeek, minimalDaysInFirstWeek); rules = new WeekFields(firstDayOfWeek, minimalDaysInFirstWeek);
CACHE.putIfAbsent(key, rules); WeekFields prev = CACHE.putIfAbsent(key, rules);
rules = CACHE.get(key); if (prev != null) {
rules = prev;
}
} }
return rules; return rules;
} }