Continuation in Javascript PDF
Continuation in Javascript PDF
Florian Loitsch
Inria Sophia Antipolis
2004 route des Lucioles - BP 93
F-06902 Sophia Antipolis, Cedex,
France
http://www.inria.fr/mimosa/Florian.Loitsch
1.1 Organization
Section 2 presents exception-based continuations and summarizes
the work that has been done in this area. In Section 2.2, 2.3 and 2.4
we show how to adapt these existing techniques to JavaScript. Sec-
tion 3 then shows how we can implement call/cc using similar
techniques. In Section 4 we discuss some optimizations. Ways to
Proceedings of the 2007 Workshop on Scheme and Functional Programming handle callbacks are proposed in Section 5. Section 6 presents the
Université Laval Technical Report DIUL-RT-0701 result of our benchmarks. Related work is discussed in Section 7
and we finally conclude in Section 8.
3. Call/cc
Suspend/Resume is sufficient for asynchronous communication
and cooperative threading. In the context of Scheme (and other lan-
guages) full fledged continuations are however needed. This section
presents the changes to evolve suspend/resume to call/cc.
Suspend/resume basically pauses the control flow. Instead of
returning, suspend aborts the execution until an event invokes
(a) before (b) after the resume-continuations. With the exception of event-handling
code, the program continues semantically as if no instruction had
been executed between the end of the suspend-function and its
continuation.
Call/cc-continuations, on the other hand, are more flexible. They
can be invoked at any time and multiple times. In particular users
are free to execute code between the return of call/cc and the
invocation of the captured continuation. This raises an important
question: what happens to (stack-)variables that are modified after
(c) boxed the continuation has been captured? Semantically there are two
possibilities: - either these variables are restored to the value they
Figure 8: Call-stacks before and after callcc-call. had when the continuation was captured; - or they should be left
at their new value. Whereas the first choice could be useful for
Our solution is to box all non-constant escaping variables (a con- checkpointing, etc. it is the latter one which is generally adopted.
servative super set of the concerned variables). The closure will Similar to Scheme we want hence modifications to variables remain
still reference an outdated variable, but the referenced box of the when continuations are executed.
escaped variables will be in sync with the equivalent variables of The function in Figure 10a, for instance, would yield different re-
the new stack-frame (see Figure 8c).5 sults depending on the chosen semantics. After the first invocation
of the continuation the print in line 4 should obviously print 1,
4 In fact, most JavaScript implementations currently just store the call-stack but more importantly (due to the assignment in the following line)
itself in the closure. other invocations could then either continue printing 1 (value at
5 Scheme (and other) compiler writers will not be surprised by the solution. time of suspension) or could then print 2, 3, etc. We would like
Boxing of escaping variables is a common practice in Scheme compilers our technique to print the incrementing sequence, but our previ-
(Kranz et al. 1986), but usually for entirely different reasons. ous suspension technique ignores modifications that happened to
1 2 3 4 5 6 7 8 9 10
All benchmarks were run on an Intel Pentium 4 3.40GHz, 1GB,
Bague 2.0
2.4 running Linux 2.6.21. Each program was run 5 times, and the
Fib
3.8
5.8
6.2 minimum time was collected. The time measurement was done by a
Mb100
Mbrot 3.5
4.2
3.9
small JavaScript program itself. Any time spent on the preparation
Nested 7.2
7.7 (parsing, precompiling, etc.) was hence not measured. The results
2.7
Quicksort
2.2
3.3 are shown in Figure 15.
Sieve 3.8
Tak 7.8
16.9
We have noticed tremendous differences between the three browsers.
Towers 10.4
11.1 Konqueror seems to be the least affected, but as it was not very fast
1.1
Even/Odd 1.1
4.4
in the beginning, the time penalties are important. Opera’s behavior
Ewal 4.7
largely depends on the benchmarks, but one can see that continu-
(b) Opera ation support can be expensive. Even though Firefox has worse
values than Konqueror one should note that Firefox was up to
Konqueror trampolines & call/cc ten times faster than Konqueror. Compared to the uninstrumented
Suspend/Resume2 Call/cc2 version continuation-enabled code was however up to 4.1 times
slower.
1
1.3
2 3 4 5
Despite these apparently bad results we think that continuations are
Bague 1.4
Fib 2.9
2.9
viable, as most benchmarks have been modified to exhibit worst
Mb100 1.0
1.0 case scenarios. Even the most realistic benchmark (ewal) repre-
1.0
Mbrot 1.0
1.9
sents a non-optimal example for exception-based continuations. Its
Nested 2.2
Quicksort 1.1
1.3
high number of anonymous functions and closures makes it diffi-
Sieve 1.1
1.7
2.0
cult to analyze.
Tak 3.1
2.5
Towers 2.5
1.0
Even/Odd 1.0
1.8
Ewal 1.7
7. Related Work
(c) Konqueror Our work is an adaption and evolution of the suspension and migra-
tion techniques presented in Tao’s thesis (Tao 2001) and Sekiguchi
Figure 15: Impact of suspend/resume and call/cc instrumentation. et al.’s paper (Sekiguchi et al. 2001). Pettyjohn et al. later extended
Raw code is the 1.0 mark. Lower is better. this technique for call/cc (Pettyjohn et al. 2005) and formally
References
Ezra Cooper, Sam Lindley, Philip Wadler, and Jeremy Yallop.
Links: Web programming without tiers. submitted to ICFP 2006,
URL http://groups.inf.ed.ac.uk/links/papers/-
links-icfp06/links-icfp06.pdf, 2006.
ECMA. ECMA-262: ECMAScript Language Specification. Third
edition, 1999.
Cormac Flanagan, Amr Sabry, Bruce F. Duba, and Matthias
Felleisen. The essence of compiling with continuations. In
Proceedings ACM SIGPLAN 1993 Conf. on Programming Lan-
guage Design and Implementation, PLDI’93, Albuquerque, NM,
USA, 23–25 June 1993, volume 28(6), pages 237–247. ACM
Press, New York, 1993.
Hamish Friedlander. djax. URL http://djax.mindcontrol-
dogs.com/.
A. Le Hors, P. Le Hegaret, G. Nicol, J. Robie, M. Champion, and
S. Byrne (Eds). “Document Object Model (DOM) Level 2 Core
Specification Version 1.0”. W3C Recommendation, 2000.
R. Kelsey, W. Klinger, and J. Rees. Revised5 report on the algo-
rithmic language Scheme. Higher-Order and Symbolic Compu-
tation, 11(1), August 1998.
David Kranz, Richard Kelsey, Jonathan Rees, Paul Hudak, James
Philbin, and Norman Adams. Orbit: an optimizing compiler
for scheme. In SIGPLAN ’86: Proceedings of the 1986 SIG-
PLAN symposium on Compiler construction, pages 219–233,
New York, NY, USA, 1986. ACM Press. ISBN 0-89791-197-
0.
Florian Loitsch and Manuel Serrano. Hop client-side compilation.
In TFP 2007: Draft Proceedings of the 8th Symposium on Trends
in Functional Programming, April 2007.
Neil Mix. Narrative javascript. URL http://neilmix.com/-
narrativejs/.
Mozilla Foundation. Rhino. URL http://www.mozilla.org/-
rhino/.
S. Muchnick. Advanced Compiler Design & Implementation. Mor-
gan Kaufmann, 1997. ISBN 1-55860-320-4.