TOWARDS A UNIVERSAL
VIRTUAL MACHINE

Alex Buckley           John Rose
Specification Lead,    Lead Engineer,
Java Language ...
Overview
• The Java Virtual Machine (JVM) has, in large part,
  been the engine behind the success of the Java
  language
...
What is a virtual machine?
• A software implementation of a computer
  architecture
• Some virtual machines simulate a who...
4
“Java is slow because it runs on a VM”
• Early implementations
  of the JVM executed
  bytecode with an
  interpreter [slo...
“Java is fast because it runs on a VM”
                    • Major breakthrough was
                      the advent of “J...
Common JVM optimizations
compiler tactics                          language-specific techniques              loop transfor...
Inlining is the Über-Optimization
• Speeding up method calls is the big win
• For a given method call, try to predict whic...
Optimizations are universal
• Optimizations work on bytecode in .class files
• A compiler for any language – not just Java...
The Great Ruby Shootout 2008




                                                                            2.00
        ...
Languages ♥ Virtual Machines
• Programming languages need runtime support
  >   Memory management / Garbage collection
  >...
Languages ♥ The JVM                                                                  Tea               iScript
 Zigzag    ...
Benefits for the developer
• Choice
  > Use the right tool for the right job, while sharing
    infrastructure
  > Unit te...
Trends in programming languages




          Data courtesy of TIOBE: www.tiobe.com   14
Is the JVM a
Universal VM?




                15
Different kinds of languages




           Data courtesy of TIOBE: www.tiobe.com   16
Fibonacci in Java and Ruby

                                  def fib(n) {
int fib(int n) {
                              ...
Not as similar as they look
• Data types
  > Not just char/int/long/double and java.lang.Object

• Collections
  > Not jus...
Java language                Ruby language
                        fictions                     fictions

                ...
Towards a Universal VM
• Simulating language features at runtime is slow
• When multiple languages target a VM, common
  i...
If we could make one change
to the JVM to improve life for
dynamic languages, what
would it be?

More flexible method call...
More flexible method calls
• The invokevirtual bytecode performs a method call
• Its behavior is Java-like and fixed
• Oth...
invokevirtua
Caller                  Method




                                 23
Language logic

invokedynami                           invokevirtua


          Caller                    Method




     ...
Language logic

invokedynami                                           invokevirtua


           Caller                   ...
invokevirtua
                       JRuby logic
  invokedynami
                          Jython logic
JRuby caller

      ...
Language logic is only needed...



ONCE
(Until a different object is assigned to the receiver variable,
or the receiver's...
Bootstrap methods
• The first time the JVM executes
   invokedynamic Object.lessThan(Object)boolean
  It consults a bootst...
It's as if invokedynamic never existed...


                         invokevirtua
  JRuby caller

                        ...
We're basically a direct
participant in the JVM's method
selection and linking process.
So cool.

Charles O. Nutter, JRuby...
JVM Specification, 1997
The Java Virtual Machine knows nothing about the Java programming
language, only of a particular b...
A budget of invokes

invokestatic invokespecia
                   l        invokevirtual     invokeinterface invokedynamic...
Optimizing invokedynamic
• Don't want it to be slow for the first 10 years
• Most of the current optimization framework ap...
JSR 292
• invokedynamic
• Interface injection
• Tail calls
• Continuations
• Hotswap




                        34
A Universal VM is in sight
• Where all languages run faster
  > Less overhead for their implementers
  > Direct applicatio...
What about Java (the language) ?
• Java depends on static types for method calls
• Code in dynamic languages has no static...
Innovation never stops
• CMT machines are here, but the software isn't
• Cheap CPU cycles make scripting practical
• New l...
Resources
• John Rose (JSR 292 spec lead)
  > http://blogs.sun.com/jrose

• Multi-Language Virtual Machine OpenJDK project...
THANK YOU

Alex Buckley           John Rose
alex.buckley@sun.com   john.rose@sun.com


                                   ...
Upcoming SlideShare
Loading in...5
×

Oow 2009 Towards A Universal Vm

566

Published on

Overview
• The Java Virtual Machine (JVM) has, in large part,
been the engine behind the success of the Java
language
• In years to come, it will power the success of other
languages too

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
566
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Oow 2009 Towards A Universal Vm"

  1. 1. TOWARDS A UNIVERSAL VIRTUAL MACHINE Alex Buckley John Rose Specification Lead, Lead Engineer, Java Language and VM Multi-Language Virtual Machine Sun Microsystems Sun Microsystems 1
  2. 2. Overview • The Java Virtual Machine (JVM) has, in large part, been the engine behind the success of the Java language • In years to come, it will power the success of other languages too 2
  3. 3. What is a virtual machine? • A software implementation of a computer architecture • Some virtual machines simulate a whole machine > VMWare, VirtualBox, VirtualPC, Parallels • Some virtual machines host a single application > Java Virtual Machine, .NET Common Language Runtime > Usually implement a custom instruction set chosen to suit general applications > Application is isolated from the Operating System and from applications in other virtual machines 3
  4. 4. 4
  5. 5. “Java is slow because it runs on a VM” • Early implementations of the JVM executed bytecode with an interpreter [slow] 5
  6. 6. “Java is fast because it runs on a VM” • Major breakthrough was the advent of “Just In Time” compilers [fast] > Compile from bytecode to machine code at runtime > Optimize using information available at runtime only • Simplifies static compilers > javac and ecj generate “dumb” bytecode and trust the JVM to optimize > Optimization is real, even if it is invisible 6
  7. 7. Common JVM optimizations compiler tactics language-specific techniques loop transformations delayed compilation class hierarchy analysis loop unrolling tiered compilation devirtualization loop peeling on-stack replacement symbolic constant propagation safepoint elimination delayed reoptimization autobox elimination iteration range splitting program dependence graph representation escape analysis range check elimination static single assignment representation lock elision loop vectorization proof-based techniques lock fusion global code shaping exact type inference de-reflection inlining (graph integration) memory value inference speculative (profile-based) techniques global code motion memory value tracking optimistic nullness assertions heat-based code layout constant folding optimistic type assertions switch balancing reassociation optimistic type strengthening throw inlining operator strength reduction optimistic array length strengthening control flow graph transformation null check elimination untaken branch pruning local code scheduling type test strength reduction optimistic N-morphic inlining local code bundling type test elimination branch frequency prediction delay slot filling algebraic simplification call frequency prediction graph-coloring register allocation common subexpression elimination memory and placement transformation linear scan register allocation integer range typing expression hoisting live range splitting flow-sensitive rewrites expression sinking copy coalescing conditional constant propagation redundant store elimination constant splitting dominating test detection adjacent store fusion copy removal flow-carried type narrowing card-mark elimination address mode matching dead code elimination merge-point splitting instruction peepholing DFA-based code generator 7
  8. 8. Inlining is the Über-Optimization • Speeding up method calls is the big win • For a given method call, try to predict which method should be called • Numerous techniques available > Devirtualization (prove there's one target method) > Monomorphic inline caching > Profile-driven inline caching • Goal is inlining: copying method body into the caller > Gives more code for the optimizer to chew on 8
  9. 9. Optimizations are universal • Optimizations work on bytecode in .class files • A compiler for any language – not just Java – can produce a .class file • All languages can benefit from dynamic compilation and optimizations like inlining 9
  10. 10. The Great Ruby Shootout 2008 2.00 means “twice as fast” 0.50 means “half the speed” http://antoniocangiano.com/2008/12/09/the-great-ruby-shootout-december- 2008/ 10
  11. 11. Languages ♥ Virtual Machines • Programming languages need runtime support > Memory management / Garbage collection > Concurrency control > Security > Reflection / Debugging / Profiling > Standard libraries (collections, database, XML) • Traditionally, language implementers coded these features themselves • Today, many implementers choose to target a virtual machine to reuse its infrastructure 11
  12. 12. Languages ♥ The JVM Tea iScript Zigzag JESS Jickle Modula-2 Lisp Nice CAL JavaScript Correlate JudoScript Simkin Drools Basic Icon Groovy Eiffel Luck v-language Pascal Prolog Mini Tcl PLAN Hojo Scala Rexx JavaFX Script Funnel FScript Tiger Anvil Yassl Oberon E Smalltalk Logo Tiger JHCR JRuby Ada G Scheme Sather Clojure Processing Dawn Phobos Sleep WebL TermWare LLP Pnuts Bex Script BeanShell Forth PHP C# SALSA Yoix ObjectScript Jython Piccola 12
  13. 13. Benefits for the developer • Choice > Use the right tool for the right job, while sharing infrastructure > Unit tests in Scala, business logic in Java, web app in JRuby, config scripts in Jython... > …with the same IDE, same debugger, same JVM • Extensibility > Extend a Java application with a Groovy plugin • Manageability > Run RubyOnRails with JRuby on a managed JVM 13
  14. 14. Trends in programming languages Data courtesy of TIOBE: www.tiobe.com 14
  15. 15. Is the JVM a Universal VM? 15
  16. 16. Different kinds of languages Data courtesy of TIOBE: www.tiobe.com 16
  17. 17. Fibonacci in Java and Ruby def fib(n) { int fib(int n) { if n<2 if (n<2) n return n; else else fib(n-1)+fib(n-2) return fib(n-1)+fib(n-2); end } } 17
  18. 18. Not as similar as they look • Data types > Not just char/int/long/double and java.lang.Object • Collections > Not just java.util.* • Method call > Not just Java-style overloading and overriding • Control structures > Not just 'for', 'while', and 'break' 18
  19. 19. Java language Ruby language fictions fictions Checked exceptions Open classes Generics Dynamic typing Enums 'eval' Java VM Overloading Closures features Constructor chaining Mixins Program analysis Regular expressions Primitive types+ops Primitive types+ops Primitive types+ops Object model Object model Object model Memory model Memory model Memory model Dynamic linking Dynamic linking Dynamic linking Access control Access control Access control GC GC GC Unicode Unicode Unicode 19
  20. 20. Towards a Universal VM • Simulating language features at runtime is slow • When multiple languages target a VM, common issues quickly become apparent > JVM Language Summits in 2008 and 2009 • With expertise and taste, the JVM's features can grow to benefit all languages > Adding a little more gains us a lot! > Each additional “stretch” helps many more languages 20
  21. 21. If we could make one change to the JVM to improve life for dynamic languages, what would it be? More flexible method calls 21
  22. 22. More flexible method calls • The invokevirtual bytecode performs a method call • Its behavior is Java-like and fixed • Other languages need custom behavior • Idea: Let some “language logic” determine the behavior of a JVM method call • Invention: the invokedynamic bytecode > VM asks some “language logic” how to call a method > Language logic decides if it needs to stay in the loop 22
  23. 23. invokevirtua Caller Method 23
  24. 24. Language logic invokedynami invokevirtua Caller Method 24
  25. 25. Language logic invokedynami invokevirtua Caller Method • Check which methods are available now in each class [open classes] • Check the dynamic types of arguments to the method [multimethods] • Rearrange and inject arguments [optional and default parameters] • Convert numbers to a different representation [fixnums] 25
  26. 26. invokevirtua JRuby logic invokedynami Jython logic JRuby caller Groovy logic Jython caller Groovy caller Method 26
  27. 27. Language logic is only needed... ONCE (Until a different object is assigned to the receiver variable, or the receiver's dynamic type is changed, or the arguments' dynamic types are changed) 27
  28. 28. Bootstrap methods • The first time the JVM executes invokedynamic Object.lessThan(Object)boolean It consults a bootstrap method in the language logic to discover which method should be called • Suppose the answer is “Integer.compare(Long)BOOL” The JVM associates that method with the invokedynamic instruction • The next time the JVM executes invokedynamic Object.lessThan(Object)boolean It jumps to the previously chosen method immediately • No language logic is involved • Now the JVM knows the target method, it can start inlining! 28
  29. 29. It's as if invokedynamic never existed... invokevirtua JRuby caller invokevirtua Jython caller Method invokevirtua Groovy caller 29
  30. 30. We're basically a direct participant in the JVM's method selection and linking process. So cool. Charles O. Nutter, JRuby lead 30
  31. 31. JVM Specification, 1997 The Java Virtual Machine knows nothing about the Java programming language, only of a particular binary format, the class file format. A class file contains Java Virtual Machine instructions (or bytecodes) and a symbol table, as well as other ancillary information. Any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine. Attracted by a generally available, machine-independent platform, implementors of other languages are turning to the Java Virtual Machine as a delivery vehicle for their languages. In the future, we will consider bounded extensions to the Java Virtual Machine to provide better support for other languages. 31
  32. 32. A budget of invokes invokestatic invokespecia l invokevirtual invokeinterface invokedynamic receiver receiver receiver no receiver no receiver class class interface no dispatch no dispatch single dispatch single dispatch custom dispatch B8 nn nn B7 nn nn B6 nn nn B9 nn nn aa 00 BA nn nn 00 00 32
  33. 33. Optimizing invokedynamic • Don't want it to be slow for the first 10 years • Most of the current optimization framework applies! • Every invokedynamic links to one target method so inlining is possible • Complex language logic can also be inlined • A JVM implementation can even assist with speculative optimizations that are language-specific 33
  34. 34. JSR 292 • invokedynamic • Interface injection • Tail calls • Continuations • Hotswap 34
  35. 35. A Universal VM is in sight • Where all languages run faster > Less overhead for their implementers > Direct application of existing JVM optimizations > Exploit the centuries of programmer effort invested in JVM implementations to make their bytecodes zoom! • Where all languages are first-class citizens > Larger community working to improve the JVM > Virtuous circle of ideas and prototypes 35
  36. 36. What about Java (the language) ? • Java depends on static types for method calls • Code in dynamic languages has no static types • How can Java code access libraries written in dynamic languages without losing type safety? > “Punch a hole” in the Java type system? > All access through privileged libraries? > Many details to consider, such as checked exceptions • We are moving cautiously > Java Language Rule #0: “First, do no harm” 36
  37. 37. Innovation never stops • CMT machines are here, but the software isn't • Cheap CPU cycles make scripting practical • New languages and APIs will continue to appear • The JVM will continue to play a central role 37
  38. 38. Resources • John Rose (JSR 292 spec lead) > http://blogs.sun.com/jrose • Multi-Language Virtual Machine OpenJDK project > http://openjdk.java.net/projects/mlvm • JVM Language Summit, September 2009 > http://www.jvmlangsummit.com • “JVM Languages” Google Group > http://groups.google.com/group/jvm-languages 38
  39. 39. THANK YOU Alex Buckley John Rose alex.buckley@sun.com john.rose@sun.com 39
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×