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