Everything is a dynamic map Everything is a map and every operation a function call Everything is a map and every operation a function call and everything is a vector C Everything is an address
8 Parser and language work to build syntax tree (AST), AST Interpreter Write a “real” VM In C/C++, still using AST interpreter, spend a lot of time implementing runtime system, GC, … Define a bytecode format and write bytecode interpreter People complain about performance Write a JIT compiler Improve the garbage collector Performance is still bad Prototype a new language
9 Parser and language work to build syntax tree (AST), AST Interpreter Write a “real” VM In C/C++, still using AST interpreter, spend a lot of time implementing runtime system, GC, … People start using it Define a bytecode format and write bytecode interpreter People complain about performance Write a JIT compiler Improve the garbage collector Performance is still bad Prototype a new language
10 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Managed Host Language or Unmanaged Language Unmanaged Language (typically C or C++) Managed Host Language Written in:
11 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle
12 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle int main() { struct complex *a = …; struct complex *b = …; add(a, b); }
13 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle int main() { struct complex *a = …; struct complex *b = …; add(a, b); }
14 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle C
17 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } int add(int a, int b) { return a + b; }
18 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } int add(int a, int b) { return a + b; } String add(String a, String b) { return a + b; }
19 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } Object add(Object a, Object b) { return genericAdd(a, b); } int add(int a, int b) { return a + b; } String add(String a, String b) { return a + b; }
20 U U U U U I I I G G I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Compilation using Partial Evaluation Compiled Code Node Transitions S U I D G Uninitialized Integer Generic Double String
21 I I I G G I I I G G Deoptimization to AST Interpreter D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation
Graal VM § A new JIT compiler for Java written in Java. § Designed for multi-language performance. § Downloadable from the Oracle Technology Network (OTN) with Java and JavaScript support. C
34 1 typedef VALUE void*; 2 typedef ID void*; 3 4 // Define a C function as a Ruby method 5 void rb_define_method 6 (VALUE class, const char* name, 7 VALUE(*func)(), int argc); 8 9 // Store an array element into a Ruby array 10 void rb_ary_store 11 (VALUE ary, long idx, VALUE val); 12 13 // Get the Ruby internal representation of an 14 // identifier 15 ID rb_intern(const char* name); 16 17 // Get instance variables of a Ruby object 18 VALUE rb_iv_get(VALUE object, 19 const char* iv_name) 20 21 // Invoke a Ruby method from C 22 VALUE rb_funcall(VALUE receiver ID method_id, 23 int argc, ...); 24 25 // Convert a Ruby Fixnum to C long 26 long FIX2INT(VALUE value); Well designed API? è Internal functions of CRuby
42 Let’s write a small part of a program in a different language! Ø There is no interface to the language I would need Ø I don’t want to write a lot of wrapper code Ø Performance will be bad #include<stdio.h> struct complex { double r; double i; } int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } struct complex *a struct complex *b
43 def function(v1, v2) sum = [] for i in 0..n do sum[i] = v1[i] + v2[i] end end function(v1, v2) { v1 + v2 } v1 v2 Let’s write a small part of a program in a different language! Ø There is no interface to the language I would need Ø I don’t want to write a lot of wrapper code Ø Performance will be bad
44 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts Gradually migrate existing projects § Programmers need to write wrapper code Reusing existing source code § Language borders introduce a performance bottleneck
46 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts Gradually migrate existing projects § Programmers need to write wrapper code Reusing existing source code § Language borders introduce a performance bottleneck
47 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects § Programmers need to write wrapper code Reusing existing source code § Language borders introduce a performance bottleneck
48 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects § Programmers need to write wrapper code è Without writing boilerplate code Reusing existing source code § Language borders introduce a performance bottleneck
49 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects § Programmers need to write wrapper code è Without writing boilerplate code Reusing existing source code § Language borders introduce a performance bottleneck è Cross-language inlining
High-Performance Language Interoperability 0 200 400 600 800 1000 1200 Ruby data JS data C data Ruby program JS program C program C Scores for mixing Ruby/JS/C executing the scimark benchmarks. Higher is better
53 Safe Harbor Statement The preceding is intended to provide some insight into a line of research in Oracle Labs. It is intended 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 decisions. The development, release, and timing of any features or functionality described in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle.