The Source for Java Technology Collaboration
User: Password:



Rémi Forax's Blog

Rémi Forax Rémi Forax is Maitre de Conférence at University of Marne-la-Vallée since 2003 where he obtained his PhD on multi-method implemntation in Java. He has been using Java for many years and enjoys himself hacking the JDK.



JSR292 backport - First release

Posted by forax on July 01, 2009 at 09:06 AM | Permalink | Comments (0)

I've just released the first version 1.0.1 (1.0 is compiled for 1.6 only) of the JSR292 backport. So you can now test invokedynamic and method handle invocations without using the latest patch queue from mlvm repository and some wizard's black magic.

The primary usage of the backport is for dynamic language runtime developers (JRuby, Jython, Groovy, etc), they can now use JSR292 API to improve their perf without having to maintain different codebases.
Even if I have no proof, I strongly believe that the JSR2 proposed design will improve performance of JVM languages runtimes.

The backport (at least currently) is a Java agent which modify (instrument) bytecodes at runtime to provide JSR 292 API support on jdk11.5/1.6 VM.
The backport embodies a kind of JIT, i.e the backport agent is able to detect hot call sites and recompile this call sites to a better bytecode blob at runtime.

Comparing to current state of JDK7, the backport run at the same speed or faster than JDK7 depending on the benchmark used. The twist is that currently the JDK7 doesn't provide a full JIT support, that's why the backport is faster :)
This assertion will change soon because I know that the JIT support is around the corner.

Anyway, the JSR292 API provides you an infrastructure that is known to be good in order to be optimized by the VM. So If you are a language runtime developer, I think it worth a glimpse.

How to get JSR292 backport ?

The JSR292 backport is hosted by the JVM language runtime project on Google Code:

  http://code.google.com/p/jvm-language-runtime/.

How to use it ?

You have an example in directory, examples.
The javadoc of JSR 292 is available here (the one included with jdk7 is a bit outdated).
To compile it with latest JDK7 javac
and run it with your favorite VM.

[examples]$ /usr/jdk/jdk1.7.0/bin/javac -Xbootclasspath/p:../mock/jsr292-mock.jar -source 1.7 FidgetDemo.java 
[examples]$ java -javaagent:../lib/jsr292-backport.jar FidgetDemo
Fidgety self-modifying call site...
--- loop #0
[link] new call site: CallSite#970110[fidget(java.lang.String)java.lang.String => null]
[link] set target to Guard:invoke(class java.lang.String itch[class java.lang.String])
fred can't get comfortable
buster can't get comfortable
ricky can't get comfortable
--- loop #1
[link] set target to Guard:invoke(class java.lang.String fuss[class java.lang.String])
fred can't get comfortable
buster is feeling moody
ricky is feeling moody
--- loop #2
fred is feeling moody
[link] set target to Guard:invoke(class java.lang.String bore[class java.lang.String])
buster is feeling moody
ricky needs a change of scenery
--- loop #3
fred needs a change of scenery
buster needs a change of scenery
[link] set target to Guard:invoke(class java.lang.String itch[class java.lang.String])
ricky needs a change of scenery
--- loop #4
fred can't get comfortable
buster can't get comfortable
ricky can't get comfortable
--- loop #5
[link] set target to Guard:invoke(class java.lang.String fuss[class java.lang.String])
fred can't get comfortable
buster is feeling moody
ricky is feeling moody

If you want more examples, JSR292 Cookbook is your friend.
If you want more info about the JSR292 backport, known bugs (none) or limitations (some), you can read the README.txt.

invokedynamically yours,
Rémi



ASM now supports invokedynamic

Posted by forax on June 11, 2009 at 10:42 PM | Permalink | Comments (2)

Great news, ASM 3.2 is released (Extended changelog).
This new version includes the support of the new bytecode instruction : invokedynamic, introduced by JSR292.

ASM supports of invokedynamic

From ASM point of view, invokedynamic is a method call instruction emittable/trappable by calling/overriding visitInsnMethod of a MethodVisitor like any other invoke* bytecode instructions.
But because invokedynamic is a function call i.e a call without any receiver, a special fake owner name Opcodes.INVOKE_DYNAMIC_OWNER was introduced to stand as owner of the invokedynamic call instruction.
This design allows ASM to handle invokedynamic without breaking all already existing codes written using ASM 3.x.

Example

This simple code transforms all invokevirtual to invokedynamic, registers a boostrap method and configure invokedynamic call site to perform an invokevirtual.

public class Invokedynamiker extends ClassAdapter {
  boolean isStaticInitPatched;
  
  public Invokedynamiker(ClassVisitor cv) {
    super(cv);
  }
  
  @Override
  public MethodVisitor visitMethod(int access, final String name, final String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    return new MethodAdapter(mv) {
      @Override
      public void visitCode() {
        super.visitCode();
        if ("".equals(name)) {
          generateClinitProlog(mv);
          isStaticInitPatched = true;
        }
      }
      
      @Override
      public void visitMethodInsn(int opcode, String owner, String name, String desc) {
        if (opcode == Opcodes.INVOKEVIRTUAL) {
          opcode = Opcodes.INVOKEDYNAMIC;
          desc = "(L"+owner+';'+desc.substring(1);
          owner = Opcodes.INVOKEDYNAMIC_OWNER;
        }
        super.visitMethodInsn(opcode, owner, name, desc);
      }
    };
  }
  
  @Override
  public void visitEnd() {
    if (!isStaticInitPatched) {
      MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "", "()V", null, null);
      mv.visitCode();
      generateClinitProlog(mv);
      mv.visitMaxs(0, 0);
      mv.visitEnd();
    }
    super.visitEnd();
  }
  
  void generateClinitProlog(MethodVisitor mv) {
    mv.visitLdcInsn(Type.getObjectType("asm/indy/sample/Bootstrap"));
    mv.visitLdcInsn("bootstrapMethod");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/dyn/Linkage",
        "registerBootstrapMethod", "(Ljava/lang/Class;Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.RETURN);
  }
}

The boostrap method creates a call site and links the call site target to a method handles performing an invokevirtual.

public static CallSite bootstrapMethod(Class<?> callerClass, String methodName, MethodType methodType) {
    MethodType argumentType = methodType.dropParameterType(0);
    MethodHandle mh = MethodHandles.lookup().findVirtual(methodType.parameterType(0), methodName, argumentType);
    CallSite site = new CallSite(callerClass, methodName, methodType);
    site.setTarget(mh);
    return site;
  }

The code of the bootstrap method can be changed to, by example, log the arguments before calling the destination method using method handle adapters : convertArguments, spreadArguments, collectArguments and a java.dyn.JavaMethodHandle.
This exercise is left to the reader :)

The whole code is available as a zip : indy-asm-sample.zip.
To run it, download the latest binary of jdk7 (at least b59) and run the ant script (ant run).

cheers,
Rémi (ASM team mate)



JDK7 Milestones

Posted by forax on May 04, 2009 at 07:41 AM | Permalink | Comments (6)

It seems that lot of people don't notice that there is a roadmap for jdk7.

http://openjdk.java.net/projects/jdk7/milestones/.

Rémi



invokedynamic now lives in JDK7

Posted by forax on April 25, 2009 at 07:11 AM | Permalink | Comments (7)

invokedynamic (at least the main parts) now lives in JDK 7 !

You haven't perhaps notice it but John pushes the first patches from the Da Vinci Machine project to the hotspot workspace.

Method Handle support (meth):
http://hg.openjdk.java.net/jdk7/hotspot-comp/hotspot/rev/e5b0439ef4ae
 
Invokedynamic support (indy):
http://hg.openjdk.java.net/jdk7/hotspot-comp/hotspot/rev/be93aad57795

Rémi

For early adopters, on Linux you can build the source of the VM by cloning the hotspot workspace, on MacOs X (Intel CPU), Atilla Szegedi gently provides an already built binary.



July 2009
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  


Search this blog:
  

Categories
Community: JavaDesktop
Community: JDK
Swing
Archives

July 2009
June 2009
May 2009
April 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
July 2008
May 2008
April 2008
March 2008
February 2008
January 2008
November 2007
October 2007
September 2007
August 2007
June 2007
May 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006

Recent Entries

JSR292 backport - First release

ASM now supports invokedynamic

JDK7 Milestones



Powered by
Movable Type 3.01D


 Feed java.net RSS Feeds