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.
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.
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).
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.
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
28 import java.lang.ClassValue.ClassValueMap;
29 import java.util.WeakHashMap;
30 import java.lang.ref.WeakReference;
31 import java.util.concurrent.atomic.AtomicInteger;
33 import static java.lang.ClassValue.ClassValueMap.probeHomeLocation;
34 import static java.lang.ClassValue.ClassValueMap.probeBackupLocations;
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
45 public abstract class ClassValue<T> {
47 * Sole constructor. (For invocation by subclass constructors, typically
50 protected ClassValue() {
54 * Computes the given class's derived value for this {@code ClassValue}.
56 * This method will be invoked within the first thread that accesses
57 * the value with the {@link #get get} method.
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}.
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.
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
71 protected abstract T computeValue(Class<?> type);
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.
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.
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}.
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.
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
99 public T get(Class<?> type) {
100 // non-racing this.hashCodeForCache : final int
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
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.
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);
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.
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.
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}.
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.
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}.
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.
148 * Discarding and retrying is an important proviso,
149 * since otherwise {@code T} could potentially install
150 * a disastrously stale value. For example:
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>
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.
168 * @param type the type whose class value must be removed
169 * @throws NullPointerException if the argument is null
171 public void remove(Class<?> type) {
172 ClassValueMap map = getMap(type);
173 map.removeEntry(this);
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);
183 /// Implementation...
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();
193 // invariant: returned value is safe to dereference and check for an Entry
196 /** Initial, one-element, empty cache used by all Class instances. Must never be filled. */
197 private static final Entry<?>[] EMPTY_CACHE = { null };
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.
205 private T getFromBackup(Entry<?>[] cache, Class<?> type) {
206 Entry<T> e = probeBackupLocations(cache, this);
209 return getFromHashMap(type);
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; }
216 /** Called when the fast path of get fails, and cache reprobe also fails.
218 private T getFromHashMap(Class<?> type) {
219 // The fail-safe recovery is to fall back to the underlying classValueMap.
220 ClassValueMap map = getMap(type);
222 Entry<T> e = map.startEntry(this);
226 // Try to make a real entry for the promised version.
227 e = makeEntry(e.version(), computeValue(type));
229 // Whether computeValue throws or returns normally,
230 // be sure to remove the empty entry.
231 e = map.finishEntry(this, e);
235 // else try again, in case a racing thread called remove (so e == null)
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>)
248 /** Internal hash code for accessing Class.classValueMap.cacheArray. */
249 final int hashCodeForCache = nextHashCode.getAndAdd(HASH_INCREMENT) & HASH_MASK;
251 /** Value stream for hashCodeForCache. See similar structure in ThreadLocal. */
252 private static final AtomicInteger nextHashCode = new AtomicInteger();
254 /** Good for power-of-two tables. See similar structure in ThreadLocal. */
255 private static final int HASH_INCREMENT = 0x61c88647;
257 /** Mask a hash code to be positive but not too large, to prevent wraparound. */
258 static final int HASH_MASK = (-1 >>> 2);
261 * Private key for retrieval of this object from ClassValueMap.
263 static class Identity {
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.
271 final Identity identity = new Identity();
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.
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.
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.
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.
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; }
312 /** One binding of a value to a class via a ClassValue.
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.
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) {
327 this.value = value; // for a regular entry, value is of type T
329 private void assertNotPromise() { assert(!isPromise()); }
330 /** For creating a promise. */
331 Entry(Version<T> version) {
333 this.value = this; // for a promise, value is not of type T, but Entry!
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();
345 Version<T> v = version();
346 if (v == null) return false;
347 if (v.isLive()) return true;
351 Entry<T> refreshVersion(Version<T> v2) {
353 @SuppressWarnings("unchecked") // if !isPromise, type is T
354 Entry<T> e2 = new Entry<>(v2, (T) value);
356 // value = null -- caller must drop
359 static final Entry<?> DEAD_ENTRY = new Entry<>(null, null);
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);
372 private static final Object CRITICAL_SECTION = new Object();
373 private static ClassValueMap initializeMap(Class<?> type) {
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);
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);
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.
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.
399 // The following class could also be top level and non-public:
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.
405 static class ClassValueMap extends WeakHashMap<ClassValue.Identity, Entry<?>> {
406 private final Class<?> type;
407 private Entry<?>[] cacheArray;
408 private int cacheLoad, cacheLoadLimit;
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.
414 private static final int INITIAL_ENTRIES = 32;
416 /** Build a backing map for ClassValues, relative the given type.
417 * Also, create an empty cache array and install it on the class.
419 ClassValueMap(Class<?> type) {
421 sizeCache(INITIAL_ENTRIES);
424 Entry<?>[] getCache() { return cacheArray; }
426 /** Initiate a query. Store a promise (placeholder) if there is no value yet. */
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();
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!
439 } else if (e.isPromise()) {
440 // Somebody else has asked the same question.
441 // Let the races begin!
442 if (e.version() != v) {
444 put(classValue.identity, e);
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);
455 // Add to the cache, to enable the fast path, next time.
457 addToCache(classValue, e);
462 /** Finish a query. Overwrite a matching placeholder. Drop stale incoming values. */
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);
468 // We can get here during exception processing, unwinding from computeValue.
469 assert(e.isPromise());
470 remove(classValue.identity);
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.
481 addToCache(classValue, e);
484 // Some sort of mismatch; caller must try again.
489 /** Remove an entry. */
491 void removeEntry(ClassValue<?> classValue) {
492 Entry<?> e = remove(classValue.identity);
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);
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);
508 /** Change the value for an entry. */
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();
515 if (e0.version() == version && e0.value() == value)
516 // no value change => no version change needed
518 classValue.bumpVersion();
519 removeStaleEntries(classValue);
521 Entry<T> e = makeEntry(version, value);
522 put(classValue.identity, e);
523 // Add to the cache, to enable the fast path, next time.
525 addToCache(classValue, e);
529 /// Cache management.
532 // Statics do not need synchronization.
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)
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));
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
555 return null; // if nobody is at home, no need to search nearby
557 // assume !classValue.match(e2), but do not assert, because of races
559 for (int i = home + 1; i < home + PROBE_LIMIT; i++) {
560 Entry<?> e = cache[i & mask];
562 break; // only search within non-null runs
564 if (classValue.match(e)) {
565 // relocate colliding entry e2 (from cache[home]) to first empty slot
568 cache[i & mask] = Entry.DEAD_ENTRY;
572 cache[pos2 & mask] = ((entryDislocation(cache, pos2, e2) < PROBE_LIMIT)
573 ? e2 // put e2 here if it fits
575 return classValue.castEntry(e);
577 // Remember first empty slot, if any:
578 if (!e.isLive() && pos2 < 0) pos2 = i;
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;
592 /// Below this line all functions are private, and assume synchronized access.
595 private void sizeCache(int length) {
596 assert((length & (length-1)) == 0); // must be power of 2
598 cacheLoadLimit = (int) ((double) length * CACHE_LOAD_LIMIT / 100);
599 cacheArray = new Entry<?>[length];
602 /** Make sure the cache load stays below its limit, if possible. */
603 private void checkCacheLoad() {
604 if (cacheLoad >= cacheLoadLimit) {
608 private void reduceCacheLoad() {
609 removeStaleEntries();
610 if (cacheLoad < cacheLoadLimit)
612 Entry<?>[] oldCache = getCache();
613 if (oldCache.length > HASH_MASK)
615 sizeCache(oldCache.length * 2);
616 for (Entry<?> e : oldCache) {
617 if (e != null && e.isLive()) {
623 /** Remove stale entries in the given range.
624 * Should be executed under a Map lock.
626 private void removeStaleEntries(Entry<?>[] cache, int begin, int count) {
627 if (PROBE_LIMIT <= 0) return;
628 int mask = (cache.length-1);
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);
639 cache[i & mask] = replacement;
640 if (replacement == null) removed += 1;
642 cacheLoad = Math.max(0, cacheLoad - removed);
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.
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.
668 } else if (haveReplacement <= 0) {
673 // And keep going, so we can favor larger dislocations.
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;
681 cache[replacementPos & mask] = null;
688 /** Remove stale entries in the range near classValue. */
689 private void removeStaleEntries(ClassValue<?> classValue) {
690 removeStaleEntries(getCache(), classValue.hashCodeForCache, PROBE_LIMIT);
693 /** Remove all stale entries, everywhere. */
694 private void removeStaleEntries() {
695 Entry<?>[] cache = getCache();
696 removeStaleEntries(cache, 0, cache.length + PROBE_LIMIT - 1);
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);
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) {
725 // Note: At this point, e2 is just dropped from the cache.
728 /** Store the given entry. Update cacheLoad, and return any live victim.
729 * 'Gently' means return self rather than dislocating a live victim.
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
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
748 private <T> Entry<T> overwrittenEntry(Entry<T> e2) {
749 if (e2 == null) cacheLoad += 1;
750 else if (e2.isLive()) return e2;
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.