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

Optimise `jdk.internal.foreign.abi.SharedUtils::swapArguments(…)`

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Currently, `jdk.internal.foreign.abi.SharedUtils::swapArguments(…)` performs calls to `MethodType::appendParameterTypes(…)` inside a loop, which performs an expensive lookup of a new cached `MethodType` value per call.

      This method should instead use a `Class<?>[]` for the swapped type parameter types computation, which avoids that overhead inside a loop:

      > ```java
      > public static MethodHandle swapArguments(MethodHandle mh, int firstArg, int secondArg) {
      > MethodType mtype = mh.type();
      > int[] perms = new int[mtype.parameterCount()];
      > Class<?>[] ptypes = new Class<?>[perms.length];
      > for (int i = 0 ; i < perms.length ; i++) {
      > int dst = i;
      > if (i == firstArg) dst = secondArg;
      > else if (i == secondArg) dst = firstArg;
      > perms[i] = dst;
      > ptypes[i] = mtype.parameterType(dst);
      > }
      > // TODO: This should use `JavaLangInvokeAccess` to invoke the internal
      > // `MethodType.methodType(Class<?> rtype, Class<?>[] ptypes, boolean trusted)`
      > // method with a `trusted` value of `true`:
      > MethodType swappedType = MethodType.methodType(mtype.returnType(), ptypes);
      > return permuteArguments(mh, swappedType, perms);
      > }
      > ```


            jvernee Jorn Vernee
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: