It seems that the JIT is lost with whe there is a loopy callsite and never stabilize (or the steady state is after the program ends).

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.MutableCallSite;

public class Loop {
  static class LoopyCS extends MutableCallSite {
    public LoopyCS() {
      super(MethodType.methodType(void.class, int.class));

      MethodHandle target = dynamicInvoker();
      target = MethodHandles.filterArguments(target, 0, FOO);
      target = MethodHandles.guardWithTest(ZERO,
          target,
MethodHandles.dropArguments(MethodHandles.constant(int.class, 0).asType(MethodType.methodType(void.class)), 0, int.class));
      setTarget(target);
    }
  }

  static final MethodHandle FOO, ZERO;
  static {
    try {
FOO = MethodHandles.lookup().findStatic(Loop.class, "foo", MethodType.methodType(int.class, int.class)); ZERO = MethodHandles.lookup().findStatic(Loop.class, "zero", MethodType.methodType(boolean.class, int.class));
    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new AssertionError(e);
    }
  }

  private static boolean zero(int i) {
    return i != 0;
  }

  private static int foo(int i) {
    COUNTER++;
    return i - 1;
  }

  private static int COUNTER = 0;

  public static void main(String[] args) throws Throwable {
    for(int i=0; i<100_000; i++) {
      new LoopyCS().getTarget().invokeExact(1_000);
    }
    System.out.println(COUNTER);
  }
}

cheers,
RĂ©mi


Reply via email to