Abstract
The popularity of algebraic effect handlers as a programming language feature for user-defined computational effects is steadily growing. Yet, even though efficient runtime representations have already been studied, most handler-based programs are still much slower than hand-written code.
This paper shows that the performance gap can be drastically narrowed (in some cases even closed) by means of type-and-effect directed optimising compilation. Our approach consists of source-to-source transformations in two phases of the compilation pipeline. Firstly, elementary rewrites, aided by judicious function specialisation, exploit the explicit type and effect information of the compiler’s core language to aggressively reduce handler applications. Secondly, after erasing the effect information further rewrites in the backend of the compiler emit tight code.
This work comes with a practical implementation: an optimising compiler from Eff, an ML style language with algebraic effect handlers, to OCaml. Experimental evaluation with this implementation demonstrates that in a number of benchmarks, our approach eliminates much of the overhead of handlers, outperforms capability-passing style compilation and yields competitive performance compared to hand-written OCaml code as well Multicore OCaml’s dedicated runtime support.
Supplemental Material
- Andrej Bauer and Matija Pretnar. 2014. An Effect System for Algebraic Effects and Handlers. Logical Methods in Computer Science, 10, 4 (2014), https://doi.org/10.2168/LMCS-10(4:9)2014 Google Scholar
Cross Ref
- Andrej Bauer and Matija Pretnar. 2015. Programming with Algebraic Effects and Handlers. Journal of Logic and Algebraic Programming, 84, 1 (2015), 108–123. https://doi.org/10.1016/j.jlamp.2014.02.001 Google Scholar
Cross Ref
- Stephen Dolan, Spiros Eliopoulos, Daniel Hillerström, Anil Madhavapeddy, K. C. Sivaramakrishnan, and Leo White. 2017. Concurrent System Programming with Effect Handlers. In Trends in Functional Programming - 18th International Symposium, TFP 2017, Canterbury, UK, June 19-21, 2017, Revised Selected Papers, Meng Wang and Scott Owens (Eds.) (Lecture Notes in Computer Science, Vol. 10788). Springer, 98–117. https://doi.org/10.1007/978-3-319-89719-6_6 Google Scholar
Cross Ref
- Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop, and Anil Madhavapeddy. 2015. Effective Concurrency through Algebraic Effects. In OCaml Workshop.Google Scholar
- Daniel Hillerström, Sam Lindley, and Robert Atkey. 2020. Effect handlers via generalised continuations. J. Funct. Program., 30 (2020), e5. https://doi.org/10.1017/S0956796820000040 Google Scholar
Cross Ref
- Daniel Hillerström, Sam Lindley, and KC Sivaramakrishnan. 2016. Compiling Links Effect Handlers to the OCaml Backend. In OCaml Workshop.Google Scholar
- Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in action. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional programming (ICFP ’13). ACM, 145–158. https://doi.org/10.1145/2500365.2500590 Google Scholar
Digital Library
- Ohad Kammar and Gordon D. Plotkin. 2012. Algebraic foundations for effect-dependent optimisations. In Proceedings of the 39th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM, 349–360. https://doi.org/10.1145/2103621.2103698 Google Scholar
Digital Library
- Georgios Karachalias, Matija Pretnar, Amr Hany Saleh, Stien Vanderhallen, and Tom Schrijvers. 2020. Explicit effect subtyping. Journal of Functional Programming, 30 (2020), https://doi.org/10.1017/S0956796820000131 Google Scholar
Cross Ref
- Oleg Kiselyov. 2012. Delimited control in OCaml, abstractly and concretely. Theorical Computer Science, 435 (2012), 56–76. https://doi.org/10.1016/j.tcs.2012.02.025 Google Scholar
Digital Library
- Oleg Kiselyov and Hiromi Ishii. 2015. Freer monads, more extensible effects. In Proceedings of the 8th ACM SIGPLAN Symposium on Haskell, Haskell 2015, Vancouver, BC, Canada, September 3-4, 2015, Ben Lippmeier (Ed.). ACM, 94–105. https://doi.org/10.1145/2887747.2804319 Google Scholar
Digital Library
- Oleg Kiselyov, Amr Sabry, and Cameron Swords. 2013. Extensible effects: an alternative to monad transformers. In Proceedings of the 2013 ACM SIGPLAN Symposium on Haskell, Boston, MA, USA, September 23-24, 2013, Chung-chieh Shan (Ed.). 59–70. https://doi.org/10.1145/2578854.2503791 Google Scholar
Digital Library
- Oleg Kiselyov and K. C. Sivaramakrishnan. 2016. Eff Directly in OCaml. In Proceedings ML Family Workshop / OCaml Users and Developers workshops, ML/OCAML 2016, Nara, Japan, September 22-23, 2016, Kenichi Asai and Mark R. Shinwell (Eds.) (EPTCS, Vol. 285). 23–58. https://doi.org/10.4204/EPTCS.285.2 Google Scholar
Cross Ref
- Daan Leijen. 2014. Koka: Programming with Row Polymorphic Effect Types. In Proceedings 5th Workshop on Mathematically Structured Functional Programming, [email protected] 2014, Grenoble, France, 12 April 2014., Paul Levy and Neel Krishnaswami (Eds.) (EPTCS, Vol. 153). 100–126. https://doi.org/10.4204/EPTCS.153.8 Google Scholar
Cross Ref
- Daan Leijen. 2017. Type directed compilation of row-typed algebraic effects. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, POPL 2017, Paris, France, January 18-20, 2017, Giuseppe Castagna and Andrew D. Gordon (Eds.). ACM, 486–499. https://doi.org/10.1145/3093333.3009872 Google Scholar
Digital Library
- Sam Lindley, Conor McBride, and Craig McLaughlin. 2017. Do be do be do. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, POPL 2017, Paris, France, January 18-20, 2017, Giuseppe Castagna and Andrew D. Gordon (Eds.). ACM, 500–514. https://doi.org/10.1145/3009837.3009897 Google Scholar
Digital Library
- Ziga Luksic and Matija Pretnar. 2020. Local algebraic effect theories. J. Funct. Program., 30 (2020), e13. https://doi.org/10.1017/S0956796819000212 Google Scholar
Cross Ref
- Atsushi Ohori and Isao Sasano. 2007. Lightweight fusion by fixed point promotion. In Proceedings of the 34th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2007, Nice, France, January 17-19, 2007, Martin Hofmann and Matthias Felleisen (Eds.). ACM, 143–154. https://doi.org/10.1145/1190216.1190241 Google Scholar
Digital Library
- Gordon D. Plotkin and John Power. 2003. Algebraic Operations and Generic Effects. Applied Categorical Structures, 11, 1 (2003), 69–94. https://doi.org/10.1023/A:1023064908962 Google Scholar
Cross Ref
- Gordon D. Plotkin and Matija Pretnar. 2013. Handling Algebraic Effects. Logical Methods in Computer Science, 9, 4 (2013), https://doi.org/10.2168/LMCS-9(4:23)2013 Google Scholar
Cross Ref
- Matija Pretnar. 2015. An introduction to algebraic effects and handlers, invited tutorial. Electronic Notes in Theoretical Computer Science, 319 (2015), 19–35. https://doi.org/10.1016/j.entcs.2015.12.003 Google Scholar
Digital Library
- Amr Hany Saleh, Georgios Karachalias, Matija Pretnar, and Tom Schrijvers. 2018. Explicit Effect Subtyping. In ESOP (Lecture Notes in Computer Science, Vol. 10801). Springer, 327–354. https://doi.org/10.1007/978-3-319-89884-1_12 Google Scholar
Cross Ref
- Amr Hany Saleh and Tom Schrijvers. 2016. Efficient algebraic effect handlers for Prolog. Theory and Practice of Logic Programming, 16, 5-6 (2016), 884–898. https://doi.org/10.1017/S147106841600034X Google Scholar
Cross Ref
- Tom Schrijvers, Bart Demoen, Benoit Desouter, and Jan Wielemaker. 2013. Delimited continuations for Prolog. Theory and Practice of Logic Programming, 13, 4-5 (2013), 533–546. https://doi.org/10.1017/S1471068413000331 Google Scholar
Cross Ref
- Philipp Schuster, Jonathan Immanuel Brachthäuser, and Klaus Ostermann. 2020. Compiling Effect Handlers in Capability-Passing Style. Proc. ACM Program. Lang., 4, ICFP (2020), Article 93, Aug., 28 pages. https://doi.org/10.1145/3408975 Google Scholar
Digital Library
- Atze van der Ploeg and Oleg Kiselyov. 2014. Reflection without remorse: revealing a hidden sequence to speed up monadic reflection. In Proceedings of the 2014 ACM SIGPLAN symposium on Haskell, Gothenburg, Sweden, September 4-5, 2014, Wouter Swierstra (Ed.). ACM, 133–144. https://doi.org/10.1145/2633357.2633360 Google Scholar
Digital Library
- Nicolas Wu and Tom Schrijvers. 2015. Fusion for Free - Efficient Algebraic Effect Handlers. In Mathematics of Program Construction (LNCS, Vol. 9129). Springer, 302–322. https://doi.org/10.1007/978-3-319-19797-5_15 Google Scholar
Cross Ref
- Ningning Xie and Daan Leijen. 2021. Generalized evidence passing for effect handlers: efficient compilation of effect handlers to C. Proc. ACM Program. Lang., 5, ICFP (2021), 1–30. https://doi.org/10.1145/3473576 Google Scholar
Digital Library
Index Terms
Efficient compilation of algebraic effect handlers
Recommendations
Generalized evidence passing for effect handlers: efficient compilation of effect handlers to C
This paper studies compilation techniques for algebraic effect handlers. In particular, we present a sequence of refinements of algebraic effects, going via multi-prompt delimited control, _generalized evidence passing_, yield bubbling, and finally a ...
Type directed compilation of row-typed algebraic effects
POPL '17Algebraic effect handlers, introduced by Plotkin and Power in 2002,
are recently gaining in popularity as a purely functional approach to
modeling effects. In this article, we give a full overview of
practical algebraic effects in the context of ...
Type directed compilation of row-typed algebraic effects
POPL '17: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming LanguagesAlgebraic effect handlers, introduced by Plotkin and Power in 2002,
are recently gaining in popularity as a purely functional approach to
modeling effects. In this article, we give a full overview of
practical algebraic effects in the context of ...






Comments