1 MLtonContIsolateImplementation
2 ==============================
4 As noted before, it is fairly easy to get the operational behavior of `isolate` with just `callcc` and `throw`, but establishing the right space behavior is trickier. Here, we show how to start from the obvious, but inefficient, implementation of `isolate` using only `callcc` and `throw`, and 'derive' an equivalent, but more efficient, implementation of `isolate` using MLton's primitive stack capture and copy operations. This isn't a formal derivation, as we are not formally showing the equivalence of the programs (though I believe that they are all equivalent, modulo the space behavior).
6 Here is a direct implementation of isolate using only `callcc` and `throw`:
10 val isolate: ('a -> unit) -> 'a t =
15 val x = callcc (fn k2 => throw (k1, k2))
16 val _ = (f x ; Exit.topLevelSuffix ())
17 handle exn => MLtonExn.topLevelHandler exn
19 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
24 We use the standard nested `callcc` trick to return a continuation that is ready to receive an argument, execute the isolated function, and exit the program. Both `Exit.topLevelSuffix` and `MLtonExn.topLevelHandler` will terminate the program.
26 Throwing to an isolated function will execute the function in a 'semantically' empty context, in the sense that we never re-execute the 'original' continuation of the call to isolate (i.e., the context that was in place at the time `isolate` was called). However, we assume that the compiler isn't able to recognize that the 'original' continuation is unused; for example, while we (the programmer) know that `Exit.topLevelSuffix` and `MLtonExn.topLevelHandler` will terminate the program, the compiler may only see opaque calls to unknown foreign-functions. So, that original continuation (in its entirety) is part of the continuation returned by `isolate` and throwing to the continuation returned by `isolate` will execute `f x` (with the exit wrapper) in the context of that original continuation. Thus, the garbage collector will retain everything reachable from that original continuation during the evaluation of `f x`, even though it is 'semantically' garbage.
28 Note that this space-leak is independent of the implementation of continuations (it arises in both MLton's stack copying implementation of continuations and would arise in SML/NJ's CPS-translation implementation); we are only assuming that the implementation can't 'see' the program termination, and so must retain the original continuation (and anything reachable from it).
30 So, we need an 'empty' continuation in which to execute `f x`. (No surprise there, as that is the written description of `isolate`.) To do this, we capture a top-level continuation and throw to that in order to execute `f x`:
35 val base: (unit -> unit) t =
39 val th = callcc (fn k2 => throw (k1, k2))
40 val _ = (th () ; Exit.topLevelSuffix ())
41 handle exn => MLtonExn.topLevelHandler exn
43 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
46 val isolate: ('a -> unit) -> 'a t =
51 val x = callcc (fn k2 => throw (k1, k2))
53 throw (base, fn () => f x)
59 We presume that `base` is evaluated 'early' in the program. There is a subtlety here, because one needs to believe that this `base` continuation (which technically corresponds to the entire rest of the program evaluation) 'works' as an empty context; in particular, we want it to be the case that executing `f x` in the `base` context retains less space than executing `f x` in the context in place at the call to `isolate` (as occurred in the previous implementation of `isolate`). This isn't particularly easy to believe if one takes a normal substitution-based operational semantics, because it seems that the context captured and bound to `base` is arbitrarily large. However, this context is mostly unevaluated code; the only heap-allocated values that are reachable from it are those that were evaluated before the evaluation of `base` (and used in the program after the evaluation of `base`). Assuming that `base` is evaluated 'early' in the program, we conclude that there are few heap-allocated values reachable from its continuation. In contrast, the previous implementation of `isolate` could capture a context that has many heap-allocated values reachable from it (because we could evaluate `isolate f` 'late' in the program and 'deep' in a call stack), which would all remain reachable during the evaluation of
60 `f x`. [We'll return to this point later, as it is taking a slightly MLton-esque view of the evaluation of a program, and may not apply as strongly to other implementations (e.g., SML/NJ).]
62 Now, once we throw to `base` and begin executing `f x`, only the heap-allocated values reachable from `f` and `x` and the few heap-allocated values reachable from `base` are retained by the garbage collector. So, it seems that `base` 'works' as an empty context.
64 But, what about the continuation returned from `isolate f`? Note that the continuation returned by `isolate` is one that receives an argument `x` and then
65 throws to `base` to evaluate `f x`. If we used a CPS-translation implementation (and assume sufficient beta-contractions to eliminate administrative redexes), then the original continuation passed to `isolate` (i.e., the continuation bound to `k1`) will not be free in the continuation returned by `isolate f`. Rather, the only free variables in the continuation returned by `isolate f` will be `base` and `f`, so the only heap-allocated values reachable from the continuation returned by `isolate f` will be those values reachable from `base` (assumed to be few) and those values reachable from `f` (necessary in order to execute `f` at some later point).
67 But, MLton doesn't use a CPS-translation implementation. Rather, at each call to `callcc` in the body of `isolate`, MLton will copy the current execution stack. Thus, `k2` (the continuation returned by `isolate f`) will include execution stack at the time of the call to `isolate f` -- that is, it will include the 'original' continuation of the call to `isolate f`. Thus, the heap-allocated values reachable from the continuation returned by `isolate f` will include those values reachable from `base`, those values reachable from `f`, and those values reachable from the original continuation of the call to `isolate f`. So, just holding on to the continuation returned by `isolate f` will retain all of the heap-allocated values live at the time `isolate f` was called. This leaks space, since, 'semantically', the
68 continuation returned by `isolate f` only needs the heap-allocated values reachable from `f` (and `base`).
70 In practice, this probably isn't a significant issue. A common use of `isolate` is implement `abort`:
73 fun abort th = throw (isolate th, ())
76 The continuation returned by `isolate th` is dead immediately after being thrown to -- the continuation isn't retained, so neither is the 'semantic'
77 garbage it would have retained.
79 But, it is easy enough to 'move' onto the 'empty' context `base` the capturing of the context that we want to be returned by `isolate f`:
84 val base: (unit -> unit) t =
88 val th = callcc (fn k2 => throw (k1, k2))
89 val _ = (th () ; Exit.topLevelSuffix ())
90 handle exn => MLtonExn.topLevelHandler exn
92 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
95 val isolate: ('a -> unit) -> 'a t =
101 val x = callcc (fn k2 => throw (k1, k2))
103 throw (base, fn () => f x)
109 This implementation now has the right space behavior; the continuation returned by `isolate f` will only retain the heap-allocated values reachable from `f` and from `base`. (Technically, the continuation will retain two copies of the stack that was in place at the time `base` was evaluated, but we are assuming that that stack small.)
111 One minor inefficiency of this implementation (given MLton's implementation of continuations) is that every `callcc` and `throw` entails copying a stack (albeit, some of them are small). We can avoid this in the evaluation of `base` by using a reference cell, because `base` is evaluated at the top-level:
116 val base: (unit -> unit) option t =
118 val baseRef: (unit -> unit) option t option ref = ref NONE
119 val th = callcc (fn k => (base := SOME k; NONE))
122 NONE => (case !baseRef of
123 NONE => raise Fail "MLton.Cont.isolate: missing base"
126 val _ = (th () ; Exit.topLevelSuffix ())
127 handle exn => MLtonExn.topLevelHandler exn
129 raise Fail "MLton.Cont.isolate: return from (wrapped)
134 val isolate: ('a -> unit) -> 'a t =
135 fn (f: 'a -> unit) =>
138 throw (base, SOME (fn () =>
140 val x = callcc (fn k2 => throw (k1, k2))
142 throw (base, SOME (fn () => f x))
148 Now, to evaluate `base`, we only copy the stack once (instead of 3 times). Because we don't have a dummy continuation around to initialize the reference cell, the reference cell holds a continuation `option`. To distinguish between the original evaluation of `base` (when we want to return the continuation) and the subsequent evaluations of `base` (when we want to evaluate a thunk), we capture a `(unit -> unit) option` continuation.
150 This seems to be as far as we can go without exploiting the concrete implementation of continuations in <:MLtonCont:>. Examining the implementation, we note that the type of
151 continuations is given by
154 type 'a t = (unit -> 'a) -> unit
157 and the implementation of `throw` is given by
160 fun ('a, 'b) throw' (k: 'a t, v: unit -> 'a): 'b =
161 (k v; raise Fail "MLton.Cont.throw': return from continuation")
163 fun ('a, 'b) throw (k: 'a t, v: 'a): 'b = throw' (k, fn () => v)
167 Suffice to say, a continuation is simply a function that accepts a thunk to yield the thrown value and the body of the function performs the actual throw. Using this knowledge, we can create a dummy continuation to initialize `baseRef` and greatly simplify the body of `isolate`:
172 val base: (unit -> unit) option t =
174 val baseRef: (unit -> unit) option t ref =
175 ref (fn _ => raise Fail "MLton.Cont.isolate: missing base")
176 val th = callcc (fn k => (baseRef := k; NONE))
181 val _ = (th () ; Exit.topLevelSuffix ())
182 handle exn => MLtonExn.topLevelHandler exn
184 raise Fail "MLton.Cont.isolate: return from (wrapped)
189 val isolate: ('a -> unit) -> 'a t =
190 fn (f: 'a -> unit) =>
191 fn (v: unit -> 'a) =>
192 throw (base, SOME (f o v))
197 Note that this implementation of `isolate` makes it clear that the continuation returned by `isolate f` only retains the heap-allocated values reachable from `f` and `base`. It also retains only one copy of the stack that was in place at the time `base` was evaluated. Finally, it completely avoids making any copies of the stack that is in place at the time `isolate f` is evaluated; indeed, `isolate f` is a constant-time operation.
199 Next, suppose we limited ourselves to capturing `unit` continuations with `callcc`. We can't pass the thunk to be evaluated in the 'empty' context directly, but we can use a reference cell.
204 val thRef: (unit -> unit) option ref = ref NONE
207 val baseRef: unit t ref =
208 ref (fn _ => raise Fail "MLton.Cont.isolate: missing base")
209 val () = callcc (fn k => baseRef := k)
215 val _ = thRef := NONE
216 val _ = (th () ; Exit.topLevelSuffix ())
217 handle exn => MLtonExn.topLevelHandler exn
219 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
223 val isolate: ('a -> unit) -> 'a t =
224 fn (f: 'a -> unit) =>
225 fn (v: unit -> 'a) =>
227 val () = thRef := SOME (f o v)
235 Note that it is important to set `thRef` to `NONE` before evaluating the thunk, so that the garbage collector doesn't retain all the heap-allocated values reachable from `f` and `v` during the evaluation of `f (v ())`. This is because `thRef` is still live during the evaluation of the thunk; in particular, it was allocated before the evaluation of `base` (and used after), and so is retained by continuation on which the thunk is evaluated.
237 This implementation can be easily adapted to use MLton's primitive stack copying operations.
242 val thRef: (unit -> unit) option ref = ref NONE
243 val base: Thread.preThread =
245 val () = Thread.copyCurrent ()
248 NONE => Thread.savedPre ()
251 val () = thRef := NONE
252 val _ = (th () ; Exit.topLevelSuffix ())
253 handle exn => MLtonExn.topLevelHandler exn
255 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
259 val isolate: ('a -> unit) -> 'a t =
260 fn (f: 'a -> unit) =>
261 fn (v: unit -> 'a) =>
263 val () = thRef := SOME (f o v)
264 val new = Thread.copy base
272 In essence, `Thread.copyCurrent` copies the current execution stack and stores it in an implicit reference cell in the runtime system, which is fetchable with `Thread.savedPre`. When we are ready to throw to the isolated function, `Thread.copy` copies the saved execution stack (because the stack is modified in place during execution, we need to retain a pristine copy in case the isolated function itself throws to other isolated functions) and `Thread.switchTo` abandons the current execution stack, installing the newly copied execution stack.
274 The actual implementation of `MLton.Cont.isolate` simply adds some `Thread.atomicBegin` and `Thread.atomicEnd` commands, which effectively protect the global `thRef` and accommodate the fact that `Thread.switchTo` does an implicit `Thread.atomicEnd` (used for leaving a signal handler thread).
279 val thRef: (unit -> unit) option ref = ref NONE
280 val base: Thread.preThread =
282 val () = Thread.copyCurrent ()
285 NONE => Thread.savedPre ()
288 val () = thRef := NONE
289 val _ = MLton.atomicEnd (* Match 1 *)
290 val _ = (th () ; Exit.topLevelSuffix ())
291 handle exn => MLtonExn.topLevelHandler exn
293 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
297 val isolate: ('a -> unit) -> 'a t =
298 fn (f: 'a -> unit) =>
299 fn (v: unit -> 'a) =>
301 val _ = MLton.atomicBegin (* Match 1 *)
302 val () = thRef := SOME (f o v)
303 val new = Thread.copy base
304 val _ = MLton.atomicBegin (* Match 2 *)
306 Thread.switchTo new (* Match 2 *)
312 It is perhaps interesting to note that the above implementation was originally 'derived' by specializing implementations of the <:MLtonThread:> `new`, `prepare`, and `switch` functions as if their only use was in the following implementation of `isolate`:
316 val isolate: ('a -> unit) -> 'a t =
317 fn (f: 'a -> unit) =>
318 fn (v: unit -> 'a) =>
320 val th = (f (v ()) ; Exit.topLevelSuffix ())
321 handle exn => MLtonExn.topLevelHandler exn
322 val t = MLton.Thread.prepare (MLton.Thread.new th, ())
324 MLton.Thread.switch (fn _ => t)
329 It was pleasant to discover that it could equally well be 'derived' starting from the `callcc` and `throw` implementation.
331 As a final comment, we noted that the degree to which the context of `base` could be considered 'empty' (i.e., retaining few heap-allocated values) depended upon a slightly MLton-esque view. In particular, MLton does not heap allocate executable code. So, although the `base` context keeps a lot of unevaluated code 'live', such code is not heap allocated. In a system like SML/NJ, that does heap allocate executable code, one might want it to be the case that after throwing to an isolated function, the garbage collector retains only the code necessary to evaluate the function, and not any code that was necessary to evaluate the `base` context.