invokedynamic under the hood

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

MethodHandle implemention tips and tricks

JSR 292 Cookbook: Fresh Recipes with New Ingredients

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

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

<Insert Picture Here> Implementing lambda expressions in Java

JSR 292 and companions Rémi Forax FOSDEM'09

Let's talk about invokedynamic

Bytecodes Meet Combinators

2012 Oracle Corporation. Monday, July 30, 12

Optimizing Jython using invokedynamic and Gradual Typing

One VM, Many Languages

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

Static Analysis of Dynamic Languages. Jennifer Strater

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

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

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

Towards Performance Measurements for the Java Virtual Machine s invokedynamic

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

Invokedynamic in Practice. Adding invokedynamic support to JRuby

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

InvokeDynamic support in Soot

Run-time Program Management. Hwansoo Han

State of java.lang.invoke Implementation in OpenJDK

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Java: framework overview and in-the-small features

Invokedynamic: Deep Dive

MethodHandle Introspection: Internals

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

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

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

The Java Language Implementation

TOWARDS A UNIVERSAL VIRTUAL MACHINE

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS263: Runtime Systems Lecture: High-level language virtual machines

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

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

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

Topic 9: Type Checking

Topic 9: Type Checking

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

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

Data Abstraction. Hwansoo Han

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

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

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

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

The name of the Show What will you enjoy learning?

Certification In Java Language Course Course Content

Inheritance, Polymorphism and the Object Memory Model

Method Handles Everywhere! Charles Oliver

15CS45 : OBJECT ORIENTED CONCEPTS

Just In Time Compilation

Delft-Java Link Translation Buffer

Optimization Techniques

Rhino & RingoJS: JavaScript on the JVM

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Roos Instruments, Inc. RTALK - SMALLTALK ON THE JVM

CSc 453 Interpreters & Interpretation

Object Oriented Programming with Java. Unit-1

Java 8 Programming for OO Experienced Developers

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Overview of Java s Support for Polymorphism

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

1 Shyam sir JAVA Notes

Lecture Notes on Programming Languages

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

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

MHDeS: Deduplicating Method Handle Graphs for Efficient Dynamic JVM Language Implementations

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Program Dynamic Analysis. Overview

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

Class, Variable, Constructor, Object, Method Questions

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Method Resolution Approaches. Dynamic Dispatch

Java Primer 1: Types, Classes and Operators

Bytecode Manipulation Techniques for Dynamic Applications for the Java Virtual Machine

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

SABLEJIT: A Retargetable Just-In-Time Compiler for a Portable Virtual Machine p. 1

Static Typing or Dynamic Typing: A Real Issue or a Simple Case of Tribalism

CS 231 Data Structures and Algorithms, Fall 2016

Problem: Too Many Platforms!

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Evolving the Java platform. Ola Bini JRuby Core Developer ThoughtWorks Studios

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

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

Index COPYRIGHTED MATERIAL

Get Unique study materials from

Evolution of Programming Languages

Pace University. Fundamental Concepts of CS121 1

Where do we go from here?

In Vogue Dynamic. Alexander Shopov

Core JAVA Training Syllabus FEE: RS. 8000/-

Transcription:

Nadeesh T V ORACLE India Pvt Ltd 26 Aug 2016

Outline 1 JVM Languages 2 PreInvokedynamic 3 Invokedynamic 4 MethodHandle 5 Summary

JVM Languages Languages which can run on Java Virtual Machine (JVM) Should be a valid class file containing byte codes More than 200+ JVM languages [1] Java (primary language) [2] JRuby (ported version of Ruby) Jython (ported version of Python) JavaScript Groovy Smalltalk Scala etc...

JVM Languages Semantics of other JVM languages differ from Java in many ways Types can be dynamic d e f sum ( a, b ) : t o t a l = a + b r e t u r n t o t a l ; Method invocation can be different Smalltalk send messages for invocation Scope, access rules can be different

Non Java Language s Overhead Figure: NonJava languages on JVM Wrapper layer adds performance overheads while running on JVM!!!

NonJava Language s Pain Points NonJava compiler implementors have to write lot of code JVM have to load lot of byte code (performance overhead) Some of the dynamic languages heavily use Reflection (performance overhead) [3] Every invocation requires an access check Method arguments are objects - boxing/unboxing

NonJava Language s Pain Points Wrapper layer act as a black box which can affect JIT inlining!!! v o i d method1 ( b o o l e a n arg ) { i f ( arg ) { do..... } e l s e { do... } } method1 ( f a l s e )

Types of Method Invocations Before Java 7 invokestatic - Used to call static methods s t a t i c v o i d s t a t i c M e t h o d ( ) { } A. s t a t i c M e t h o d ( ) ; Byte code of above invocation will be i n v o k e s t a t i c #7 //Method s t a t i c M e t h o d : ( ) V

Types of Method Invocations Before Java 7 invokevirtual - Used to call instance methods v o i d instancemethod ( ) { } o b j. instancemethod ( ) ; Byte code of above invocation will be i n v o k e v i r t u a l #4 // Method instancemethod : ( ) V

Types of Method Invocations Before Java 7 invokeinterface - Use interface reference to call methods L i s t l = new A r r a y L i s t ( ) ; l. add ( 1 ) ; Byte code of above invocation will be i n v o k e i n t e r f a c e #10, 1 // I n t e r f a c e M e t h o d j a v a / u t i l / L i s t. add : ( L j a v a / l a n g / Object )Z

Types of Method Invocations Before Java 7 invokespecial - Used to call private, constructors, super methods p r i v a t e v o i d privatemethod ( ) { } o b j. privatemethod ( ) ; Byte code of above invocation will be i n v o k e s p e c i a l #5 // Method privatemethod : ( ) V

Types of Method Invocations Before Java 7 Figure: Summary of JVM bytecode invocations [2] Receiver - Object in oop Dispatch - Method searching and linking

: Supporting Dynamically Typed Languages on the Java Platform Started in 2005 Feature Completed in 2011 Released in JDK 7 Key Features New byte code - invokedynamic New unit of behavior - methodhandle

invokedynamic Key Features No receiver Custom dispatching under user control Dynamic call sites can be relinked dynamically

BootStrapping 1st time invocation Linking of a Dynamic Callsite (DC) to a method (One time activity) Figure: Flow of BootStrapping [4]

BootStrap Method Accept 3 Compulsory arguments Byte code generator s responsibility to link indy to BSM Linking under user control!!!

BootStrapping 2nd time invocation (No bootstrapping/linking) Figure: Flow of BootStrapping [4]

BootStrap Method - Example Runnable r = ( ) > System. out. p r i n t l n ( ) ; Figure: Lambda expression byte code details Image adapted from [4]

MethodHandle (MH) MethodHandle is a typed, directly executable reference to an underlying method, constructor, field MH can be considered as a function/field pointer

Lookup Contain access permission of a class Should share only with trusted parties Used in creating MethodHandle Two types - publiclookup and lookup In case of indy, JVM will pass lookup object to BSM

MethodHandle - Usage Create using MethodHandles.Lookup (in Java) CONSTANT MethodHandle (in byte code) MethodHandles. Lookup lookup = MethodHandles. p u b l i c L o o k u p ( ) MethodHandle m = lookup. f i n d S t a t i c ( S t r i n g. c l a s s, v a l u e O f, MethodType. methodtype ( S t r i n g. c l a s s, char. c l a s s ) ) ; S t r i n g output = ( S t r i n g ) m. i n v o k e E x a c t ( c ) ;

MethodHandle : Invocation invokeexact Argument type should be exact match If no match, throw WrongMethodTypeException invoke (invokegeneric) Argument should be exact match/type converted match (boxing/unboxing, casting) If no match, throw WrongMethodTypeException

MethodHandle - Lookup Failing Case c l a s s B { p r i v a t e v o i d m( ) {} } p u b l i c c l a s s A { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) throws Throwable { MethodHandle m = MethodHandles. lookup ( ). f i n d S p e c i a l ( B. c l a s s, m, MethodType. methodtype ( v o i d. c l a s s ), B. c l a s s ) ; } } Throws IllegalAccessException

MethodHandle - Advantages Access check only during creation of MH No more access check during usage No more boxing/unboxing overhead due to signature polymorphic invocation

Signature Polymorphism A signature polymorphic method is one which can operate with any of a wide range of call signatures and return types. invoke/invokeexact are signature polymorphic

CallSite (CS) MethodHandle(MH) wrapped into CallSite CS relink/retarget to different MH dynamically (settarget) CS is transparent to JIT and can optimistically inline through it

Summary Figure: Flow of BootStrapping [4]

Acknowledgement I would like to thank Vladimir Ivanov, Jamsheed CM (Oracle, Hotspot team) for helping me in understanding invokedynamic.

References 1. A Renaissance VM: One Platform, Many Languages, JRose, JVMLS 2011 2. http://cr.openjdk.java.net/ jrose/pres/200910-vmil.pdf 3. Invokedynamic for Mere Mortals, David Buck, JavaOne 2015 4. http://cr.openjdk.java.net/ vlivanov/talks/2015- IndyDeepDive.pdf, VIvanov 5. http://blog.headius.com/2008/09/first-taste-ofinvokedynamic.html, CNutter

Thank you

Types Static - types determined at compile time i n t x ; x = 2 ; x = h i ; // E r r o r Dynamic - types determined at run time x = 2 ; x = h i ; // Works

Types Weak - types can be mixed $a =10; $b= a ; $c=$a. $b ; p r i n t $c ; #10 a $a =10; $b= 20 a ; $c=$a+$b ; p r i n t $c ; #30

Types Strong - No implicit type conversion x = 2 ; y = 2 + h i ; // E r r o r References 1. http://www.rubyfleebie.com/ruby-is-dynamically-and-strongly-typed/ 2. http://www.i-programmer.info/programming/theory/1469-type-systemsdemystified-part2-weak-vs-strong.html