Top Banner
JVM Continuations Lukas Stadler Johannes Kepler University Linz, Austria
25

JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Jul 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

JVM Continuations

Lukas StadlerJohannes Kepler University Linz, Austria

Page 2: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Agenda

● Continuations● Uses for continuations● Common implementation techniques● Our lazy approach● Implementation● Summary

JVM Language Summit 2009

Page 3: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Continuations

● Functional / dynamic languages● “the rest of the computation”● “everything thats going to happen from now on”● In Java terminology: (part of) the contents of

the stack of activation frames(method, bci, variables, expressions)

● Can be stored● Can be reinstated (possibly more than once)

● Different types with different semantics

JVM Language Summit 2009

Page 4: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Continuations

Continuation alpha;

void method() {    int value = 0;    alpha.capture();    System.out.println("current value: " + value);    value += 1;    alpha.resume();}

JVM Language Summit 2009

Page 5: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Continuations

Continuation alpha;

void method() {    int value = 0;    alpha.capture();    System.out.println("current value: " + value);    value += 1;    alpha.resume();}

current value: 0current value: 0current value: 0current value: 0......

JVM Language Summit 2009

Page 6: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Uses for continuations

● Functional languages: basic language features● return, exception handling, etc.

● Java: advanced features● green threads, coroutines, fibers, etc.

● Web servers● linearize complex interactions● “back button” problem

● Checkpointing, portable agents, etc.

JVM Language Summit 2009

Page 7: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Common Techniques

● One-shot continuations (via exceptions)● Activation frames as objects (Smalltalk)● Segments containing many activation frames

allocated on heap(some Scheme environments)

● Most implementations: Copy-all approach

JVM Language Summit 2009

Page 8: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Common Techniques

● Copy-all approach: example

Continuation alpha;Continuation beta;

void a() {    b();}void b() {    alpha.capture();    beta.capture();}

JVM Language Summit 2009

Page 9: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Common Techniques

● Copy-all approach: example

Continuation alpha;Continuation beta;

void a() {    b();}void b() {    alpha.capture();    beta.capture();}

JVM Language Summit 2009

Page 10: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Common Techniques

● Copy-all approach: example

Continuation alpha;Continuation beta;

void a() {    b();}void b() {    alpha.capture();    beta.capture();}

JVM Language Summit 2009

Page 11: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Common Techniques

● Problems:● Immediate cost of continuation capture● Continuations often share activation frames● No way to tell if an activation frame needs to be

restored

● Be Lazy!

JVM Language Summit 2009

Page 12: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Lazy Continuations

● Store activation frames as late as possible● Intercept the return to an activation frame by

patching the return address● Call site - specific trampoline● One Object per activation frame (called

activation object): linked list● The activation object for the next activation

frame stored in the thread

JVM Language Summit 2009

Page 13: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Lazy Continuations

● Continuations joined into tree structure

JVM Language Summit 2009

Page 14: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Lazy Continuations

● Continuations joined into tree structure

JVM Language Summit 2009

Page 15: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Lazy Continuations

● Continuations joined into tree structure

JVM Language Summit 2009

Page 16: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Implementation tricks

Continuation alpha;

void a() {    b();}

void b() {    alpha.capture();}

Stack

a()

b()

capture()

Page 17: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Implementation tricks

Continuation alpha;

void a() {    b();}storeFrame();goto;

void b() {    alpha.capture();}storeFrame();goto;

Stack

a()

b()

capture()

native void storeFrame();

Page 18: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Implementation tricks

● Assembly fast path, C++ slow paths● Where to put / how to connect all this?

● Patching● One extra trampoline per call site● Keeps stack walking, etc. simple

● Interfacing asm/C++● JNI method called by trampoline● Stackless, no-safepoint asm fast path

Page 19: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Java Interface

● Passing a return value on resume● Annotation to mark methods continuation - safe

public class Continuation {    public static final Object CAPTURED;    public native Object capture();    public native void resume(Object retVal);}

public @interface Continuable {}

JVM Language Summit 2009

Page 20: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Java Interfaceclass Test {    Continuation alpha = new Continuation();

    @Continuable    public static void main() {        System.out.println(“start”);        if (alpha.capture() == Continuation.CAPTURED) {            System.out.println(“captured”);            alpha.resume(null);        } else {            System.out.println(“resumed”);        }        System.out.println(“end”);    }}

startcapturedresumedend

JVM Language Summit 2009

Page 21: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Summary, Future

● Saves time● Saves memory

(break even at ~30%)

● Future:● C2 implementation● Serialization● Other uses

JVM Language Summit 2009

Page 22: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Thank you.Questions?

Lukas StadlerJohannes Kepler University Linz, Austria

Christian WimmerUniversity of California, Irvine

Thomas WürthingerJohannes Kepler University Linz, Austria

Hanspeter MössenböckJohannes Kepler University Linz, Austria

John RoseSun Microsystems, Inc.

For details on the algorithm see:Lazy Continuations for Java Virtual MachinesConference on Principles and Practice of Programming in Java 2009

Page 23: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Copy cases

JVM Language Summit 2009

Page 24: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Frame storing cases

JVM Language Summit 2009

Page 25: JVM Continuationswiki.jvmlangsummit.com/images/2/2b/JVMLanguage...Continuations Functional / dynamic languages “the rest of the computation” “everything thats going to happen

Resume cases

JVM Language Summit 2009