guardIfConstant, the constant sniffer combinator

Remi Forax forax at univ-mlv.fr
Wed Apr 10 21:47:27 UTC 2019


The problem is the following,
with the java compiler intrinsic of amber, String.format() is optimized using invokedynamic in the case the format (the first argument) is constant (and some other conditions on the format), this is great, perf are like 25x in simple benchmarks, and that all because in a lot of code, the format is not constant for the Java compiler. 

By example,
  class Logger {
    public static void log(String format, String message) {
      System.err.println(String.format(format, message));
    }
  }
  ...
  logger.log("%s", "hello");

The format is not a constant inside Logger::log for the Java compiler but when the code is JITed, due to inlining, logger.log("hello") calls String.format() with a constant.


I propose a way to fix that, by providing a method handle combiner (guardIfConstant) that detects if an argument is a constant and do something different if it's a constant or not.
It's a little more complex than that, we don't only want to have a separate path if the argument is a constant, we also want to be able to build a method handle tree depending on the value of that constant.

  MethodHandle guardIfConstant(int argument, MethodHandle targetProvider, MethodHandle fallback)

the semantics: if the nth argument is a constant, the target provider is called with that argument and the return value, a method handle, is called with all the arguments, otherwise the fallback is called.

in term of method type:
  - the method type of the return value of guardIfConstant is the same as fallback
  - the method type of targetProvider returns a MethodHandle and takes a single parameter which is the nth parameter type of the fallback method type,
    the returned method handle as to have the same method type as the fallback.

Rémi




More information about the valhalla-dev mailing list