Effective Concurrency with Algebraic Effects
Stephen Dolan1, Leo White2, KC Sivaramakrishnan1, Jeremy Yallop1, Anil Madhavapeddy1
1 2
E ff ective Concurrency with Algebraic E ff ects Stephen Dolan 1 , - - PowerPoint PPT Presentation
E ff ective Concurrency with Algebraic E ff ects Stephen Dolan 1 , Leo White 2 , KC Sivaramakrishnan 1 , Jeremy Yallop 1 , Anil Madhavapeddy 1 1 2 Concurrency Parallelism Concurrency Programming technique Overlapped execution of
Stephen Dolan1, Leo White2, KC Sivaramakrishnan1, Jeremy Yallop1, Anil Madhavapeddy1
1 2
pure setting.
, ICFP ’13
exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1
exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1
effect Foo : int -> int let f () = 1 + (perform (Foo 3)) let r = try f () (* spawned in a new fiber *) with effect (Foo i) k -> continue k (i + 1)
type _ eff += Foo : int -> int eff val perform : 'a eff -> 'a type ('a,'b) continuation val continue : ('a,'b) continuation -> 'a -> 'b
Effects interface
exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1
effect Foo : int -> int let f () = 1 + (perform (Foo 3)) let r = try f () (* spawned in a new fiber *) with effect (Foo i) k -> continue k (i + 1)
type _ eff += Foo : int -> int eff val perform : 'a eff -> 'a type ('a,'b) continuation val continue : ('a,'b) continuation -> 'a -> 'b
Effects interface
exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1
effect Foo : int -> int let f () = 1 + (perform (Foo 3)) 4 let r = try f () (* spawned in a new fiber *) with effect (Foo i) k -> continue k (i + 1)
type _ eff += Foo : int -> int eff val perform : 'a eff -> 'a type ('a,'b) continuation val continue : ('a,'b) continuation -> 'a -> 'b
Effects interface
effect Foo : int -> int let f () = (perform (Foo 3)) (* 3 + 1 *) + (perform (Foo 3)) (* 3 + 1 *) let r = try f () (* spawned in a new fiber *) with effect (Foo i) k -> (* continuation called outside try/with *) continue k (i + 1)
effect Foo : int -> int let f () = (perform (Foo 3)) (* 3 + 1 *) + (perform (Foo 3)) (* 3 + 1 *) let r = try f () (* spawned in a new fiber *) with effect (Foo i) k -> (* continuation called outside try/with *) continue k (i + 1)
effect Foo : int -> int let f () = (perform (Foo 3)) (* 3 + 1 *) + (perform (Foo 3)) (* 3 + 1 *) let r = try f () (* spawned in a new fiber *) with effect (Foo i) k -> (* continuation called outside try/with *) continue k (i + 1)
[1] https://github.com/kayceesrk/ocaml15-eff/tree/master/chameneos-redux
handle / continue
handler sp
call chain reference
handle / continue handle / continue
sp handler
call chain reference
perform
sp
handle / continue
handler
call chain reference
Time (S) 2.5 5 7.5 10 Iterations (X100,000) 1 2 3 4 5 6 7 8 9 10
Lwt (bytecode) Fibers (bytecode) Concurrency Monad (bytecode)
Time (S) 0.45 0.9 1.35 1.8 Iterations (X100,000) 1 2 3 4 5 6 7 8 9 10
Lwt (native) Fibers (bytecode) Concurreny Monad (native) GHC (native)
[1] https://github.com/kayceesrk/ocaml15-eff/blob/master/generator.ml
let rec iter f = function | Leaf -> () | Node (l, x, r) -> iter f l; f x; iter f r type 'a t = | Leaf | Node of 'a t * 'a * 'a t
[1] https://github.com/kayceesrk/ocaml15-eff/blob/master/generator.ml
(* val to_gen : 'a t -> (unit -> 'a option) *) let to_gen (type a) (t : a t) = let module M = struct effect Next : a -> unit end in let open M in let step = ref (fun () -> assert false) in let first_step () = try iter (fun x -> perform (Next x)) t; None with effect (Next v) k -> step := continue k; Some v in step := first_step; fun () -> !step () let rec iter f = function | Leaf -> () | Node (l, x, r) -> iter f l; f x; iter f r type 'a t = | Leaf | Node of 'a t * 'a * 'a t
Time (S) 1 2 3 4 Binary tree depth 15 16 17 18 19 20 21 22 23 24 25
Iterator Fiber Generator H/W Generator
[1] Matija Pretnar, “Inferring Algebraic Effects” , http://arxiv.org/abs/1312.2334
[1] Matija Pretnar, “Inferring Algebraic Effects” , http://arxiv.org/abs/1312.2334 [2] https://github.com/kayceesrk/ocaml15-eff/blob/master/reify_refmect.ml
[1] Matija Pretnar, “Inferring Algebraic Effects” , http://arxiv.org/abs/1312.2334 [2] https://github.com/kayceesrk/ocaml15-eff/blob/master/reify_refmect.ml [3] T Rompf et al., “Implementing fjrst-class polymorphic delimited continuations by a type-directed selective CPS-transform” , ICFP ‘09