Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / MLtonContIsolateImplementation.adoc
CommitLineData
7f918cf1
CE
1MLtonContIsolateImplementation
2==============================
3
4As 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).
5
6Here is a direct implementation of isolate using only `callcc` and `throw`:
7
8[source,sml]
9----
10val isolate: ('a -> unit) -> 'a t =
11 fn (f: 'a -> unit) =>
12 callcc
13 (fn k1 =>
14 let
15 val x = callcc (fn k2 => throw (k1, k2))
16 val _ = (f x ; Exit.topLevelSuffix ())
17 handle exn => MLtonExn.topLevelHandler exn
18 in
19 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
20 end)
21----
22
23
24We 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.
25
26Throwing 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.
27
28Note 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).
29
30So, 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`:
31
32[source,sml]
33----
34local
35val base: (unit -> unit) t =
36 callcc
37 (fn k1 =>
38 let
39 val th = callcc (fn k2 => throw (k1, k2))
40 val _ = (th () ; Exit.topLevelSuffix ())
41 handle exn => MLtonExn.topLevelHandler exn
42 in
43 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
44 end)
45in
46val isolate: ('a -> unit) -> 'a t =
47 fn (f: 'a -> unit) =>
48 callcc
49 (fn k1 =>
50 let
51 val x = callcc (fn k2 => throw (k1, k2))
52 in
53 throw (base, fn () => f x)
54 end)
55end
56----
57
58
59We 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).]
61
62Now, 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.
63
64But, what about the continuation returned from `isolate f`? Note that the continuation returned by `isolate` is one that receives an argument `x` and then
65throws 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).
66
67But, 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
68continuation returned by `isolate f` only needs the heap-allocated values reachable from `f` (and `base`).
69
70In practice, this probably isn't a significant issue. A common use of `isolate` is implement `abort`:
71[source,sml]
72----
73fun abort th = throw (isolate th, ())
74----
75
76The continuation returned by `isolate th` is dead immediately after being thrown to -- the continuation isn't retained, so neither is the 'semantic'
77garbage it would have retained.
78
79But, 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`:
80
81[source,sml]
82----
83local
84val base: (unit -> unit) t =
85 callcc
86 (fn k1 =>
87 let
88 val th = callcc (fn k2 => throw (k1, k2))
89 val _ = (th () ; Exit.topLevelSuffix ())
90 handle exn => MLtonExn.topLevelHandler exn
91 in
92 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
93 end)
94in
95val isolate: ('a -> unit) -> 'a t =
96 fn (f: 'a -> unit) =>
97 callcc
98 (fn k1 =>
99 throw (base, fn () =>
100 let
101 val x = callcc (fn k2 => throw (k1, k2))
102 in
103 throw (base, fn () => f x)
104 end))
105end
106----
107
108
109This 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.)
110
111One 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:
112
113[source,sml]
114----
115local
116val base: (unit -> unit) option t =
117 let
118 val baseRef: (unit -> unit) option t option ref = ref NONE
119 val th = callcc (fn k => (base := SOME k; NONE))
120 in
121 case th of
122 NONE => (case !baseRef of
123 NONE => raise Fail "MLton.Cont.isolate: missing base"
124 | SOME base => base)
125 | SOME th => let
126 val _ = (th () ; Exit.topLevelSuffix ())
127 handle exn => MLtonExn.topLevelHandler exn
128 in
129 raise Fail "MLton.Cont.isolate: return from (wrapped)
130 func"
131 end
132 end
133in
134val isolate: ('a -> unit) -> 'a t =
135 fn (f: 'a -> unit) =>
136 callcc
137 (fn k1 =>
138 throw (base, SOME (fn () =>
139 let
140 val x = callcc (fn k2 => throw (k1, k2))
141 in
142 throw (base, SOME (fn () => f x))
143 end)))
144end
145----
146
147
148Now, 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.
149
150This 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
151continuations is given by
152[source,sml]
153----
154type 'a t = (unit -> 'a) -> unit
155----
156
157and the implementation of `throw` is given by
158[source,sml]
159----
160fun ('a, 'b) throw' (k: 'a t, v: unit -> 'a): 'b =
161 (k v; raise Fail "MLton.Cont.throw': return from continuation")
162
163fun ('a, 'b) throw (k: 'a t, v: 'a): 'b = throw' (k, fn () => v)
164----
165
166
167Suffice 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`:
168
169[source,sml]
170----
171local
172val base: (unit -> unit) option t =
173 let
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))
177 in
178 case th of
179 NONE => !baseRef
180 | SOME th => let
181 val _ = (th () ; Exit.topLevelSuffix ())
182 handle exn => MLtonExn.topLevelHandler exn
183 in
184 raise Fail "MLton.Cont.isolate: return from (wrapped)
185 func"
186 end
187 end
188in
189val isolate: ('a -> unit) -> 'a t =
190 fn (f: 'a -> unit) =>
191 fn (v: unit -> 'a) =>
192 throw (base, SOME (f o v))
193end
194----
195
196
197Note 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.
198
199Next, 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.
200
201[source,sml]
202----
203local
204val thRef: (unit -> unit) option ref = ref NONE
205val base: unit t =
206 let
207 val baseRef: unit t ref =
208 ref (fn _ => raise Fail "MLton.Cont.isolate: missing base")
209 val () = callcc (fn k => baseRef := k)
210 in
211 case !thRef of
212 NONE => !baseRef
213 | SOME th =>
214 let
215 val _ = thRef := NONE
216 val _ = (th () ; Exit.topLevelSuffix ())
217 handle exn => MLtonExn.topLevelHandler exn
218 in
219 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
220 end
221 end
222in
223val isolate: ('a -> unit) -> 'a t =
224 fn (f: 'a -> unit) =>
225 fn (v: unit -> 'a) =>
226 let
227 val () = thRef := SOME (f o v)
228 in
229 throw (base, ())
230 end
231end
232----
233
234
235Note 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.
236
237This implementation can be easily adapted to use MLton's primitive stack copying operations.
238
239[source,sml]
240----
241local
242val thRef: (unit -> unit) option ref = ref NONE
243val base: Thread.preThread =
244 let
245 val () = Thread.copyCurrent ()
246 in
247 case !thRef of
248 NONE => Thread.savedPre ()
249 | SOME th =>
250 let
251 val () = thRef := NONE
252 val _ = (th () ; Exit.topLevelSuffix ())
253 handle exn => MLtonExn.topLevelHandler exn
254 in
255 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
256 end
257 end
258in
259val isolate: ('a -> unit) -> 'a t =
260 fn (f: 'a -> unit) =>
261 fn (v: unit -> 'a) =>
262 let
263 val () = thRef := SOME (f o v)
264 val new = Thread.copy base
265 in
266 Thread.switchTo new
267 end
268end
269----
270
271
272In 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.
273
274The 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).
275
276[source,sml]
277----
278local
279val thRef: (unit -> unit) option ref = ref NONE
280val base: Thread.preThread =
281 let
282 val () = Thread.copyCurrent ()
283 in
284 case !thRef of
285 NONE => Thread.savedPre ()
286 | SOME th =>
287 let
288 val () = thRef := NONE
289 val _ = MLton.atomicEnd (* Match 1 *)
290 val _ = (th () ; Exit.topLevelSuffix ())
291 handle exn => MLtonExn.topLevelHandler exn
292 in
293 raise Fail "MLton.Cont.isolate: return from (wrapped) func"
294 end
295 end
296in
297val isolate: ('a -> unit) -> 'a t =
298 fn (f: 'a -> unit) =>
299 fn (v: unit -> 'a) =>
300 let
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 *)
305 in
306 Thread.switchTo new (* Match 2 *)
307 end
308end
309----
310
311
312It 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`:
313
314[source,sml]
315----
316val isolate: ('a -> unit) -> 'a t =
317 fn (f: 'a -> unit) =>
318 fn (v: unit -> 'a) =>
319 let
320 val th = (f (v ()) ; Exit.topLevelSuffix ())
321 handle exn => MLtonExn.topLevelHandler exn
322 val t = MLton.Thread.prepare (MLton.Thread.new th, ())
323 in
324 MLton.Thread.switch (fn _ => t)
325 end
326----
327
328
329It was pleasant to discover that it could equally well be 'derived' starting from the `callcc` and `throw` implementation.
330
331As 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.