一句话:提出基于延续的协程管道实现,消除嵌套减速并显著提升通信密集场景性能。
协程管道便于组织处理数据流的复杂程序,相比惰性流还能让管道两端及中间过程与 I/O 交互。Haskell 的 Pipes 与 Conduit 以直接风格实现:用通信事件的自由单子结合解释器来交错伪并行组合。该实现在进程深度嵌套于顺序或并行组合时出现减速。本文提出基于延续(continuation)的管道实现,避免这一减速;在小型、通信密集的例子中即使未受减速影响也显著更快,速度可与基于惰性流的程序相当。作者还通过代数推理说明该实现可由直接风格实现推导而来。
Coroutine pipelines provide an attractive structuring mechanism for complex programs that process streams of data, with the advantage over lazy streams that both ends of a pipeline may interact with the I/O system, as may processes in the middle. Two popular Haskell libraries, Pipes and Conduit, support such pipelines. In both libraries, pipelines are implemented in a direct style by combining a free monad of communication events with an interpreter for (pseudo-)parallel composition that interleaves the events of its argument processes. These implementations both suffer from a slow-down when processes are deeply nested in sequence or in parallel. We propose an alternative implementation of pipelines based on continuations that does not suffer from this slow-down. What is more, the implementation is significantly faster on small, communication-intensive examples even where they do not suffer from the slow-down, and comparable in speed with similar programs based on lazy streams. We also show that the continuation-based implementation may be derived from the direct-style implementation by algebraic reasoning.