Skip to content

Commit 13625be

Browse files
committed
8263090: Avoid reading volatile fields twice in Locale.getDefault(Category)
Reviewed-by: rriggs, naoto, serb
1 parent 61cff4d commit 13625be

File tree

2 files changed

+87
-20
lines changed

2 files changed

+87
-20
lines changed

src/java.base/share/classes/java/util/Locale.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -939,29 +939,38 @@ public static Locale getDefault() {
939939
*/
940940
public static Locale getDefault(Locale.Category category) {
941941
// do not synchronize this method - see 4071298
942-
switch (category) {
943-
case DISPLAY:
944-
if (defaultDisplayLocale == null) {
945-
synchronized(Locale.class) {
946-
if (defaultDisplayLocale == null) {
947-
defaultDisplayLocale = initDefault(category);
948-
}
949-
}
942+
Objects.requireNonNull(category);
943+
if (category == Category.DISPLAY) {
944+
Locale loc = defaultDisplayLocale; // volatile read
945+
if (loc == null) {
946+
loc = getDisplayLocale();
950947
}
951-
return defaultDisplayLocale;
952-
case FORMAT:
953-
if (defaultFormatLocale == null) {
954-
synchronized(Locale.class) {
955-
if (defaultFormatLocale == null) {
956-
defaultFormatLocale = initDefault(category);
957-
}
958-
}
948+
return loc;
949+
} else {
950+
assert category == Category.FORMAT : "Unknown category";
951+
Locale loc = defaultFormatLocale; // volatile read
952+
if (loc == null) {
953+
loc = getFormatLocale();
959954
}
960-
return defaultFormatLocale;
961-
default:
962-
assert false: "Unknown Category";
955+
return loc;
956+
}
957+
}
958+
959+
private static synchronized Locale getDisplayLocale() {
960+
Locale loc = defaultDisplayLocale;
961+
if (loc == null) {
962+
loc = defaultDisplayLocale = initDefault(Category.DISPLAY);
963+
}
964+
return loc;
965+
}
966+
967+
968+
private static synchronized Locale getFormatLocale() {
969+
Locale loc = defaultFormatLocale;
970+
if (loc == null) {
971+
loc = defaultFormatLocale = initDefault(Category.FORMAT);
963972
}
964-
return getDefault();
973+
return loc;
965974
}
966975

967976
private static Locale initDefault() {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.util;
24+
25+
import org.openjdk.jmh.annotations.Benchmark;
26+
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Mode;
28+
import org.openjdk.jmh.annotations.OutputTimeUnit;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.State;
31+
32+
import java.util.Locale;
33+
import java.util.concurrent.TimeUnit;
34+
35+
/*
36+
* This benchmark tests Locale.getDefault variants
37+
*/
38+
@BenchmarkMode(Mode.AverageTime)
39+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
40+
@State(Scope.Benchmark)
41+
public class LocaleDefaults {
42+
43+
@Benchmark
44+
public Locale getDefault() {
45+
return Locale.getDefault();
46+
}
47+
48+
@Benchmark
49+
public Locale getDefaultDisplay() {
50+
return Locale.getDefault(Locale.Category.DISPLAY);
51+
}
52+
53+
@Benchmark
54+
public Locale getDefaultFormat() {
55+
return Locale.getDefault(Locale.Category.FORMAT);
56+
}
57+
}
58+

0 commit comments

Comments
 (0)