jvm continuationswiki.jvmlangsummit.com/images/2/2b/jvmlanguage...continuations functional / dynamic...
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/1.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/2.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/10.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/19.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/22.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/23.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/24.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042417/5f32876d12169e6d310f8db9/html5/thumbnails/25.jpg)
Resume cases
JVM Language Summit 2009