skip to main content

Efficient compilation of algebraic effect handlers

Published:15 October 2021Publication History
Skip Abstract Section

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.

Skip Supplemental Material Section

Supplemental Material

Auxiliary Presentation Video

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. Our approach consists of source-to-source transformations. 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 compiler from Eff 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

References

  1. 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 ScholarGoogle ScholarCross RefCross Ref
  2. 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 ScholarGoogle ScholarCross RefCross Ref
  3. 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 ScholarGoogle ScholarCross RefCross Ref
  4. Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop, and Anil Madhavapeddy. 2015. Effective Concurrency through Algebraic Effects. In OCaml Workshop.Google ScholarGoogle Scholar
  5. 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 ScholarGoogle ScholarCross RefCross Ref
  6. Daniel Hillerström, Sam Lindley, and KC Sivaramakrishnan. 2016. Compiling Links Effect Handlers to the OCaml Backend. In OCaml Workshop.Google ScholarGoogle Scholar
  7. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  8. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  9. 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 ScholarGoogle ScholarCross RefCross Ref
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. 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 ScholarGoogle ScholarCross RefCross Ref
  14. 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 ScholarGoogle ScholarCross RefCross Ref
  15. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  16. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  17. Ziga Luksic and Matija Pretnar. 2020. Local algebraic effect theories. J. Funct. Program., 30 (2020), e13. https://doi.org/10.1017/S0956796819000212 Google ScholarGoogle ScholarCross RefCross Ref
  18. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  19. 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 ScholarGoogle ScholarCross RefCross Ref
  20. 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 ScholarGoogle ScholarCross RefCross Ref
  21. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  22. 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 ScholarGoogle ScholarCross RefCross Ref
  23. 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 ScholarGoogle ScholarCross RefCross Ref
  24. 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 ScholarGoogle ScholarCross RefCross Ref
  25. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  26. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  27. 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 ScholarGoogle ScholarCross RefCross Ref
  28. 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 ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Efficient compilation of algebraic effect handlers

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!