Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8227831

Avoid using volatile for write-once, read-many class field

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      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().

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              ogatak Kazunori Ogata
              Reporter:
              ogatak Kazunori Ogata
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: