E ff ect handlers in OCaml KC Sivaramakrishnan 1 & Stephen Dolan - - PowerPoint PPT Presentation

e ff ect handlers in ocaml
SMART_READER_LITE
LIVE PREVIEW

E ff ect handlers in OCaml KC Sivaramakrishnan 1 & Stephen Dolan - - PowerPoint PPT Presentation

E ff ect handlers in OCaml KC Sivaramakrishnan 1 & Stephen Dolan 1 Leo White 2 , Jeremy Yallop 1,3 , Armal Guneau 4 , Anil Madhavapeddy 1,3 1 2 3 4 Concurrency Parallelism Concurrency Programming technique Overlapped


slide-1
SLIDE 1

Effect handlers in OCaml

KC Sivaramakrishnan1 & Stephen Dolan1 Leo White2, Jeremy Yallop1,3, Armaël Guéneau4, Anil Madhavapeddy1,3

1 2 3 4

slide-2
SLIDE 2

Concurrency ≠ Parallelism

  • Concurrency
  • Programming technique
  • Overlapped execution of processes
  • Parallelism
  • (Extreme) Performance hack
  • Simultaneous execution of computations

Concurrency ∩ Parallelism ➔ Scalable Concurrency (Fibers) (Domains)

slide-3
SLIDE 3

Schedulers

  • Multiplexing fjbers over domain(s)
  • Bake scheduler into the runtime system (GHC)
  • Allow programmers to describe schedulers!
  • Parallel search —> LIFO work-stealing
  • Web-server —> FIFO runqueue
  • Data parallel —> Gang scheduling
  • Algebraic Effects and Handlers
slide-4
SLIDE 4

Algebraic Effects: Example

effect Foo : int -> int let f () = (perform (Foo 3)) (* 3 + 1 *) + (perform (Foo 3)) (* 3 + 1 *) let r = try f () with effect (Foo i) k -> continue k (i + 1)

val r : int = 8

slide-5
SLIDE 5

Dynamic wind

let dynamic_wind before_thunk thunk after_thunk = before_thunk (); let res = match thunk () with | v -> v | exception e -> after_thunk (); raise e | effect e k -> after_thunk (); let res' = perform e in before_thunk (); continue k res' in after_thunk (); res

slide-6
SLIDE 6

Effect systems and modularity

  • Right now, we type effects like ML exceptions
  • (we're in the market for an effect system, if anyone has one

lying around...)

  • We need modularity, because effects can:
  • be local, dynamic and fresh
  • be abstracted, renamed and reuse
  • We don't know statically whether two effects are the same
slide-7
SLIDE 7

Scheduler Demo1

[1] https://github.com/kayceesrk/ocaml15-eff/tree/master/chameneos-redux

slide-8
SLIDE 8

Generator from Iterator1

[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

slide-9
SLIDE 9
  • Fibers: Heap allocated, dynamically resized stacks
  • ~10s of bytes
  • No unnecessary closure allocation costs unlike CPS
  • One-shot delimited continuations
  • Simplifjes reasoning about resources - sockets, locks, etc.
  • Handlers —> Linked-list of fjbers

Implementation

handle / continue

handler sp

call chain reference

slide-10
SLIDE 10

Implementation

handle / continue handle / continue

sp handler

call chain reference

  • Fibers: Heap allocated, dynamically resized stacks
  • ~10s of bytes
  • No unnecessary closure allocation costs unlike CPS
  • One-shot delimited continuations
  • Simplifjes reasoning about resources - sockets, locks, etc.
  • Handlers —> Linked-list of fjbers
slide-11
SLIDE 11

perform

sp

handle / continue

Implementation

handler

call chain reference

  • Fibers: Heap allocated, dynamically resized stacks
  • ~10s of bytes
  • No unnecessary closure allocation costs unlike CPS
  • One-shot delimited continuations
  • Simplifjes reasoning about resources - sockets, locks, etc.
  • Handlers —> Linked-list of fjbers
slide-12
SLIDE 12

Native-code fjbers — Vanilla

OCaml start program C call OCaml callback C call OCaml callback

C OCaml C OCaml C OCaml

slide-13
SLIDE 13

C

system stack

Native-code fjbers — Effects

OCaml heap

OCaml start program C call handle OCaml callback C call

C C

  • 1. Stack overfmow checks for OCaml functions
  • Simple static analysis eliminates many checks
  • 2. FFI calls are more expensive due to stack switching
  • Specialise for calls which {allocate / pass arguments on stack / do neither}
slide-14
SLIDE 14

Performance : Vanilla OCaml

Normalised time (lower is better)

Effects ~0.9% slower

0.25 0.5 0.75 1 ae--add_times_nsec_sum_higher_ sequence-cps ae--04124___why_e36d6b_int-T- ae--04298___why_7ae35b_p4_3_ numal-k-means ae--Automaton_i_part2-B_transla ae--01192___why_98479f_p4_3_ ae--fill_assert_39_Ae- ae--00076___why_f2468a_Site_ce ae--00344___why_fb54b2_Foncti numal-fft chameneos-async ae--00224___why_c6049d_p9_17- ae--00020___why_bf6246_euler00 ae--00329___why_265778_p4_25 numal-lu-decomposition numal-levinson-durbin ae--08033___why_bebe52_p4_3_ numal-rnd_access ae--00145___why_0a8ac0_p9_15 ae--00195___fib__package-T-WP ae--02802___step_function_test__ ae--02362___why_be93d3_p4_3_ cpdf-squeeze ae--02182___why_3f7a7d_inverse_ ae--01201___flight_manager__pack thread-ring-async-pipe ae--00222___fib__package-T-WP_ chameneos-lwt ae--00893___why_b3d830_euler001 sequence async_echo_merge setrip thread-sleep-async ae--01012___p__package-T-WP_p thread-ring-lwt-mvar setrip-smallbuf numal-qr-decomposition numal-durand-kerner-aberth cpdf-transform valet-async lexifi-g2pp almabench numal-naive-multilayer jsontrip-sample async_rpc cohttp-lwt cohttp-async frama-c-idct sauvola-contrast cpdf-reformat chameneos-th minilight valet-lwt menhir-fancy js_of_ocaml menhir-standard bdd numal-simple_access cpdf-merge kb patdiff core_micro kb-no-exc ydump-sample frama-c-deflate menhir-sql ae--00115___why_b6d80d_relabel thread-ring-lwt-stream thread-sleep-lwt chameneos-evtchn

Effects Vanilla

slide-15
SLIDE 15

Performance : Chameneos-Redux

Time (S) 0.45 0.9 1.35 1.8 Iterations (X100,000) 1 2 3 4 5 6 7 8 9 10

Lwt Concurrency Monad GHC Fibers

slide-16
SLIDE 16

Performance : Generator

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

slide-17
SLIDE 17

Javascript backend

  • js_of_ocaml
  • OCaml bytecode —> Javascript
  • js_of_ocaml compiler pass
  • Whole-program selective CPS transformation
  • Work-in-progress!
  • Selective CPS translation
slide-18
SLIDE 18

fjn.

Examples: https://github.com/kayceesrk/ocaml-eff-example Multicore OCaml: https://github.com/ocamllabs/ocaml-multicore