Skip to content

Commit

Permalink
[PATCH] Avoid redundant ConcurrentHashMap.get call in java.time
Browse files Browse the repository at this point in the history
Instead of separate ConcurrentHashMap.get call, we can use result of previous putIfAbsent call.
  • Loading branch information
turbanoff committed Jun 18, 2022
1 parent 47b8669 commit 73a2f6c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/java.base/share/classes/java/time/ZoneOffset.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -428,8 +428,10 @@ 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);
ZoneOffset prev = SECONDS_CACHE.putIfAbsent(totalSecs, result);
if (prev != null) {
result = prev;
}
ID_CACHE.putIfAbsent(result.getId(), result);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -312,8 +312,10 @@ private Object findStore(TemporalField field, Locale locale) {
Object store = CACHE.get(key);
if (store == null) {
store = createStore(field, locale);
CACHE.putIfAbsent(key, store);
store = CACHE.get(key);
Object prev = CACHE.putIfAbsent(key, store);
if (prev != null) {
store = prev;
}
}
return store;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -163,8 +163,10 @@ public static DecimalStyle of(Locale locale) {
DecimalStyle info = CACHE.get(locale);
if (info == null) {
info = create(locale);
CACHE.putIfAbsent(locale, info);
info = CACHE.get(locale);
DecimalStyle prev = CACHE.putIfAbsent(locale, info);
if (prev != null) {
info = prev;
}
}
return info;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 73a2f6c

Please sign in to comment.