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

Atomic*FieldUpdaters final fields should be trusted

XMLWordPrintable

    • b92

        The usual pattern for using Atomic*FieldUpdaters is as follows:

        public class Data {
            volatile int dataInt;
            static final AtomicIntegerFieldUpdater<Data> UPDATER_INT
                    = AtomicIntegerFieldUpdater.newUpdater(Data.class, "dataInt");

            int m() {
                return UPDATER_INT.get(this);
            }
        }

        This code will lose against the plain field access and Unsafe access, because updaters do the extensive checks before doing the actual access:

                private final Class<T> tclass;
                private final Class<?> cclass;

                public final int get(T obj) {
                    if (obj == null || obj.getClass() != tclass || cclass != null) fullCheck(obj);
                    return unsafe.getIntVolatile(obj, offset);
                }

        Fortunately, since the use pattern makes the updater a (static final) constant, we can make VM to trust the final instance fields in updater. Then, updater checks would be folded in as constants, closing the gap in updater performance. This is a special case of more general approach (see JDK-8058164), but it seems doable before a more generic solution arrives. This caters for lots of existing users.

        Proof-of-concept change:
         http://cr.openjdk.java.net/~shade/8140483/webrev.00/

        Benchmarks:
         http://cr.openjdk.java.net/~shade/8140483/AFUBench.java
         http://cr.openjdk.java.net/~shade/8140483/benchmarks.jar

        Quick runs show the entire slab of tests is folded into the typecheck + implicit NP check, with good performance improvements:
         http://cr.openjdk.java.net/~shade/8140483/notes.txt

              shade Aleksey Shipilev
              shade Aleksey Shipilev
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: