RFR: JDK-8227831: Avoid using volatile for write-once, read-many class field

Kazunori Ogata OGATAK at jp.ibm.com
Wed Jul 17 06:48:44 UTC 2019


Hi,

May I have a review for "JDK-8227831: Avoid using volatile for write-once, 
read-many class field"?

In jdk.internal.reflect.ReflectionFactory, there is a private class field 
named "langReflectAccess", which is referenced every time when the library 
handles various reflective operations.  This field is initialized on the 
first access to the ReflectionFactory class.  This field is declared as 
volatile to avoid (or reduce) race condition between initialization and 
references to the field.

On the platforms with weak memory model (i.e, POWER and ARM), reading a 
volatile variable requires memory fence and incurs overhead.  So it is 
preferable to avoid use of volatile for such a write-once, read-many 
variable.

langReflectAccess can be modified only in setLangReflectAccess() method. 
So we can avoid using volatile by modifying setLangReflectAccess() to use 
a synchronized block to avoid race condition.  This change reduced elapsed 
time of a micro benchmark by 9%, which repeatedly invoke 
Class.getMethods().


Bug: https://bugs.openjdk.java.net/browse/JDK-8227831

Webrev: http://cr.openjdk.java.net/~ogatak/8227831/webrev/


Regards,
Ogata



More information about the core-libs-dev mailing list