Destination-passing style programming introduces destinations, which represent the address of a write-once memory cell. Those destinations can be passed as function parameters, and thus enable the caller of a function to keep control over memory management: the body of the called function will just be responsible of filling that memory cell. This is especially useful in functional programming languages, in which the body of a function is typically responsible for allocation of the result value. Programming with destination in Haskell is an interesting way to improve performance of critical parts of some programs, without sacrificing memory guarantees. Indeed, thanks to a linearly-typed API I present, a write-once memory cell cannot be left uninitialized before being read, and is still disposed of by the garbage collector when it is not in use anymore, eliminating the risk of uninitialized read, memory leak, or double-free errors that can arise when memory is managed manually. In this article, I present an implementation of destinations for Haskell, which relies on so-called compact regions. I demonstrate, in particular, a simple parser example for which the destination-based version uses 35% less memory and time than its naive counterpart for large inputs.
翻译:目标传递风格编程引入了目标,它们代表一次性写入内存单元的地址。这些目标可以作为函数参数传递,从而使函数调用者能够保持对内存管理的控制:被调用函数的主体仅负责填充该内存单元。这在函数式编程语言中尤为有用,因为这类语言中函数主体通常负责结果值的分配。在Haskell中使用目标进行编程,是提升某些程序关键部分性能且不牺牲内存保障的有效方式。得益于我提出的线性类型API,一次性写入内存单元在被读取前不会被遗留未初始化状态,并且在不再使用时仍由垃圾回收器处理,从而消除了手动管理内存时可能出现的未初始化读取、内存泄漏或双重释放错误。本文中,我基于所谓的紧凑区域,展示了Haskell中目标的实现。我特别通过一个简单的解析器示例证明,在处理大规模输入时,基于目标的版本相比朴素版本减少了35%的内存和时间消耗。