Theory and Practice of Coroutines with Snapshots

Aleksandar Prokopec, Fengyun Liu, Michael Wagner
2018 European Conference on Object-Oriented Programming  
While event-driven programming is a widespread model for asynchronous computing, its inherent control flow fragmentation makes event-driven programs notoriously difficult to understand and maintain. Coroutines are a general control flow construct that can eliminate control flow fragmentation. However, coroutines are still missing in many popular languages. This gap is partly caused by the difficulties of supporting suspendable computations in the language runtime. We introduce first-class,
more » ... safe, stackful coroutines with snapshots, which unify many variants of suspendable computing. Our design relies solely on the static metaprogramming support of the host language, without modifying the language implementation or the runtime. We also develop a formal model for type-safe, stackful and delimited coroutines, and we prove the respective safety properties. We show that the model is sufficiently general to express iterators, singleassignment variables, async-await, actors, event streams, backtracking, symmetric coroutines and continuations. Performance evaluations reveal that the proposed metaprogramming-based approach has a decent performance, with workload-dependent overheads of 1.03 − 2.11× compared to equivalent manually written code, and improvements of up to 6× compared to other approaches. ACM Subject Classification Software and its engineering → Coroutines, Software and its engineering → Control structures
doi:10.4230/lipics.ecoop.2018.3 dblp:conf/ecoop/ProkopecL18 fatcat:6e7eipgjrrbd5bu5x4r6oodrvq