There’s JVM, and that’s it, right? Well, not exactly. Even within JVM, there’s an increasing support for running all kinds of non-Java languages: we have invokedynamic, but it’s being improved, and new layers of functionality are emerging on top of it, making JVM a better home for all kinds of programming languages. There’s life outside of JVM too. JavaScript seems to be a new assembler-lever compilation target even for C programs (I’ll show some amusing examples of what exactly you can run these days in a browser) , and there are some independent efforts at managed runtimes in various stages of completion that seem promising – PyPy, Topaz, Rubinius, Parrot VM (it’s alive again!). This talk is admittedly a language-runtime-enthusiast’s walk-through the things he finds interesting happening this year. Recorded at GeekOut 2013.
2. THE FOLLOWING IS INTENDED TO OUTLINE
OUR GENERAL PRODUCT DIRECTION. FOR
INFORMATION PURPOSES ONLY,AND MAY NOT
BE INCORPORATED INTO ANY CONTRACT. IT
IS NOT A COMMITMENT TO DELIVER ANY
MATERIAL, CODE, OR FUNCTIONALITY,AND
SHOULD NOT BE RELIED UPON IN MAKING
PURCHASING DECISION.THE DEVELOPMENT,
RELEASE,AND TIMING OF ANY FEATURES OR
FUNCTIONALITY DESCRIBED FOR ORACLE'S
PRODUCTS REMAINS AT THE SOLE DISCRETION
OF ORACLE.
4. A virtual machine is…
• a software… (not hardware)
• …designed as runtime environment for other
programs… (open ended)
• …that conform to a particular machine
specification…
• …either existing hardware specification…
• …or entirely synthetic specification.
6. This is a virtual machine
• a software…
• …designed as runtime environment for other
programs… (operating systems, mostly)
• …that conform to a particular machine
specification…
• …in this case, a particular combination of x86
CPU and other goodies (graphics, audio, IO).
7. This is a virtual machine
• This is not the kind of a virtual machine I’m
talking about today.
8. What we’ll survey
today
• JavaVirtual Machine
• Parrot
• Some applications of Low LevelVirtual Machine
• Rubinius
• PyPy
9. There’s lots of ways to
target programs today
• Directly for hardware.
• Compiled for a virtual machine.
• Have it interpreted.
• These are not mutually exclusive.
10. What are benefits of
managed runtimes?
• Automatic memory management
• Object model
• Code transformations
• ...
11. JavaVirtual Machine
• Probably the best knownVM out there.
• Started out as aVM for executing programs in Java.
• Today it has a vibrant ecosystem with myriad
languages around it.
12. JavaVirtual Machine
• Defined as a stack-based machine.
• Built-in concept of classes, objects, and fixed-size
arrays.
• Classes have fixed field layout, with strongly typed
field types.
• Single inheritance for classes.
13. JVM as target for
dynamic languages
• Fixed-layout classes make it harder.
• People end up with hashtables etc.
• Leading to poor interop with Java.
• Your dynamic object doesn’t look like a Java
object to Java program.
14. JVM as target for
dynamic languages
• No multiple dispatch.
• Wait, doesn’t Java have overloaded methods?
• It does, but they’re resolved by the Java
compiler, not by theVM.
• Every language has to figure it out themselves.
• No pluggable dispatch.
15. JVM as target for
dynamic languages
• Where it excels:
• Threading support
• Memory management
• JIT compiler
16. JVM as target for
dynamic languages
• How to fix the deficiencies for dynamic languages?
• Step 1: INVOKEDYNAMIC
• Step 2: high-level operation resolver framework
• This is what I’m doing by day.
18. Invokedynamic lets you
be abstract
Object duck = ...;
duck.quack(); // won’t work in Java!
INVOKEDYNAMIC call:quack(Object)Object
19. Invokedynamic lets you
be abstract
INVOKEINTERFACE quack(Duck)V
vs.
INVOKEDYNAMIC call:quack(Object)Object
• Doesn’t prescribe types
• Doesn’t prescribe dispatch
20. Invokedynamic lets you
be abstract
Duck duck = ...;
Voice voice = duck.voice;
INVOKEINTERFACE getVoice(Duck)Voice
21. Invokedynamic lets you
be abstract
Object duck = ...;
Object voice = duck.voice;
INVOKEDYNAMIC get:voice(Object)Object
22. Invokedynamic lets you
be abstract
INVOKEINTERFACE getVoice(Duck)Voice
vs.
INVOKEDYNAMIC get:voice(Object)Object
• “get” can map to a field getter, array element
getter, or a property method implementation.
• Okay, but what is mapping it?
23. Bootstrap methods
public static CallSite bootstrap(
String operation,
MethodType signature)
• The JVM will invoke it with bootstrap(“get:voice”,
MethodType(Object.class, Object.class))
• We can return a “CallSite” - basically a slot with a
target function pointer.
• Target can be set multiple times.
24. Putting it together
public class Ducks {
public static class ShyDuck {
public void quack() {
System.out.println("...quack?");
}
}
public static class LoudDuck {
public void quack() {
System.out.println("QUACK!");
}
}
public static class ConfusedDuck {
public void quack() {
System.out.println("Woof!");
}
}
}
25. Putting it together
var ducks = [
new org.szegedi.geekout2103.Ducks.ShyDuck(),
new org.szegedi.geekout2103.Ducks.LoudDuck(),
new org.szegedi.geekout2103.Ducks.ConfusedDuck()
]
for each(duck in ducks) {
duck.quack()
}
27. How’s JVM faring so far
• Java 8 will ship Nashorn, a JavaScript-on-the-JVM
• completely INVOKEDYNAMIC based
• in-house dogfooding of INVOKEDYNAMIC
• A lot of other languages, both using Indy and not,
already target the JVM. I hear some are here at this
conference too :-)
• My Dynalink project (on GitHub) helps you reduce
the pain of providing high level operations for your
language.
28. Parrot virtual machine
• Started out as aVM for Perl 6.
• Now it is language agnostic.
• Specifically designed with dynamic languages in
mind.
29. Parrot virtual machine
• Objects are “polymorphic containers” with
changing layout.
• Register based (not stack based as JVM).
• Continuations as primary expression of control
flow.
• Built-in opcodes invokecc, yield, tailcall,
capture_lex, newclosure…
30. Parrot virtual machine
• Cross-level language interop is basically built-in
• getprop, setprop opcodes!
• opcodes for loading languages and registering
compilers!
• Much higher level bytecode operations than
JVM.
• Basically starts out with Indy+Metaobject
Protocols built in.
32. Parrot virtual machine
• PIR is human readable assembly-level language
.sub factorial
# Get input parameter.
.param int n
# return (n > 1 ? n * factorial(n - 1) : 1)
.local int result
if n > 1 goto recurse
result = 1
goto return
recurse:
$I0 = n - 1
result = factorial($I0)
result *= n
return:
.return (result)
.end
33. LLVM
• It’s not actually aVM.
• Used to stand for “Low-LevelVirtual Machine”.
• Today, LLVM is the name of the project, and it’s not
considered an acronym.
• It’s a compiler toolchain project.
38. Emscripten
• LLVM backend for JavaScript
• Anything that compiles to LLVM IR can be
translated to JavaScript.
• Audio, graphics, IO libraries mapped to HTML5
constructs.
42. asm.js
• Mozilla embraced Emscripten’s code generator
output as a JS subset:
• arithmetic operations only
• loads and stores into a single ArrayBuffer
• calls to functions that only take numers and
return number.
43. asm.js
• Force an expression to be double:“+x”
• Force an expression to be int:“x | 0”
function diagonal(x, y) {
x = +x; // x has type double
y = +y; // y has type double
return +sqrt(square(x) + square(y));
}
function add1(x) {
x = x|0; // x : int
return (x+1)|0;
}
44. asm.js
• It’s a statically typed language that only looks like
JavaScript.
• No strings
• No allocation
• No calling of other JS libraries
46. • PyPy is Python runtime using LLVM.
• Has sandboxing, good memory management
• Has stackless mode (ever played EVE Online?)
• Itself largely written in Python!
47.
48. • Toolchain available as separate project RPython.
• Rpython is a statically compilable subset of Python
• Most of PyPy written in it.
• There’s now Topaz: a Ruby on top of PyPy!
• How crazy is that?!
49. • A word on “stackless”
• Execution doesn’t use C stack
• Can have million of micro-threads executing as
coroutines efficiently on few CPUs
• Very useful to program MMOs.
50. • Similar to PyPy, only for Ruby:
• Big part of implementation written in Ruby itself
• Relies on LLVM to efficiently compile to Ruby
• Similar memory management etc. characteristics
51. • Similar to JVM too:
• intermediate format is stack machine with
bytecode
• has high level bytecodes for yield, pushing blocks
etc.
52. • Object representation:
• Has immediates for fixnums, symbols, booleans
• This can be huge deal performance-wise
• JVM-like objects (header, class pointer, fields)
• Generational GC, similar layout to JVM:
• Nursery,Young, Mature generations.
53. Self-hosting
• Recurring theme is to write as much of the
runtime as possible in the runtime’s target
language itself.
• More accessible for language users.
• Reduced bootstrapping core.
• Benefits from its own optimizations.
• Of course, old news in Lisp world.
54. MaxineVM
• Speaking of self hostedVMs…
• Oracle’s open-sourced JavaVM written in Java.
• Pluggable GC and JIT-compiler.
• “Maxine Inspector” for debugging/visualizing
runningVM state.
• Great tool for learning and research ofVM
technologies.
• Great IDE support (Eclipse, NetBeans, IntelliJ)
56. JavaScript runtimes
• Probably biggest innovation hotbed right now.
• Google’sV8, Mozilla’s *Monkey,Apple’s Nitro
• … as well as Oracle’s Nashorn.
57. JavaScript is ideal
• JavaScript is ideal language to get paid to work on
it.
• Extreme dynamism ensures there’s no single
correct optimization approach.
• Got to try them all!
58. JS optimization
strategies
• Type-specializing code generators
• Optimistic typing with on-stack code rewrite
• Allocation-site object shaping
• Static analysis (as far as it can go)
• Profiling-driven recompilation.
• Many more.
59. Summary
• I love JVM. JVM is great. Lots of innovation going
on.
• There is life outside the JVM.
• I’m very enthusiastic for Parrot, PyPy, and Rubinius.
• Diversity of JS runtimes and their strategies for
efficient execution is also great to watch and
participate in.
• … especially if it means bringing back Transport
Tycoon.
60. Summary
• As a general observation, commercial JVM
technologies still have best memory management,
observability/serviceability, and threading support.
• Not saying others won’t catch up, but HotSpot/
JRockit/Azul/J9 etc. have a big lead in engineering
effort poured into them.
• So, if you stay with JVM, you’re also in a very good
place.