arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2312.11257cs.PL
收藏

目标传递风格编程:一种 Haskell 实现

Destination-passing style programming: a Haskell implementation

Thomas Bagrel

AI总结:

本文提出在 Haskell 中基于 compact regions 与线性类型 API 实现目标传递风格编程,以兼顾内存安全与性能,并通过解析器示例证明大规模输入下内存和时间均降低 35%。

摘要:

目标传递风格编程引入了目标(destinations),用于表示一次性写入内存单元的地址。这些目标可以作为函数参数传递,从而使函数调用方能够继续控制内存管理:被调用函数的函数体只需负责填充该内存单元。这在函数式编程语言中尤其有用,因为在这类语言中,函数体通常负责为结果值分配内存。\n在 Haskell 中使用目标进行编程,是一种在不牺牲内存安全性保证的情况下提升某些程序关键部分性能的有趣方式。事实上,借助我提出的线性类型 API,一次性写入内存单元不可能在读取前保持未初始化状态,并且在不再使用时仍会由垃圾回收器释放,从而消除了手动管理内存时可能出现的未初始化读取、内存泄漏或重复释放错误风险。\n在本文中,我提出了一种面向 Haskell 的目标实现,该实现依赖于所谓的 compact regions(紧凑区域)。我特别通过一个简单的解析器示例进行演示:对于大规模输入,基于目标的版本相比朴素对应版本减少了 35% 的内存占用和运行时间。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑