src/java.base/share/classes/java/lang/invoke/MethodTypeForm.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Sdiff src/java.base/share/classes/java/lang/invoke

src/java.base/share/classes/java/lang/invoke/MethodTypeForm.java

Print this page
rev 11011 : 8057020: LambdaForm caches should support eviction
Reviewed-by: psandoz, ?


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import sun.invoke.util.Wrapper;

  29 import static java.lang.invoke.MethodHandleStatics.*;
  30 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  31  import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  32 
  33 /**
  34  * Shared information for a group of method types, which differ
  35  * only by reference types, and therefore share a common erasure
  36  * and wrapping.
  37  * <p>
  38  * For an empirical discussion of the structure of method types,
  39  * see <a href="http://groups.google.com/group/jvm-languages/browse_thread/thread/ac9308ae74da9b7e/">
  40  * the thread "Avoiding Boxing" on jvm-languages</a>.
  41  * There are approximately 2000 distinct erased method types in the JDK.
  42  * There are a little over 10 times that number of unerased types.
  43  * No more than half of these are likely to be loaded at once.
  44  * @author John Rose
  45  */
  46 final class MethodTypeForm {
  47     final int[] argToSlotTable, slotToArgTable;
  48     final long argCounts;               // packed slot & value counts
  49     final long primCounts;              // packed prim & double counts
  50     final MethodType erasedType;        // the canonical erasure
  51     final MethodType basicType;         // the canonical erasure, with primitives simplified
  52 
  53     // Cached adapter information:
  54     @Stable final MethodHandle[] methodHandles;
  55     // Indexes into methodHandles:
  56     static final int
  57             MH_BASIC_INV      =  0,  // cached instance of MH.invokeBasic
  58             MH_NF_INV         =  1,  // cached helper for LF.NamedFunction
  59             MH_UNINIT_CS      =  2,  // uninitialized call site
  60             MH_LIMIT          =  3;
  61 
  62     // Cached lambda form information, for basic types only:
  63     final @Stable LambdaForm[] lambdaForms;
  64     // Indexes into lambdaForms:
  65     static final int
  66             LF_INVVIRTUAL              =  0,  // DMH invokeVirtual
  67             LF_INVSTATIC               =  1,
  68             LF_INVSPECIAL              =  2,
  69             LF_NEWINVSPECIAL           =  3,
  70             LF_INVINTERFACE            =  4,
  71             LF_INVSTATIC_INIT          =  5,  // DMH invokeStatic with <clinit> barrier
  72             LF_INTERPRET               =  6,  // LF interpreter
  73             LF_REBIND                  =  7,  // BoundMethodHandle
  74             LF_DELEGATE                =  8,  // DelegatingMethodHandle
  75             LF_DELEGATE_BLOCK_INLINING =  9,  // Counting DelegatingMethodHandle w/ @DontInline
  76             LF_EX_LINKER               = 10,  // invokeExact_MT (for invokehandle)
  77             LF_EX_INVOKER              = 11,  // MHs.invokeExact
  78             LF_GEN_LINKER              = 12,  // generic invoke_MT (for invokehandle)
  79             LF_GEN_INVOKER             = 13,  // generic MHs.invoke
  80             LF_CS_LINKER               = 14,  // linkToCallSite_CS
  81             LF_MH_LINKER               = 15,  // linkToCallSite_MH
  82             LF_GWC                     = 16,  // guardWithCatch (catchException)
  83             LF_GWT                     = 17,  // guardWithTest


  91     }
  92 
  93     /** Return the basic type derived from the erased type of this MT-form.
  94      *  A basic type is erased (all references Object) and also has all primitive
  95      *  types (except int, long, float, double, void) normalized to int.
  96      *  Such basic types correspond to low-level JVM calling sequences.
  97      */
  98     public MethodType basicType() {
  99         return basicType;
 100     }
 101 
 102     private boolean assertIsBasicType() {
 103         // primitives must be flattened also
 104         assert(erasedType == basicType)
 105                 : "erasedType: " + erasedType + " != basicType: " + basicType;
 106         return true;
 107     }
 108 
 109     public MethodHandle cachedMethodHandle(int which) {
 110         assert(assertIsBasicType());
 111         return methodHandles[which];

 112     }
 113 
 114     synchronized public MethodHandle setCachedMethodHandle(int which, MethodHandle mh) {
 115         // Simulate a CAS, to avoid racy duplication of results.
 116         MethodHandle prev = methodHandles[which];
 117         if (prev != null)  return prev;
 118         return methodHandles[which] = mh;






 119     }
 120 
 121     public LambdaForm cachedLambdaForm(int which) {
 122         assert(assertIsBasicType());
 123         return lambdaForms[which];

 124     }
 125 
 126     synchronized public LambdaForm setCachedLambdaForm(int which, LambdaForm form) {
 127         // Simulate a CAS, to avoid racy duplication of results.
 128         LambdaForm prev = lambdaForms[which];
 129         if (prev != null) return prev;
 130         return lambdaForms[which] = form;






 131     }
 132 
 133     /**
 134      * Build an MTF for a given type, which must have all references erased to Object.
 135      * This MTF will stand for that type and all un-erased variations.
 136      * Eagerly compute some basic properties of the type, common to all variations.
 137      */

 138     protected MethodTypeForm(MethodType erasedType) {
 139         this.erasedType = erasedType;
 140 
 141         Class<?>[] ptypes = erasedType.ptypes();
 142         int ptypeCount = ptypes.length;
 143         int pslotCount = ptypeCount;            // temp. estimate
 144         int rtypeCount = 1;                     // temp. estimate
 145         int rslotCount = 1;                     // temp. estimate
 146 
 147         int[] argToSlotTab = null, slotToArgTab = null;
 148 
 149         // Walk the argument types, looking for primitives.
 150         int pac = 0, lac = 0, prc = 0, lrc = 0;
 151         Class<?>[] epts = ptypes;
 152         Class<?>[] bpts = epts;
 153         for (int i = 0; i < epts.length; i++) {
 154             Class<?> pt = epts[i];
 155             if (pt != Object.class) {
 156                 ++pac;
 157                 Wrapper w = Wrapper.forPrimitiveType(pt);


 217         } else {
 218             int slot = ptypeCount; // first arg is deepest in stack
 219             slotToArgTab = new int[slot+1];
 220             argToSlotTab = new int[1+ptypeCount];
 221             argToSlotTab[0] = slot;  // argument "-1" is past end of slots
 222             for (int i = 0; i < ptypeCount; i++) {
 223                 --slot;
 224                 slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
 225                 argToSlotTab[1+i]  = slot;
 226             }
 227         }
 228         this.primCounts = pack(lrc, prc, lac, pac);
 229         this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount);
 230         this.argToSlotTable = argToSlotTab;
 231         this.slotToArgTable = slotToArgTab;
 232 
 233         if (pslotCount >= 256)  throw newIllegalArgumentException("too many arguments");
 234 
 235         // Initialize caches, but only for basic types
 236         assert(basicType == erasedType);
 237         this.lambdaForms = new LambdaForm[LF_LIMIT];
 238         this.methodHandles = new MethodHandle[MH_LIMIT];
 239     }
 240 
 241     private static long pack(int a, int b, int c, int d) {
 242         assert(((a|b|c|d) & ~0xFFFF) == 0);
 243         long hw = ((a << 16) | b), lw = ((c << 16) | d);
 244         return (hw << 32) | lw;
 245     }
 246     private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
 247         assert(word <= 3);
 248         return (char)(packed >> ((3-word) * 16));
 249     }
 250 
 251     public int parameterCount() {                      // # outgoing values
 252         return unpack(argCounts, 3);
 253     }
 254     public int parameterSlotCount() {                  // # outgoing interpreter slots
 255         return unpack(argCounts, 2);
 256     }
 257     public int returnCount() {                         // = 0 (V), or 1
 258         return unpack(argCounts, 1);


 392      */
 393     static Class<?>[] canonicalizeAll(Class<?>[] ts, int how) {
 394         Class<?>[] cs = null;
 395         for (int imax = ts.length, i = 0; i < imax; i++) {
 396             Class<?> c = canonicalize(ts[i], how);
 397             if (c == void.class)
 398                 c = null;  // a Void parameter was unwrapped to void; ignore
 399             if (c != null) {
 400                 if (cs == null)
 401                     cs = ts.clone();
 402                 cs[i] = c;
 403             }
 404         }
 405         return cs;
 406     }
 407 
 408     @Override
 409     public String toString() {
 410         return "Form"+erasedType;
 411     }
 412 
 413 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import sun.invoke.util.Wrapper;
  29 import java.lang.ref.SoftReference;
  30 import static java.lang.invoke.MethodHandleStatics.*;


  31 
  32 /**
  33  * Shared information for a group of method types, which differ
  34  * only by reference types, and therefore share a common erasure
  35  * and wrapping.
  36  * <p>
  37  * For an empirical discussion of the structure of method types,
  38  * see <a href="http://groups.google.com/group/jvm-languages/browse_thread/thread/ac9308ae74da9b7e/">
  39  * the thread "Avoiding Boxing" on jvm-languages</a>.
  40  * There are approximately 2000 distinct erased method types in the JDK.
  41  * There are a little over 10 times that number of unerased types.
  42  * No more than half of these are likely to be loaded at once.
  43  * @author John Rose
  44  */
  45 final class MethodTypeForm {
  46     final int[] argToSlotTable, slotToArgTable;
  47     final long argCounts;               // packed slot & value counts
  48     final long primCounts;              // packed prim & double counts
  49     final MethodType erasedType;        // the canonical erasure
  50     final MethodType basicType;         // the canonical erasure, with primitives simplified
  51 
  52     // Cached adapter information:
  53     @Stable final SoftReference<MethodHandle>[] methodHandles;
  54     // Indexes into methodHandles:
  55     static final int
  56             MH_BASIC_INV      =  0,  // cached instance of MH.invokeBasic
  57             MH_NF_INV         =  1,  // cached helper for LF.NamedFunction
  58             MH_UNINIT_CS      =  2,  // uninitialized call site
  59             MH_LIMIT          =  3;
  60 
  61     // Cached lambda form information, for basic types only:
  62     final @Stable SoftReference<LambdaForm>[] lambdaForms;
  63     // Indexes into lambdaForms:
  64     static final int
  65             LF_INVVIRTUAL              =  0,  // DMH invokeVirtual
  66             LF_INVSTATIC               =  1,
  67             LF_INVSPECIAL              =  2,
  68             LF_NEWINVSPECIAL           =  3,
  69             LF_INVINTERFACE            =  4,
  70             LF_INVSTATIC_INIT          =  5,  // DMH invokeStatic with <clinit> barrier
  71             LF_INTERPRET               =  6,  // LF interpreter
  72             LF_REBIND                  =  7,  // BoundMethodHandle
  73             LF_DELEGATE                =  8,  // DelegatingMethodHandle
  74             LF_DELEGATE_BLOCK_INLINING =  9,  // Counting DelegatingMethodHandle w/ @DontInline
  75             LF_EX_LINKER               = 10,  // invokeExact_MT (for invokehandle)
  76             LF_EX_INVOKER              = 11,  // MHs.invokeExact
  77             LF_GEN_LINKER              = 12,  // generic invoke_MT (for invokehandle)
  78             LF_GEN_INVOKER             = 13,  // generic MHs.invoke
  79             LF_CS_LINKER               = 14,  // linkToCallSite_CS
  80             LF_MH_LINKER               = 15,  // linkToCallSite_MH
  81             LF_GWC                     = 16,  // guardWithCatch (catchException)
  82             LF_GWT                     = 17,  // guardWithTest


  90     }
  91 
  92     /** Return the basic type derived from the erased type of this MT-form.
  93      *  A basic type is erased (all references Object) and also has all primitive
  94      *  types (except int, long, float, double, void) normalized to int.
  95      *  Such basic types correspond to low-level JVM calling sequences.
  96      */
  97     public MethodType basicType() {
  98         return basicType;
  99     }
 100 
 101     private boolean assertIsBasicType() {
 102         // primitives must be flattened also
 103         assert(erasedType == basicType)
 104                 : "erasedType: " + erasedType + " != basicType: " + basicType;
 105         return true;
 106     }
 107 
 108     public MethodHandle cachedMethodHandle(int which) {
 109         assert(assertIsBasicType());
 110         SoftReference<MethodHandle> entry = methodHandles[which];
 111         return (entry != null) ? entry.get() : null;
 112     }
 113 
 114     synchronized public MethodHandle setCachedMethodHandle(int which, MethodHandle mh) {
 115         // Simulate a CAS, to avoid racy duplication of results.
 116         SoftReference<MethodHandle> entry = methodHandles[which];
 117         if (entry != null) {
 118             MethodHandle prev = entry.get();
 119             if (prev != null) {
 120                 return prev;
 121             }
 122         }
 123         methodHandles[which] = new SoftReference<>(mh);
 124         return mh;
 125     }
 126 
 127     public LambdaForm cachedLambdaForm(int which) {
 128         assert(assertIsBasicType());
 129         SoftReference<LambdaForm> entry = lambdaForms[which];
 130         return (entry != null) ? entry.get() : null;
 131     }
 132 
 133     synchronized public LambdaForm setCachedLambdaForm(int which, LambdaForm form) {
 134         // Simulate a CAS, to avoid racy duplication of results.
 135         SoftReference<LambdaForm> entry = lambdaForms[which];
 136         if (entry != null) {
 137             LambdaForm prev = entry.get();
 138             if (prev != null) {
 139                 return prev;
 140             }
 141         }
 142         lambdaForms[which] = new SoftReference<>(form);
 143         return form;
 144     }
 145 
 146     /**
 147      * Build an MTF for a given type, which must have all references erased to Object.
 148      * This MTF will stand for that type and all un-erased variations.
 149      * Eagerly compute some basic properties of the type, common to all variations.
 150      */
 151     @SuppressWarnings({"rawtypes", "unchecked"})
 152     protected MethodTypeForm(MethodType erasedType) {
 153         this.erasedType = erasedType;
 154 
 155         Class<?>[] ptypes = erasedType.ptypes();
 156         int ptypeCount = ptypes.length;
 157         int pslotCount = ptypeCount;            // temp. estimate
 158         int rtypeCount = 1;                     // temp. estimate
 159         int rslotCount = 1;                     // temp. estimate
 160 
 161         int[] argToSlotTab = null, slotToArgTab = null;
 162 
 163         // Walk the argument types, looking for primitives.
 164         int pac = 0, lac = 0, prc = 0, lrc = 0;
 165         Class<?>[] epts = ptypes;
 166         Class<?>[] bpts = epts;
 167         for (int i = 0; i < epts.length; i++) {
 168             Class<?> pt = epts[i];
 169             if (pt != Object.class) {
 170                 ++pac;
 171                 Wrapper w = Wrapper.forPrimitiveType(pt);


 231         } else {
 232             int slot = ptypeCount; // first arg is deepest in stack
 233             slotToArgTab = new int[slot+1];
 234             argToSlotTab = new int[1+ptypeCount];
 235             argToSlotTab[0] = slot;  // argument "-1" is past end of slots
 236             for (int i = 0; i < ptypeCount; i++) {
 237                 --slot;
 238                 slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
 239                 argToSlotTab[1+i]  = slot;
 240             }
 241         }
 242         this.primCounts = pack(lrc, prc, lac, pac);
 243         this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount);
 244         this.argToSlotTable = argToSlotTab;
 245         this.slotToArgTable = slotToArgTab;
 246 
 247         if (pslotCount >= 256)  throw newIllegalArgumentException("too many arguments");
 248 
 249         // Initialize caches, but only for basic types
 250         assert(basicType == erasedType);
 251         this.lambdaForms   = new SoftReference[LF_LIMIT];
 252         this.methodHandles = new SoftReference[MH_LIMIT];
 253     }
 254 
 255     private static long pack(int a, int b, int c, int d) {
 256         assert(((a|b|c|d) & ~0xFFFF) == 0);
 257         long hw = ((a << 16) | b), lw = ((c << 16) | d);
 258         return (hw << 32) | lw;
 259     }
 260     private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
 261         assert(word <= 3);
 262         return (char)(packed >> ((3-word) * 16));
 263     }
 264 
 265     public int parameterCount() {                      // # outgoing values
 266         return unpack(argCounts, 3);
 267     }
 268     public int parameterSlotCount() {                  // # outgoing interpreter slots
 269         return unpack(argCounts, 2);
 270     }
 271     public int returnCount() {                         // = 0 (V), or 1
 272         return unpack(argCounts, 1);


 406      */
 407     static Class<?>[] canonicalizeAll(Class<?>[] ts, int how) {
 408         Class<?>[] cs = null;
 409         for (int imax = ts.length, i = 0; i < imax; i++) {
 410             Class<?> c = canonicalize(ts[i], how);
 411             if (c == void.class)
 412                 c = null;  // a Void parameter was unwrapped to void; ignore
 413             if (c != null) {
 414                 if (cs == null)
 415                     cs = ts.clone();
 416                 cs[i] = c;
 417             }
 418         }
 419         return cs;
 420     }
 421 
 422     @Override
 423     public String toString() {
 424         return "Form"+erasedType;
 425     }

 426 }
src/java.base/share/classes/java/lang/invoke/MethodTypeForm.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File