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

[lworld] Useless checks in generated VarHandle code for flat fields

XMLWordPrintable

      Access to flat fields through VarHandles is performed with the code generated in VarHandleValues.java. In this generated code, there's a pattern of testing the value returned by Unsafe.getValue() against null to substitute null with the all-zero default value.

      Exemple:

              @ForceInline
              static Object get(VarHandle ob) {
                  FieldStaticReadOnly handle = (FieldStaticReadOnly) ob.target();
                  Object value = UNSAFE.getValue(handle.base,
                                       handle.fieldOffset, handle.fieldType);
                  if (value == null && handle.checkedFieldType instanceof NullRestrictedCheckedType) {
                      return ValueClass.zeroInstance(handle.fieldType);
                  }
                  return value;
              }

       ~~While this is a required practice when accessing reference fields (which include non-flat value fields), this check is useless for flat value fields. Unsafe.getValue() will never return null for a null-free flat field.~~

      Note: Now with verifier-enforced null restriction, this check is useless for any read access.

            liach Chen Liang
            fparain Frederic Parain
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: