invokedynamic IN 45 MINUTES!!! Wednesday, February 6, 13

Similar documents
Let's talk about invokedynamic

invokedynamic under the hood

JSR 292 and companions Rémi Forax FOSDEM'09

Static Analysis of Dynamic Languages. Jennifer Strater

JSR 292 Cookbook: Fresh Recipes with New Ingredients

MethodHandle implemention tips and tricks

April 15, 2009 John R. Rose, Sr. Staff Engineer

Run-time Program Management. Hwansoo Han

Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Invokedynamic in Practice. Adding invokedynamic support to JRuby

JSR 292 backport. (Rémi Forax) University Paris East

Beyond JVM. or, How to Boil the Ocean in Seven Years

<Insert Picture Here> Implementing lambda expressions in Java

Jython Python for the Java Platform. Jim Baker, Committer twitter.com/jimbaker zyasoft.com/pythoneering

Towards Performance Measurements for the Java Virtual Machine s invokedynamic

The following presentation is provided under DHMB license. (Do Not Hurt My Brain). Lecturer is not responsible for the financial and moral

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

MethodHandle Introspection: Internals

jdk.dynalink is here! Attila Szegedi, Fauna Inc.

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

2012 Oracle Corporation. Monday, July 30, 12

MethodHandlesArrayElementGetterBench.testCreate Analysis. Copyright 2016, Oracle and/or its affiliates. All rights reserved.

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Optimizing Jython using invokedynamic and Gradual Typing

Dynalink. Dynamic Linker Framework for Languages on the JVM. Attila Szegedi, Software Engineer, Twitter

Java Class Loading and Bytecode Verification

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

In Vogue Dynamic. Alexander Shopov

Bytecodes Meet Combinators

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Compiler construction 2009

State of java.lang.invoke Implementation in OpenJDK

02 B The Java Virtual Machine

InvokeDynamic support in Soot

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Method Handles Everywhere! Charles Oliver

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16

Generating Efficient Code for Lambdas and Function Types. Fredrik Öhrström Principal Member of Technical Staff JRockit+Hotspot...

Invokedynamic: Deep Dive

Roos Instruments, Inc. RTALK - SMALLTALK ON THE JVM

The Java Language Implementation

Space Exploration EECS /25

Delft-Java Link Translation Buffer

Program Dynamic Analysis. Overview

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

Delft-Java Dynamic Translation

Java TM. Multi-Dispatch in the. Virtual Machine: Design and Implementation. Computing Science University of Saskatchewan

code://rubinius/technical

Taming the Java Virtual Machine. Li Haoyi, Chicago Scala Meetup, 19 Apr 2017

JRuby: Bringing Ruby to the JVM

Advances in Programming Languages: Generics, interoperability and implementation

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

Overview of Java s Support for Polymorphism

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

Problem: Too Many Platforms!

CSc 520 Principles of Programming Languages

Adopting the JVM. Ola Bini computational metalinguist onsdag, 2010 november 03

Improving the efficiency of adaptive middleware based on dynamic AOP

Introduction Basic elements of Java

<Insert Picture Here> Adventures in JSR-292 or How To Be A Duck Without Really Trying

CSc 453. Compilers and Systems Software. 18 : Interpreters. Department of Computer Science University of Arizona. Kinds of Interpreters

LaboratoriodiProgrammazione III

Bugs in software. Using Static Analysis to Find Bugs. David Hovemeyer

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Java: framework overview and in-the-small features

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

The Java Virtual Machine

Design and Implementation of Pep, a Java Just-In-Time Translator

Is Exploitation Over? Bypassing Memory Protections in Windows 7

Compiling Techniques

Code Generation Introduction

TOWARDS A UNIVERSAL VIRTUAL MACHINE

CSc 553. Principles of Compilation. 2 : Interpreters. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages. Getting started... Getting started. B: Java Bytecode BCEL

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

JavaSplit. A Portable Distributed Runtime for Java. Michael Factor Assaf Schuster Konstantin Shagin

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

JAM 16: The Instruction Set & Sample Programs

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

CSc 620 Debugging, Profiling, Tracing, and Visualizing Programs. Getting started... Getting started. 2 : Java Bytecode BCEL

CS11 Java. Fall Lecture 1

Improving Java Code Performance. Make your Java/Dalvik VM happier

Flow-sensitive rewritings and Inliner improvements for the Graal JIT compiler

CSc 620 Debugging, Profiling, Tracing, and Visualizing Programs

Jaos - Java on Aos. Oberon Event 03 Patrik Reali

Special Topics: Programming Languages

Compiling for Different Platforms. Problem: Too Many Platforms! Dream: Platform Independence. Java Platform 5/3/2011

One VM, Many Languages

Bytecode Manipulation Techniques for Dynamic Applications for the Java Virtual Machine

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

CSC 4181 Handout : JVM

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

Static Program Analysis

Running R Faster. Tomas Kalibera

Golo, a Dynamic, Light and Efficient Language for Post-Invokedynamic JVM

Improving Java Performance

Transcription:

invokedynamic IN 45 MINUTES!!!

Me Charles Oliver Nutter headius@headius.com, @headius blog.headius.com JRuby Guy at Sun, Engine Yard, Red Hat JVM enthusiast, educator, contributor Earliest adopter of invokedynamic

27 April, 2011

History JVM authors mentioned non-java languages Language authors have targeted JVM Hundreds of JVM languages now But JVM was a mismatch for many of them Usually required tricks that defeated JVM optimizations Or required features JDK could not provide

JVM Languages Through the Years Early impls of Python (JPython), JS (Rhino) and many others No official backing by Sun until 2006 JRuby team hired JSR-292 invokedynamic rebooted in 2007 Java 7 shipped invokedynamic in 2011

What is invokedynamic

New Bytecode? Well, yes...but what does that mean? And is that all?

New form of invocation? That s one use, but there are many others

Only for Dynamic Languages? Dynamic dispatch is a common use, but there are many others

A User-definable Bytecode You decide how the JVM implements it

A User-definable Bytecode You decide how the JVM implements it + Method Pointers and Adapters Faster than reflection, with user-defined argument, flow, and exception handling

invokedynamic

user-def d bytecode invokedynamic

user-def d bytecode method pointers invokedynamic

user-def d bytecode + bytecode bootstrap method MethodHandles pointers invokedynamic

https://github.com/headius/indy_deep_dive

MethodHandles

Method Handles Function/field/array pointers Argument manipulation Flow control Optimizable by the JVM This is very important

java.lang.invoke MethodHandle An invokable target + adaptations MethodType Representation of args + return type MethodHandles Utilities for acquiring, adapting handles

MethodHandles.Lookup Method pointers findstatic, findvirtual, findspecial, findconstructor Field pointers findgetter, findsetter, findstaticgetter, findstaticsetter

Why Casting? value1 = (String)mh1.invoke("java.home"); mh2.invoke((object)"hello, world"); invoke() is signature polymorphic Call-side types define signature At compile time Signature must match MethodHandle type Or use invokewitharguments

Adapters Methods on j.l.i.methodhandles Argument manipulation, modification Flow control and exception handling Similar to writing your own commandpattern utility objects

Argument Juggling insert, drop, permute filter, fold, cast splat (varargs), spread (unbox varargs)

Flow Control guardwithtest: boolean branch condition, true path, false path Combination of three handles SwitchPoint: on/off branch true and false paths Once off, always off

Exception Handling catchexception body, exception type, handler throwexception Throws Throwable in argument 0

bytecode

Goals of JSR 292 A user-definable bytecode Full freedom to define VM behavior Fast method pointers + adapters Optimizable like normal Java code Avoid future modifications

JVM 101

JVM 101 200 opcodes

JVM 101 200 opcodes Ten (or 16) endpoints

JVM 101 Invocation invokevirtual invokeinterface invokestatic invokespecial 200 opcodes Ten (or 16) endpoints

JVM 101 200 opcodes Ten (or 16) endpoints Invocation invokevirtual invokeinterface invokestatic invokespecial Field Access getfield setfield getstatic setstatic

JVM 101 200 opcodes Ten (or 16) endpoints Invocation invokevirtual invokeinterface invokestatic invokespecial Field Access getfield setfield getstatic setstatic Array Access *aload *astore b,s,c,i,l,d,f,a

JVM 101 200 opcodes Ten (or 16) endpoints Invocation invokevirtual invokeinterface invokestatic invokespecial Field Access getfield setfield getstatic setstatic Array Access *aload *astore b,s,c,i,l,d,f,a All Java code revolves around these endpoints Remaining ops are stack, local vars, flow control, allocation, and math/boolean/bit operations

The Problem JVM spec (pre-7) defined 200 opcodes All bytecode lives within these 200 What if your use case does not fit? Dynamic language/dispatch Lazy initialization Non-Java features

// Static System.currentTimeMillis() Math.log(1.0) // Virtual "hello".touppercase() System.out.println() // Interface mylist.add("happy happy") myrunnable.run() // Special new ArrayList() super.equals(other)

// Static invokestatic java/lang/system.currenttimemillis:()j invokestatic java/lang/math.log:(d)d // Virtual invokevirtual java/lang/string.touppercase:()ljava/lang/string; invokevirtual java/io/printstream.println:()v // Interface invokeinterface java/util/list.add:(ljava/lang/object;)z invokeinterface java/lang/runnable.add:()v // Special invokespecial java/util/arraylist.<init>:()v invokespecial java/lang/object.equals:(java/lang/object)z

invokestatic invokevirtual invokeinterface invokespecial

invokevirtual 1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Look up method on Java class 4. Cache method 5. Invoke method invokestatic 1. Confirm arguments are of correct type 2. Look up method on Java class 3. Cache method 4. Invoke method invokeinterface 1. Confirm object s type implements interface 2. Confirm arguments are of correct type 3. Look up method on Java class 4. Cache method 5. Invoke method invokespecial 1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Confirm target method is visible 4. Look up method on Java class 5. Cache method 6. Invoke method

invokevirtual 1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Look up method on Java class 4. Cache method 5. Invoke method invokestatic 1. Confirm arguments are of correct type 2. Look up method on Java class 3. Cache method 4. Invoke method invokeinterface 1. Confirm object s type implements interface 2. Confirm arguments are of correct type 3. Look up method on Java class 4. Cache method 5. Invoke method invokespecial 1. Confirm object is of correct type 2. Confirm arguments are of correct type 3. Confirm target method is visible 4. Look up method on Java class 5. Cache method 6. Invoke method invokedynamic 1. Call bootstrap handle (your code) 2. Bootstrap prepares CallSite + MethodHandle 3. MethodHandle invoked now and future (until CallSite changes)

invokedynamic bytecode

invokedynamic bytecode bootstrap method

invokedynamic bytecode bootstrap method method handles

invokedynamic bytecode target method bootstrap method method handles

invokedynamic bytecode target method bootstrap method method handles

invokedynamic bytecode target method bootstrap method method handles

All Together Now...

Tools ObjectWeb's ASM library De facto standard bytecode library Jitescript DSL/fluent wrapper around ASM InvokeBinder DSL/fluent API for building MH chains

#1: Trivial Binding Simple binding of a method j.l.i.constantcallsite Method returns String Print out String

#2: Modifying the Site Toggle between two targets each call j.l.i.mutablecallsite Call site sent into methods...so they can modify it Trivial example of late binding InvokeBinder usage

#3: Dynamic Dispatch Target method not known at compile time Dynamically look up after bootstrap Rebind call site to proper method

StupidScript push <string>: push string on stack send <number>: call function with n args One arg call: ["print", "Hello, world"] def <name> '\n' <op1> [ '\n' <op2>...] '\n' end define function with given ops One builtin: print (System.out.println)

StupidScript push <string>: push string on stack send <number>: call function with n args def <name> '\n' <op1> [ '\n' <op2>...] '\n' end One builtin: print (System.out.println)

Implementation Simple parser generates AST Compiler walks AST, emits.class Top level code goes into run() method defs create additional methods

Hello, world! push Hello, world! push print send 1

Define Function def hello push print push Hello, world! send 1 end

Call Function push hello send 0

More Information My blog: http://blog.headius.com Follow me: @headius Code on Github https://github.com/headius/indy_deep_dive Slides posted online