rt/emul/compact/src/main/java/java/lang/ClassValue.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Aug 2014 05:55:55 +0200
branchjdk8-b132
changeset 1649 98bdfed1a6e9
child 1653 bd151459ee4f
permissions -rw-r--r--
New exceptions as of JDK8-b132 needed for invoke dynamic
     1 /*
     2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import java.lang.ClassValue.ClassValueMap;
    29 import java.util.WeakHashMap;
    30 import java.lang.ref.WeakReference;
    31 import java.util.concurrent.atomic.AtomicInteger;
    32 
    33 import static java.lang.ClassValue.ClassValueMap.probeHomeLocation;
    34 import static java.lang.ClassValue.ClassValueMap.probeBackupLocations;
    35 
    36 /**
    37  * Lazily associate a computed value with (potentially) every type.
    38  * For example, if a dynamic language needs to construct a message dispatch
    39  * table for each class encountered at a message send call site,
    40  * it can use a {@code ClassValue} to cache information needed to
    41  * perform the message send quickly, for each class encountered.
    42  * @author John Rose, JSR 292 EG
    43  * @since 1.7
    44  */
    45 public abstract class ClassValue<T> {
    46     /**
    47      * Sole constructor.  (For invocation by subclass constructors, typically
    48      * implicit.)
    49      */
    50     protected ClassValue() {
    51     }
    52 
    53     /**
    54      * Computes the given class's derived value for this {@code ClassValue}.
    55      * <p>
    56      * This method will be invoked within the first thread that accesses
    57      * the value with the {@link #get get} method.
    58      * <p>
    59      * Normally, this method is invoked at most once per class,
    60      * but it may be invoked again if there has been a call to
    61      * {@link #remove remove}.
    62      * <p>
    63      * If this method throws an exception, the corresponding call to {@code get}
    64      * will terminate abnormally with that exception, and no class value will be recorded.
    65      *
    66      * @param type the type whose class value must be computed
    67      * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface
    68      * @see #get
    69      * @see #remove
    70      */
    71     protected abstract T computeValue(Class<?> type);
    72 
    73     /**
    74      * Returns the value for the given class.
    75      * If no value has yet been computed, it is obtained by
    76      * an invocation of the {@link #computeValue computeValue} method.
    77      * <p>
    78      * The actual installation of the value on the class
    79      * is performed atomically.
    80      * At that point, if several racing threads have
    81      * computed values, one is chosen, and returned to
    82      * all the racing threads.
    83      * <p>
    84      * The {@code type} parameter is typically a class, but it may be any type,
    85      * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
    86      * <p>
    87      * In the absence of {@code remove} calls, a class value has a simple
    88      * state diagram:  uninitialized and initialized.
    89      * When {@code remove} calls are made,
    90      * the rules for value observation are more complex.
    91      * See the documentation for {@link #remove remove} for more information.
    92      *
    93      * @param type the type whose class value must be computed or retrieved
    94      * @return the current value associated with this {@code ClassValue}, for the given class or interface
    95      * @throws NullPointerException if the argument is null
    96      * @see #remove
    97      * @see #computeValue
    98      */
    99     public T get(Class<?> type) {
   100         // non-racing this.hashCodeForCache : final int
   101         Entry<?>[] cache;
   102         Entry<T> e = probeHomeLocation(cache = getCacheCarefully(type), this);
   103         // racing e : current value <=> stale value from current cache or from stale cache
   104         // invariant:  e is null or an Entry with readable Entry.version and Entry.value
   105         if (match(e))
   106             // invariant:  No false positive matches.  False negatives are OK if rare.
   107             // The key fact that makes this work: if this.version == e.version,
   108             // then this thread has a right to observe (final) e.value.
   109             return e.value();
   110         // The fast path can fail for any of these reasons:
   111         // 1. no entry has been computed yet
   112         // 2. hash code collision (before or after reduction mod cache.length)
   113         // 3. an entry has been removed (either on this type or another)
   114         // 4. the GC has somehow managed to delete e.version and clear the reference
   115         return getFromBackup(cache, type);
   116     }
   117 
   118     /**
   119      * Removes the associated value for the given class.
   120      * If this value is subsequently {@linkplain #get read} for the same class,
   121      * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
   122      * This may result in an additional invocation of the
   123      * {@code computeValue} method for the given class.
   124      * <p>
   125      * In order to explain the interaction between {@code get} and {@code remove} calls,
   126      * we must model the state transitions of a class value to take into account
   127      * the alternation between uninitialized and initialized states.
   128      * To do this, number these states sequentially from zero, and note that
   129      * uninitialized (or removed) states are numbered with even numbers,
   130      * while initialized (or re-initialized) states have odd numbers.
   131      * <p>
   132      * When a thread {@code T} removes a class value in state {@code 2N},
   133      * nothing happens, since the class value is already uninitialized.
   134      * Otherwise, the state is advanced atomically to {@code 2N+1}.
   135      * <p>
   136      * When a thread {@code T} queries a class value in state {@code 2N},
   137      * the thread first attempts to initialize the class value to state {@code 2N+1}
   138      * by invoking {@code computeValue} and installing the resulting value.
   139      * <p>
   140      * When {@code T} attempts to install the newly computed value,
   141      * if the state is still at {@code 2N}, the class value will be initialized
   142      * with the computed value, advancing it to state {@code 2N+1}.
   143      * <p>
   144      * Otherwise, whether the new state is even or odd,
   145      * {@code T} will discard the newly computed value
   146      * and retry the {@code get} operation.
   147      * <p>
   148      * Discarding and retrying is an important proviso,
   149      * since otherwise {@code T} could potentially install
   150      * a disastrously stale value.  For example:
   151      * <ul>
   152      * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
   153      * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
   154      * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
   155      * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
   156      * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
   157      * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
   158      * <li> the previous actions of {@code T2} are repeated several times
   159      * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
   160      * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
   161      * </ul>
   162      * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
   163      * observe the time-dependent states as it computes {@code V1}, etc.
   164      * This does not remove the threat of a stale value, since there is a window of time
   165      * between the return of {@code computeValue} in {@code T} and the installation
   166      * of the the new value.  No user synchronization is possible during this time.
   167      *
   168      * @param type the type whose class value must be removed
   169      * @throws NullPointerException if the argument is null
   170      */
   171     public void remove(Class<?> type) {
   172         ClassValueMap map = getMap(type);
   173         map.removeEntry(this);
   174     }
   175 
   176     // Possible functionality for JSR 292 MR 1
   177     /*public*/ void put(Class<?> type, T value) {
   178         ClassValueMap map = getMap(type);
   179         map.changeEntry(this, value);
   180     }
   181 
   182     /// --------
   183     /// Implementation...
   184     /// --------
   185 
   186     /** Return the cache, if it exists, else a dummy empty cache. */
   187     private static Entry<?>[] getCacheCarefully(Class<?> type) {
   188         // racing type.classValueMap{.cacheArray} : null => new Entry[X] <=> new Entry[Y]
   189         ClassValueMap map = type.classValueMap;
   190         if (map == null)  return EMPTY_CACHE;
   191         Entry<?>[] cache = map.getCache();
   192         return cache;
   193         // invariant:  returned value is safe to dereference and check for an Entry
   194     }
   195 
   196     /** Initial, one-element, empty cache used by all Class instances.  Must never be filled. */
   197     private static final Entry<?>[] EMPTY_CACHE = { null };
   198 
   199     /**
   200      * Slow tail of ClassValue.get to retry at nearby locations in the cache,
   201      * or take a slow lock and check the hash table.
   202      * Called only if the first probe was empty or a collision.
   203      * This is a separate method, so compilers can process it independently.
   204      */
   205     private T getFromBackup(Entry<?>[] cache, Class<?> type) {
   206         Entry<T> e = probeBackupLocations(cache, this);
   207         if (e != null)
   208             return e.value();
   209         return getFromHashMap(type);
   210     }
   211 
   212     // Hack to suppress warnings on the (T) cast, which is a no-op.
   213     @SuppressWarnings("unchecked")
   214     Entry<T> castEntry(Entry<?> e) { return (Entry<T>) e; }
   215 
   216     /** Called when the fast path of get fails, and cache reprobe also fails.
   217      */
   218     private T getFromHashMap(Class<?> type) {
   219         // The fail-safe recovery is to fall back to the underlying classValueMap.
   220         ClassValueMap map = getMap(type);
   221         for (;;) {
   222             Entry<T> e = map.startEntry(this);
   223             if (!e.isPromise())
   224                 return e.value();
   225             try {
   226                 // Try to make a real entry for the promised version.
   227                 e = makeEntry(e.version(), computeValue(type));
   228             } finally {
   229                 // Whether computeValue throws or returns normally,
   230                 // be sure to remove the empty entry.
   231                 e = map.finishEntry(this, e);
   232             }
   233             if (e != null)
   234                 return e.value();
   235             // else try again, in case a racing thread called remove (so e == null)
   236         }
   237     }
   238 
   239     /** Check that e is non-null, matches this ClassValue, and is live. */
   240     boolean match(Entry<?> e) {
   241         // racing e.version : null (blank) => unique Version token => null (GC-ed version)
   242         // non-racing this.version : v1 => v2 => ... (updates are read faithfully from volatile)
   243         return (e != null && e.get() == this.version);
   244         // invariant:  No false positives on version match.  Null is OK for false negative.
   245         // invariant:  If version matches, then e.value is readable (final set in Entry.<init>)
   246     }
   247 
   248     /** Internal hash code for accessing Class.classValueMap.cacheArray. */
   249     final int hashCodeForCache = nextHashCode.getAndAdd(HASH_INCREMENT) & HASH_MASK;
   250 
   251     /** Value stream for hashCodeForCache.  See similar structure in ThreadLocal. */
   252     private static final AtomicInteger nextHashCode = new AtomicInteger();
   253 
   254     /** Good for power-of-two tables.  See similar structure in ThreadLocal. */
   255     private static final int HASH_INCREMENT = 0x61c88647;
   256 
   257     /** Mask a hash code to be positive but not too large, to prevent wraparound. */
   258     static final int HASH_MASK = (-1 >>> 2);
   259 
   260     /**
   261      * Private key for retrieval of this object from ClassValueMap.
   262      */
   263     static class Identity {
   264     }
   265     /**
   266      * This ClassValue's identity, expressed as an opaque object.
   267      * The main object {@code ClassValue.this} is incorrect since
   268      * subclasses may override {@code ClassValue.equals}, which
   269      * could confuse keys in the ClassValueMap.
   270      */
   271     final Identity identity = new Identity();
   272 
   273     /**
   274      * Current version for retrieving this class value from the cache.
   275      * Any number of computeValue calls can be cached in association with one version.
   276      * But the version changes when a remove (on any type) is executed.
   277      * A version change invalidates all cache entries for the affected ClassValue,
   278      * by marking them as stale.  Stale cache entries do not force another call
   279      * to computeValue, but they do require a synchronized visit to a backing map.
   280      * <p>
   281      * All user-visible state changes on the ClassValue take place under
   282      * a lock inside the synchronized methods of ClassValueMap.
   283      * Readers (of ClassValue.get) are notified of such state changes
   284      * when this.version is bumped to a new token.
   285      * This variable must be volatile so that an unsynchronized reader
   286      * will receive the notification without delay.
   287      * <p>
   288      * If version were not volatile, one thread T1 could persistently hold onto
   289      * a stale value this.value == V1, while while another thread T2 advances
   290      * (under a lock) to this.value == V2.  This will typically be harmless,
   291      * but if T1 and T2 interact causally via some other channel, such that
   292      * T1's further actions are constrained (in the JMM) to happen after
   293      * the V2 event, then T1's observation of V1 will be an error.
   294      * <p>
   295      * The practical effect of making this.version be volatile is that it cannot
   296      * be hoisted out of a loop (by an optimizing JIT) or otherwise cached.
   297      * Some machines may also require a barrier instruction to execute
   298      * before this.version.
   299      */
   300     private volatile Version<T> version = new Version<>(this);
   301     Version<T> version() { return version; }
   302     void bumpVersion() { version = new Version<>(this); }
   303     static class Version<T> {
   304         private final ClassValue<T> classValue;
   305         private final Entry<T> promise = new Entry<>(this);
   306         Version(ClassValue<T> classValue) { this.classValue = classValue; }
   307         ClassValue<T> classValue() { return classValue; }
   308         Entry<T> promise() { return promise; }
   309         boolean isLive() { return classValue.version() == this; }
   310     }
   311 
   312     /** One binding of a value to a class via a ClassValue.
   313      *  States are:<ul>
   314      *  <li> promise if value == Entry.this
   315      *  <li> else dead if version == null
   316      *  <li> else stale if version != classValue.version
   317      *  <li> else live </ul>
   318      *  Promises are never put into the cache; they only live in the
   319      *  backing map while a computeValue call is in flight.
   320      *  Once an entry goes stale, it can be reset at any time
   321      *  into the dead state.
   322      */
   323     static class Entry<T> extends WeakReference<Version<T>> {
   324         final Object value;  // usually of type T, but sometimes (Entry)this
   325         Entry(Version<T> version, T value) {
   326             super(version);
   327             this.value = value;  // for a regular entry, value is of type T
   328         }
   329         private void assertNotPromise() { assert(!isPromise()); }
   330         /** For creating a promise. */
   331         Entry(Version<T> version) {
   332             super(version);
   333             this.value = this;  // for a promise, value is not of type T, but Entry!
   334         }
   335         /** Fetch the value.  This entry must not be a promise. */
   336         @SuppressWarnings("unchecked")  // if !isPromise, type is T
   337         T value() { assertNotPromise(); return (T) value; }
   338         boolean isPromise() { return value == this; }
   339         Version<T> version() { return get(); }
   340         ClassValue<T> classValueOrNull() {
   341             Version<T> v = version();
   342             return (v == null) ? null : v.classValue();
   343         }
   344         boolean isLive() {
   345             Version<T> v = version();
   346             if (v == null)  return false;
   347             if (v.isLive())  return true;
   348             clear();
   349             return false;
   350         }
   351         Entry<T> refreshVersion(Version<T> v2) {
   352             assertNotPromise();
   353             @SuppressWarnings("unchecked")  // if !isPromise, type is T
   354             Entry<T> e2 = new Entry<>(v2, (T) value);
   355             clear();
   356             // value = null -- caller must drop
   357             return e2;
   358         }
   359         static final Entry<?> DEAD_ENTRY = new Entry<>(null, null);
   360     }
   361 
   362     /** Return the backing map associated with this type. */
   363     private static ClassValueMap getMap(Class<?> type) {
   364         // racing type.classValueMap : null (blank) => unique ClassValueMap
   365         // if a null is observed, a map is created (lazily, synchronously, uniquely)
   366         // all further access to that map is synchronized
   367         ClassValueMap map = type.classValueMap;
   368         if (map != null)  return map;
   369         return initializeMap(type);
   370     }
   371 
   372     private static final Object CRITICAL_SECTION = new Object();
   373     private static ClassValueMap initializeMap(Class<?> type) {
   374         ClassValueMap map;
   375         synchronized (CRITICAL_SECTION) {  // private object to avoid deadlocks
   376             // happens about once per type
   377             if ((map = type.classValueMap) == null)
   378                 type.classValueMap = map = new ClassValueMap(type);
   379         }
   380             return map;
   381         }
   382 
   383     static <T> Entry<T> makeEntry(Version<T> explicitVersion, T value) {
   384         // Note that explicitVersion might be different from this.version.
   385         return new Entry<>(explicitVersion, value);
   386 
   387         // As soon as the Entry is put into the cache, the value will be
   388         // reachable via a data race (as defined by the Java Memory Model).
   389         // This race is benign, assuming the value object itself can be
   390         // read safely by multiple threads.  This is up to the user.
   391         //
   392         // The entry and version fields themselves can be safely read via
   393         // a race because they are either final or have controlled states.
   394         // If the pointer from the entry to the version is still null,
   395         // or if the version goes immediately dead and is nulled out,
   396         // the reader will take the slow path and retry under a lock.
   397     }
   398 
   399     // The following class could also be top level and non-public:
   400 
   401     /** A backing map for all ClassValues, relative a single given type.
   402      *  Gives a fully serialized "true state" for each pair (ClassValue cv, Class type).
   403      *  Also manages an unserialized fast-path cache.
   404      */
   405     static class ClassValueMap extends WeakHashMap<ClassValue.Identity, Entry<?>> {
   406         private final Class<?> type;
   407         private Entry<?>[] cacheArray;
   408         private int cacheLoad, cacheLoadLimit;
   409 
   410         /** Number of entries initially allocated to each type when first used with any ClassValue.
   411          *  It would be pointless to make this much smaller than the Class and ClassValueMap objects themselves.
   412          *  Must be a power of 2.
   413          */
   414         private static final int INITIAL_ENTRIES = 32;
   415 
   416         /** Build a backing map for ClassValues, relative the given type.
   417          *  Also, create an empty cache array and install it on the class.
   418          */
   419         ClassValueMap(Class<?> type) {
   420             this.type = type;
   421             sizeCache(INITIAL_ENTRIES);
   422         }
   423 
   424         Entry<?>[] getCache() { return cacheArray; }
   425 
   426         /** Initiate a query.  Store a promise (placeholder) if there is no value yet. */
   427         synchronized
   428         <T> Entry<T> startEntry(ClassValue<T> classValue) {
   429             @SuppressWarnings("unchecked")  // one map has entries for all value types <T>
   430             Entry<T> e = (Entry<T>) get(classValue.identity);
   431             Version<T> v = classValue.version();
   432             if (e == null) {
   433                 e = v.promise();
   434                 // The presence of a promise means that a value is pending for v.
   435                 // Eventually, finishEntry will overwrite the promise.
   436                 put(classValue.identity, e);
   437                 // Note that the promise is never entered into the cache!
   438                 return e;
   439             } else if (e.isPromise()) {
   440                 // Somebody else has asked the same question.
   441                 // Let the races begin!
   442                 if (e.version() != v) {
   443                     e = v.promise();
   444                     put(classValue.identity, e);
   445                 }
   446                 return e;
   447             } else {
   448                 // there is already a completed entry here; report it
   449                 if (e.version() != v) {
   450                     // There is a stale but valid entry here; make it fresh again.
   451                     // Once an entry is in the hash table, we don't care what its version is.
   452                     e = e.refreshVersion(v);
   453                     put(classValue.identity, e);
   454                 }
   455                 // Add to the cache, to enable the fast path, next time.
   456                 checkCacheLoad();
   457                 addToCache(classValue, e);
   458                 return e;
   459             }
   460         }
   461 
   462         /** Finish a query.  Overwrite a matching placeholder.  Drop stale incoming values. */
   463         synchronized
   464         <T> Entry<T> finishEntry(ClassValue<T> classValue, Entry<T> e) {
   465             @SuppressWarnings("unchecked")  // one map has entries for all value types <T>
   466             Entry<T> e0 = (Entry<T>) get(classValue.identity);
   467             if (e == e0) {
   468                 // We can get here during exception processing, unwinding from computeValue.
   469                 assert(e.isPromise());
   470                 remove(classValue.identity);
   471                 return null;
   472             } else if (e0 != null && e0.isPromise() && e0.version() == e.version()) {
   473                 // If e0 matches the intended entry, there has not been a remove call
   474                 // between the previous startEntry and now.  So now overwrite e0.
   475                 Version<T> v = classValue.version();
   476                 if (e.version() != v)
   477                     e = e.refreshVersion(v);
   478                 put(classValue.identity, e);
   479                 // Add to the cache, to enable the fast path, next time.
   480                 checkCacheLoad();
   481                 addToCache(classValue, e);
   482                 return e;
   483             } else {
   484                 // Some sort of mismatch; caller must try again.
   485                 return null;
   486             }
   487         }
   488 
   489         /** Remove an entry. */
   490         synchronized
   491         void removeEntry(ClassValue<?> classValue) {
   492             Entry<?> e = remove(classValue.identity);
   493             if (e == null) {
   494                 // Uninitialized, and no pending calls to computeValue.  No change.
   495             } else if (e.isPromise()) {
   496                 // State is uninitialized, with a pending call to finishEntry.
   497                 // Since remove is a no-op in such a state, keep the promise
   498                 // by putting it back into the map.
   499                 put(classValue.identity, e);
   500             } else {
   501                 // In an initialized state.  Bump forward, and de-initialize.
   502                 classValue.bumpVersion();
   503                 // Make all cache elements for this guy go stale.
   504                 removeStaleEntries(classValue);
   505             }
   506         }
   507 
   508         /** Change the value for an entry. */
   509         synchronized
   510         <T> void changeEntry(ClassValue<T> classValue, T value) {
   511             @SuppressWarnings("unchecked")  // one map has entries for all value types <T>
   512             Entry<T> e0 = (Entry<T>) get(classValue.identity);
   513             Version<T> version = classValue.version();
   514             if (e0 != null) {
   515                 if (e0.version() == version && e0.value() == value)
   516                     // no value change => no version change needed
   517                     return;
   518                 classValue.bumpVersion();
   519                 removeStaleEntries(classValue);
   520             }
   521             Entry<T> e = makeEntry(version, value);
   522             put(classValue.identity, e);
   523             // Add to the cache, to enable the fast path, next time.
   524             checkCacheLoad();
   525             addToCache(classValue, e);
   526         }
   527 
   528         /// --------
   529         /// Cache management.
   530         /// --------
   531 
   532         // Statics do not need synchronization.
   533 
   534         /** Load the cache entry at the given (hashed) location. */
   535         static Entry<?> loadFromCache(Entry<?>[] cache, int i) {
   536             // non-racing cache.length : constant
   537             // racing cache[i & (mask)] : null <=> Entry
   538             return cache[i & (cache.length-1)];
   539             // invariant:  returned value is null or well-constructed (ready to match)
   540         }
   541 
   542         /** Look in the cache, at the home location for the given ClassValue. */
   543         static <T> Entry<T> probeHomeLocation(Entry<?>[] cache, ClassValue<T> classValue) {
   544             return classValue.castEntry(loadFromCache(cache, classValue.hashCodeForCache));
   545         }
   546 
   547         /** Given that first probe was a collision, retry at nearby locations. */
   548         static <T> Entry<T> probeBackupLocations(Entry<?>[] cache, ClassValue<T> classValue) {
   549             if (PROBE_LIMIT <= 0)  return null;
   550             // Probe the cache carefully, in a range of slots.
   551             int mask = (cache.length-1);
   552             int home = (classValue.hashCodeForCache & mask);
   553             Entry<?> e2 = cache[home];  // victim, if we find the real guy
   554             if (e2 == null) {
   555                 return null;   // if nobody is at home, no need to search nearby
   556             }
   557             // assume !classValue.match(e2), but do not assert, because of races
   558             int pos2 = -1;
   559             for (int i = home + 1; i < home + PROBE_LIMIT; i++) {
   560                 Entry<?> e = cache[i & mask];
   561                 if (e == null) {
   562                     break;   // only search within non-null runs
   563                 }
   564                 if (classValue.match(e)) {
   565                     // relocate colliding entry e2 (from cache[home]) to first empty slot
   566                     cache[home] = e;
   567                     if (pos2 >= 0) {
   568                         cache[i & mask] = Entry.DEAD_ENTRY;
   569                     } else {
   570                         pos2 = i;
   571                     }
   572                     cache[pos2 & mask] = ((entryDislocation(cache, pos2, e2) < PROBE_LIMIT)
   573                                           ? e2                  // put e2 here if it fits
   574                                           : Entry.DEAD_ENTRY);
   575                     return classValue.castEntry(e);
   576                 }
   577                 // Remember first empty slot, if any:
   578                 if (!e.isLive() && pos2 < 0)  pos2 = i;
   579             }
   580             return null;
   581         }
   582 
   583         /** How far out of place is e? */
   584         private static int entryDislocation(Entry<?>[] cache, int pos, Entry<?> e) {
   585             ClassValue<?> cv = e.classValueOrNull();
   586             if (cv == null)  return 0;  // entry is not live!
   587             int mask = (cache.length-1);
   588             return (pos - cv.hashCodeForCache) & mask;
   589         }
   590 
   591         /// --------
   592         /// Below this line all functions are private, and assume synchronized access.
   593         /// --------
   594 
   595         private void sizeCache(int length) {
   596             assert((length & (length-1)) == 0);  // must be power of 2
   597             cacheLoad = 0;
   598             cacheLoadLimit = (int) ((double) length * CACHE_LOAD_LIMIT / 100);
   599             cacheArray = new Entry<?>[length];
   600         }
   601 
   602         /** Make sure the cache load stays below its limit, if possible. */
   603         private void checkCacheLoad() {
   604             if (cacheLoad >= cacheLoadLimit) {
   605                 reduceCacheLoad();
   606             }
   607         }
   608         private void reduceCacheLoad() {
   609             removeStaleEntries();
   610             if (cacheLoad < cacheLoadLimit)
   611                 return;  // win
   612             Entry<?>[] oldCache = getCache();
   613             if (oldCache.length > HASH_MASK)
   614                 return;  // lose
   615             sizeCache(oldCache.length * 2);
   616             for (Entry<?> e : oldCache) {
   617                 if (e != null && e.isLive()) {
   618                     addToCache(e);
   619                 }
   620             }
   621         }
   622 
   623         /** Remove stale entries in the given range.
   624          *  Should be executed under a Map lock.
   625          */
   626         private void removeStaleEntries(Entry<?>[] cache, int begin, int count) {
   627             if (PROBE_LIMIT <= 0)  return;
   628             int mask = (cache.length-1);
   629             int removed = 0;
   630             for (int i = begin; i < begin + count; i++) {
   631                 Entry<?> e = cache[i & mask];
   632                 if (e == null || e.isLive())
   633                     continue;  // skip null and live entries
   634                 Entry<?> replacement = null;
   635                 if (PROBE_LIMIT > 1) {
   636                     // avoid breaking up a non-null run
   637                     replacement = findReplacement(cache, i);
   638                 }
   639                 cache[i & mask] = replacement;
   640                 if (replacement == null)  removed += 1;
   641             }
   642             cacheLoad = Math.max(0, cacheLoad - removed);
   643         }
   644 
   645         /** Clearing a cache slot risks disconnecting following entries
   646          *  from the head of a non-null run, which would allow them
   647          *  to be found via reprobes.  Find an entry after cache[begin]
   648          *  to plug into the hole, or return null if none is needed.
   649          */
   650         private Entry<?> findReplacement(Entry<?>[] cache, int home1) {
   651             Entry<?> replacement = null;
   652             int haveReplacement = -1, replacementPos = 0;
   653             int mask = (cache.length-1);
   654             for (int i2 = home1 + 1; i2 < home1 + PROBE_LIMIT; i2++) {
   655                 Entry<?> e2 = cache[i2 & mask];
   656                 if (e2 == null)  break;  // End of non-null run.
   657                 if (!e2.isLive())  continue;  // Doomed anyway.
   658                 int dis2 = entryDislocation(cache, i2, e2);
   659                 if (dis2 == 0)  continue;  // e2 already optimally placed
   660                 int home2 = i2 - dis2;
   661                 if (home2 <= home1) {
   662                     // e2 can replace entry at cache[home1]
   663                     if (home2 == home1) {
   664                         // Put e2 exactly where he belongs.
   665                         haveReplacement = 1;
   666                         replacementPos = i2;
   667                         replacement = e2;
   668                     } else if (haveReplacement <= 0) {
   669                         haveReplacement = 0;
   670                         replacementPos = i2;
   671                         replacement = e2;
   672                     }
   673                     // And keep going, so we can favor larger dislocations.
   674                 }
   675             }
   676             if (haveReplacement >= 0) {
   677                 if (cache[(replacementPos+1) & mask] != null) {
   678                     // Be conservative, to avoid breaking up a non-null run.
   679                     cache[replacementPos & mask] = (Entry<?>) Entry.DEAD_ENTRY;
   680                 } else {
   681                     cache[replacementPos & mask] = null;
   682                     cacheLoad -= 1;
   683                 }
   684             }
   685             return replacement;
   686         }
   687 
   688         /** Remove stale entries in the range near classValue. */
   689         private void removeStaleEntries(ClassValue<?> classValue) {
   690             removeStaleEntries(getCache(), classValue.hashCodeForCache, PROBE_LIMIT);
   691         }
   692 
   693         /** Remove all stale entries, everywhere. */
   694         private void removeStaleEntries() {
   695             Entry<?>[] cache = getCache();
   696             removeStaleEntries(cache, 0, cache.length + PROBE_LIMIT - 1);
   697         }
   698 
   699         /** Add the given entry to the cache, in its home location, unless it is out of date. */
   700         private <T> void addToCache(Entry<T> e) {
   701             ClassValue<T> classValue = e.classValueOrNull();
   702             if (classValue != null)
   703                 addToCache(classValue, e);
   704         }
   705 
   706         /** Add the given entry to the cache, in its home location. */
   707         private <T> void addToCache(ClassValue<T> classValue, Entry<T> e) {
   708             if (PROBE_LIMIT <= 0)  return;  // do not fill cache
   709             // Add e to the cache.
   710             Entry<?>[] cache = getCache();
   711             int mask = (cache.length-1);
   712             int home = classValue.hashCodeForCache & mask;
   713             Entry<?> e2 = placeInCache(cache, home, e, false);
   714             if (e2 == null)  return;  // done
   715             if (PROBE_LIMIT > 1) {
   716                 // try to move e2 somewhere else in his probe range
   717                 int dis2 = entryDislocation(cache, home, e2);
   718                 int home2 = home - dis2;
   719                 for (int i2 = home2; i2 < home2 + PROBE_LIMIT; i2++) {
   720                     if (placeInCache(cache, i2 & mask, e2, true) == null) {
   721                         return;
   722                     }
   723                 }
   724             }
   725             // Note:  At this point, e2 is just dropped from the cache.
   726         }
   727 
   728         /** Store the given entry.  Update cacheLoad, and return any live victim.
   729          *  'Gently' means return self rather than dislocating a live victim.
   730          */
   731         private Entry<?> placeInCache(Entry<?>[] cache, int pos, Entry<?> e, boolean gently) {
   732             Entry<?> e2 = overwrittenEntry(cache[pos]);
   733             if (gently && e2 != null) {
   734                 // do not overwrite a live entry
   735                 return e;
   736             } else {
   737                 cache[pos] = e;
   738                 return e2;
   739             }
   740         }
   741 
   742         /** Note an entry that is about to be overwritten.
   743          *  If it is not live, quietly replace it by null.
   744          *  If it is an actual null, increment cacheLoad,
   745          *  because the caller is going to store something
   746          *  in its place.
   747          */
   748         private <T> Entry<T> overwrittenEntry(Entry<T> e2) {
   749             if (e2 == null)  cacheLoad += 1;
   750             else if (e2.isLive())  return e2;
   751             return null;
   752         }
   753 
   754         /** Percent loading of cache before resize. */
   755         private static final int CACHE_LOAD_LIMIT = 67;  // 0..100
   756         /** Maximum number of probes to attempt. */
   757         private static final int PROBE_LIMIT      =  6;       // 1..
   758         // N.B.  Set PROBE_LIMIT=0 to disable all fast paths.
   759     }
   760 }