一句话:通过虚拟化续延所用的栈空间,在词法效应处理器中完整支持多次性续延,并带来并行执行性能收益。
效应处理器与多次性续延是管理控制流的强大抽象,可简洁模块化地表达非确定性、随机性等。然而在存在栈分配的词法资源(尤其是词法效应处理器)时实现多次性续延十分困难,因为栈复制会使对这些资源的引用失效。本文提出一种新的词法效应处理器实现策略,完整支持多次性续延:核心思想是虚拟化续延所使用的栈空间——为每个栈分配的处理器实例分配虚拟地址,所有经由这些虚拟地址的效应调用都由地址转换机制中介,运行时中的软件内存管理单元(MMU)利用效应处理器的词法作用域规律高效完成转换。作者为词法效应处理器给出新的操作语义,并证明其相对于标准语义的正确性,同时在编译器与运行时系统中实现该方案。与先前支持词法效应处理器的语言相比,该实现完整支持多次性续延从而提升表达力,并因此支持多次性续延的并行执行,带来显著性能提升。
Effect handlers and multishot continuations are powerful abstractions for managing control flow; together, they offer concise and modular ways to express and handle nondeterminism, randomness, and more. However, implementing multishot continuations in the presence of stack-allocated lexical resources—lexical effect handlers in particular—is challenging, since stack copying invalidates references to these resources. We present a novel implementation strategy for lexical effect handlers that fully supports multishot continuations. The key idea is to virtualize the stack space used by continuations. Each stack-allocated handler instance is assigned a virtual address, and all effect invocations through these virtual addresses are mediated by an address translation mechanism. A software-based memory management unit in the runtime system performs these translations efficiently, exploiting the lexical scoping discipline of effect handlers. We capture the essence of our approach via a new operational semantics for lexical effect handlers and prove it correct with respect to the standard semantics. We also implement it in a compiler and runtime system. Compared to prior languages with lexical effect handlers, our implementation increases expressivity by fully supporting multishot continuations—and, as a happy consequence, unlocks significant performance gains by enabling parallel execution of multishot continuations.