Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JS, Rust, Python and the Holy Graal

Lars Hupel
February 28, 2020

JS, Rust, Python and the Holy Graal

In the old times, way back, Java used to be the only JVM language. Later on, a few other players entered the field, such as Jython and JRuby. But those were just for scripting, right? It didn’t take long for compiled languages to catch up. With Scala and Kotlin, we have definitely entered the age of polyglot programming on the JVM. The Truffle project – part of GraalVM – is a promising approach to make it easy, efficient, and seamless to run alternative languages on the same VM as Java. This talk will look at the new age of polyglot programming and all the cool things we can do with it.

Lars Hupel

February 28, 2020
Tweet

More Decks by Lars Hupel

Other Decks in Programming

Transcript

  1. J S , R u s t , P y t h o n
    a n d t h e H o l y
    G r a a l
    L a r s H u p e l
    B O B
    2 0 2 0 - 0 2 - 2 8

    View Slide

  2. T h e o r i g i n s o f J a v a
    “ 1 9 9 6 – J a m e s G o s l i n g i n v e n t s J a v a . J a v a i s a r e l a t i v e l y v e r b o s e ,
    g a r b a g e c o l l e c t e d , c l a s s b a s e d , s t a t i c a l l y t y p e d , s i n g l e d i s p a t c h ,
    o b j e c t o r i e n t e d l a n g u a g e w i t h s i n g l e i m p l e m e n t a t i o n i n h e r i t a n c e
    a n d m u l t i p l e i n t e r f a c e i n h e r i t a n c e . S u n l o u d l y h e r a l d s J a v a ’ s n o v e l t y . ”
    – J a m e s I r y

    View Slide

  3. View Slide

  4. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n

    View Slide

  5. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y

    View Slide

  6. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y

    View Slide

  7. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a

    View Slide

  8. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a
    2 0 0 6 J a v a 6 w i t h J S R 2 2 3 ( s c r i p t i n g )

    View Slide

  9. J S R 2 2 3
    P r o b l e m
    S e r v l e t s a r e c o o l , b u t w e w a n t t o w r i t e o u r w e b p a g e s i n P H P .

    View Slide

  10. J S R 2 2 3
    P r o b l e m
    S e r v l e t s a r e c o o l , b u t w e w a n t t o w r i t e o u r w e b p a g e s i n P H P .
    S o l u t i o n
    D e f i n e a n A P I f o r s c r i p t i n g l a n g u a g e s t o e x c h a n g e o b j e c t s w i t h J a v a .

    View Slide

  11. S c r i p t i n g l a n g u a g e s
    • J S R 2 2 3 d o e s n o t d e f i n e “ s c r i p t i n g
    l a n g u a g e ”
    • w o r d i n g i m p l i e s u n t y p e d l a n g u a g e s
    • f u n d a m e n t a l p r o b l e m : m o s t c a l l s
    r e q u i r e r e f l e c t i o n ( s l o w )

    View Slide

  12. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a
    2 0 0 6 J a v a 6 w i t h J S R 2 2 3 ( s c r i p t i n g )

    View Slide

  13. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a
    2 0 0 6 J a v a 6 w i t h J S R 2 2 3 ( s c r i p t i n g )
    2 0 1 1 J a v a 7 w i t h J S R 2 9 2 ( invokedynamic)

    View Slide

  14. D a V i n c i M a c h i n e
    • s t a r t e d i n 2 0 0 7 a s a V M p l a y g r o u n d
    • s u b p r o j e c t s : d y n a m i c i n v o c a t i o n ,
    c o n t i n u a t i o n s , t a i l - c a l l s , . . .

    View Slide

  15. View Slide

  16. i n v o k e d y n a m i c
    P r o b l e m
    S c r i p t i n g l a n g u a g e s a r e c o o l , b u t w e d o n ’ t
    k n o w w h a t t h e h e c k x + y m e a n s

    View Slide

  17. i n v o k e d y n a m i c
    P r o b l e m
    S c r i p t i n g l a n g u a g e s a r e c o o l , b u t w e d o n ’ t
    k n o w w h a t t h e h e c k x + y m e a n s
    [object Object]

    View Slide

  18. i n v o k e d y n a m i c
    P r o b l e m
    S c r i p t i n g l a n g u a g e s a r e c o o l , b u t w e d o n ’ t
    k n o w w h a t t h e h e c k x + y m e a n s
    [object Object]
    S o l u t i o n
    A l l o w c o m p i l e r i m p l e m e n t e r s t o
    i m p l e m e n t c u s t o m d i s p a t c h i n g

    View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a
    2 0 0 6 J a v a 6 w i t h J S R 2 2 3 ( s c r i p t i n g )
    2 0 1 1 J a v a 7 w i t h J S R 2 9 2 ( invokedynamic)

    View Slide

  23. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a
    2 0 0 6 J a v a 6 w i t h J S R 2 2 3 ( s c r i p t i n g )
    2 0 1 1 J a v a 7 w i t h J S R 2 9 2 ( invokedynamic)
    2 0 1 4 J a v a 8 w i t h N a s h o r n

    View Slide

  24. J a v a : a s h o r t h i s t o r y
    1 9 9 4 f i r s t J V M v e r s i o n
    2 0 0 1 f i r s t v e r s i o n s o f J y t h o n a n d J R u b y
    2 0 0 3 f i r s t v e r s i o n o f G r o o v y
    2 0 0 4 f i r s t v e r s i o n o f S c a l a
    2 0 0 6 J a v a 6 w i t h J S R 2 2 3 ( s c r i p t i n g )
    2 0 1 1 J a v a 7 w i t h J S R 2 9 2 ( invokedynamic)
    2 0 1 4 J a v a 8 w i t h N a s h o r n
    2 0 1 9 f i r s t v e r s i o n o f G r a a l V M

    View Slide

  25. E n t e r G r a a l V M !
    • p o l y g l o t J V M d e v e l o p e d b y O r a c l e
    • t o n s o f f e a t u r e s

    View Slide

  26. W h a t i s T r u f f l e ?
    “ T h e T r u f f l e f r a m e w o r k a l l o w s y o u t o r u n p r o g r a m m i n g l a n g u a g e s e f -
    f i c i e n t l y o n G r a a l V M . I t s i m p l i f i e s l a n g u a g e i m p l e m e n t a t i o n b y a u t o -
    m a t i c a l l y d e r i v i n g h i g h - p e r f o r m a n c e c o d e f r o m i n t e r p r e t e r s .

    View Slide

  27. P a r t i a l a p p l i c a t i o n
    A => B => C

    View Slide

  28. P a r t i a l a p p l i c a t i o n
    A => B => C
    +
    A

    View Slide

  29. P a r t i a l a p p l i c a t i o n
    A => B => C
    +
    A
    =
    B => C

    View Slide

  30. P a r t i a l e v a l u a t i o n
    A => B => C
    +
    A
    =
    B => C

    View Slide

  31. C o m p i l e r s p e r f o r m p a r t i a l e v a l u a t i o n a l l t h e t i m e !
    L o o p u n r o l l i n g
    C o n s t a n t f o l d i n g
    B o u n d s - c h e c k i n g e l i m i n a t i o n
    D e a d c o d e e l i m i n a t i o n
    D e f o r e s t a t i o n
    I n l i n i n g

    View Slide

  32. E x a m p l e
    int f(int x, int y) {
    int res = 0;
    for (int i = 0; i < x; ++i)
    res += y;
    return res;
    }

    View Slide

  33. E x a m p l e
    int f(int x, int y) {
    int res = 0;
    for (int i = 0; i < x; ++i)
    res += y;
    return res;
    }
    stream.map(y => f(3, y));

    View Slide

  34. E x a m p l e
    int f_x3(int y) {
    int res = 0;
    res += y;
    res += y;
    res += y;
    return res;
    }
    stream.map(y => f_x3(y));

    View Slide

  35. E x a m p l e
    int f_x3(int y) {
    return 3 * y;
    }
    stream.map(y => f_x3(y));

    View Slide

  36. E x a m p l e
    stream.map(y => 3 * y);

    View Slide

  37. View Slide

  38. C o m p i l e r
    S o u r c e
    C o d e
    C o m p i l e r
    T a r g e t
    C o d e
    R u n t i m e O u t p u t
    I n p u t

    View Slide

  39. I n t e r p r e t e r
    S o u r c e
    C o d e
    R u n t i m e O u t p u t
    I n p u t

    View Slide

  40. View Slide

  41. View Slide

  42. F u t a m u r a p r o j e c t i o n
    S o u r c e
    C o d e
    R u n t i m e O u t p u t
    I n p u t
    S o u r c e
    C o d e
    C o m p i l e d
    P r o g r a m

    View Slide

  43. C o d e →
    R e s u l t

    View Slide

  44. C o d e →
    R e s u l t
    ( C o d e ,
    I n t e r p r e t e r ) →
    E x e c u t a b l e

    View Slide

  45. C o d e →
    R e s u l t
    I n t e r p r e t e r →
    C o m p i l e r
    ( C o d e ,
    I n t e r p r e t e r ) →
    E x e c u t a b l e

    View Slide

  46. C o d e →
    R e s u l t
    I n t e r p r e t e r →
    C o m p i l e r
    ( C o d e ,
    I n t e r p r e t e r ) →
    E x e c u t a b l e
    ( I n t e r p r e t e r →
    C o m p i l e r ) →
    S u p e r c o m p i l e r

    View Slide

  47. F u t a m u r a p r o j e c t i o n s
    c o n j e c t u r e d b y Y o s h i h i k o F u t a m u r a i n t h e 1 9 8 0 ’s

    View Slide

  48. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
    Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
    Applying Futamura projections
    to compose Languages and Tools
    in GraalVM
    PEPM 2019
    Christian Humer
    VM Research Group, Oracle Labs, Zurich

    View Slide

  49. View Slide

  50. View Slide

  51. public abstract class JSMultiplyNode extends JSBinaryNode {
    public abstract Object execute(Object a, Object b);
    @Specialization(guards = "b > 0", rewriteOn = ArithmeticException.class)
    protected int doIntBLargerZero(int a, int b) { /* ... */ }
    @Specialization(rewriteOn = ArithmeticException.class)
    protected int doInt(int a, int b) { /* ... */ }
    @Specialization
    protected double doDouble(double a, double b) {
    return a * b;
    }
    // ...
    }

    View Slide

  52. J V M + P o l y g l o t = P r o f i t
    • l a n g u a g e s m a y c a l l e a c h o t h e r
    • l a n g u a g e s b e n e f i t f r o m t h e J V M :
    p a r a l l e l i s m
    t o o l i n g
    l i b r a r i e s
    . . .

    View Slide

  53. View Slide

  54. N a t i v e c o d e

    View Slide

  55. Q & A
    L a r s H u p e l

    l a r s . h u p e l @ i n n o q . c o m
    � @ l a r s r _ h
    w w w . i n n o q . c o m
    i n n o Q D e u t s c h l a n d G m b H
    K r i s c h e r s t r . 1 0 0
    4 0 7 8 9 M o n h e i m a . R h .
    G e r m a n y
    + 4 9 2 1 7 3 3 3 6 6 - 0
    O h l a u e r S t r . 4 3
    1 0 9 9 9 B e r l i n
    G e r m a n y
    L u d w i g s t r . 1 8 0 E
    6 3 0 6 7 O f f e n b a c h
    G e r m a n y
    K r e u z s t r . 1 6
    8 0 3 3 1 M ü n c h e n
    G e r m a n y
    c / o W e W o r k
    H e r m a n n s t r a s s e 1 3
    2 0 0 9 5 H a m b u r g
    G e r m a n y
    i n n o Q S c h w e i z G m b H
    G e w e r b e s t r . 1 1
    C H - 6 3 3 0 C h a m
    S w i t z e r l a n d
    + 4 1 4 1 7 4 3 0 1 1 1
    A l b u l a s t r . 5 5
    8 0 4 8 Z ü r i c h
    S w i t z e r l a n d

    View Slide

  56. L A R S H U P E L
    C o n s u l t a n t
    i n n o Q D e u t s c h l a n d G m b H
    L a r s e n j o y s p r o g r a m m i n g i n a v a r i e t y o f l a n -
    g u a g e s , i n c l u d i n g S c a l a , H a s k e l l , a n d R u s t . H e i s
    k n o w n a s a f r e q u e n t c o n f e r e n c e s p e a k e r a n d o n e
    o f t h e f o u n d e r s o f t h e T y p e l e v e l i n i t i a t i v e w h i c h
    i s d e d i c a t e d t o p r o v i d i n g p r i n c i p l e d , t y p e - d r i v e n
    S c a l a l i b r a r i e s .

    View Slide

  57. I m a g e s o u r c e s

    J a m e s G o s l i n g : P e t e r C a m p b e l l , C C - B Y - S A 4 . 0 ,
    https://commons.wikimedia.org/w/index.php?title=File:James_Gosling_2008.jpg&oldid=149207971

    C o n d e - C l e m e n t e , P a t r i c i a & O r t i n , F r a n c i s c o . ( 2 0 1 4 ) . J I N D Y : A j a v a l i b r a r y t o s u p p o r t i n v o k e d y n a m i c .

    S p i c e s : https://pixabay.com/photos/spices-spice-mix-market-73776/

    D u c k t y p i n g : https://twitter.com/sketchymacro/status/1229046533359689730

    G r a a l V M a r c h i t e c t u r e : https://blogs.oracle.com/graalvm/announcement

    G r a a l V M s l i d e : https://popl19.sigplan.org/details/pepm-2019-papers/2/
    Applying-Futamura-Projections-to-Compose-Languages-and-Tools-in-GraalVM-Invited-Talk

    R a i n b o w : https://pixabay.com/photos/rainbow-seaside-coast-beach-sky-675832/

    F u t u r a m a l o g o : https://de.wikipedia.org/w/index.php?title=Datei:Futurama-logo.svg&oldid=88835467

    D a V i n c i V M s l i d e : https://openjdk.java.net/projects/mlvm/pdf/LangNet20080128.pdf

    L L V M l o g o : A p p l e

    D u k e : O r a c l e

    F i e l d : https://unsplash.com/photos/4miBe6zg5r0

    C a t w i t h y a r n : https://www.publicdomainpictures.net/en/view-image.php?image=161669&picture=cat-isolated-on-the-white

    View Slide