<i18n dev> RFR: 8263090: Avoid reading volatile fields twice in Locale.getDefault(Category) [v2]

Claes Redestad redestad at openjdk.java.net
Sat Mar 6 13:37:07 UTC 2021


On Sat, 6 Mar 2021 05:51:17 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> Claes Redestad has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Fix omitted synchronized
>
> src/java.base/share/classes/java/util/Locale.java line 946:
> 
>> 944:             Locale loc = defaultDisplayLocale; // volatile read
>> 945:             if (loc == null) {
>> 946:                 loc = getDisplayLocale();
> 
> Just interesting how did you check that the performance difference is because of volatile read, and not because of replacing of the switch by the "if"?

I started out with this variant, only removing the double volatile reads:

    public static Locale getDefault(Locale.Category category) {
        // do not synchronize this method - see 4071298
        Locale loc;
        switch (category) {
        case DISPLAY:
            loc = defaultDisplayLocale;
            if (loc == null) {
                synchronized(Locale.class) {
                    loc = defaultDisplayLocale;
                    if (loc == null) {
                        loc = defaultDisplayLocale = initDefault(category);
                    }
                }
            }
            return loc;
        case FORMAT:
            loc = defaultFormatLocale;
            if (loc == null) {
                synchronized(Locale.class) {
                    loc = defaultFormatLocale;
                    if (loc == null) {
                        loc = defaultFormatLocale = initDefault(category);
                    }
                }
            }
            return loc;
        default:
            assert false: "Unknown Category";
        }
        return getDefault();
    }

Scores were the same:
Benchmark                         Mode  Cnt   Score   Error  Units
LocaleDefaults.getDefault         avgt    5  10.045 ± 0.032  ns/op
LocaleDefaults.getDefaultDisplay  avgt    5  11.301 ± 0.053  ns/op
LocaleDefaults.getDefaultFormat   avgt    5  11.303 ± 0.054  ns/op

I then refactored and checked that the refactorings were performance neutral.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2845


More information about the i18n-dev mailing list