<i18n dev> RFR: 8287860: Revise usage of volatile in j.u.Locale

liach duke at openjdk.java.net
Mon Jun 6 13:36:53 UTC 2022


On Mon, 6 Jun 2022 13:28:44 GMT, Сергей Цыпанов <duke at openjdk.java.net> wrote:

>> src/java.base/share/classes/java/util/Locale.java line 2260:
>> 
>>> 2258:      * Calculated hashcode
>>> 2259:      */
>>> 2260:     private transient volatile int hashCodeValue;
>> 
>> We can additionally annotate such cache fields with `@Stable` so as to enable constant-folding optimizations from the hotspot JIT.
>
> Shouldn't the fields annotated with `@Stable` be `final` as well?

These fields can only be written once besides the default values, but they don't have to be written in the static initializer or constructor. So when a non-zero hash code is written, it's cached as if it's a final field. For a zero value, it would always undergo extra calculations like before and write multiple times, but the writes don't matter as it would only write zero, which would not yield false hash code if a previously written 0 value is cached.

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

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


More information about the i18n-dev mailing list