一句话:以 affinity、uniqueness、locality 三条 mode 轴实现可完全推断、向后兼容的 OCaml 栈分配与原地更新,安全减少堆分配。
减少堆分配(栈分配或原地复用已有内存)通常能提升程序性能,但缺乏安全保证时极易引发 use-after-free,甚至破坏类型健全性。本文提出一套基于 mode(模态)的设计,让程序员能够安全地减少分配:在不可变结构上使用栈分配与原地更新。设计围绕三条 mode 轴展开——affinity、uniqueness 与 locality。这些 mode 与现有 OCaml 代码完全向后兼容,并且可以被完全推断,无需程序员手动标注。该工作使 OCaml 中的手动内存管理既安全又便捷,为把 Rust 的优势引入 OCaml 铺平了道路。摘要未报告具体的基准测试或量化评估结果。
Programmers can often improve the performance of their programs by reducing heap allocations: either by allocating on the stack or reusing existing memory in-place. However, without safety guarantees, these optimizations can easily lead to use-after-free errors and even type unsoundness. In this paper, we present a design based on modes which allows programmers to safely reduce allocations by using stack allocation and in-place updates of immutable structures. We focus on three mode axes: affinity, uniqueness and locality. Modes are fully backwards compatible with existing OCaml code and can be completely inferred. Our work makes manual memory management in OCaml safe and convenient and charts a path towards bringing the benefits of Rust to OCaml.
@article{LorenzenWDEL24,
title = {Oxidizing OCaml with Modal Memory Management},
author = {Anton Lorenzen and Leo White and Stephen Dolan and Richard A. Eisenberg and Sam Lindley},
journal = {Proceedings of the ACM on Programming Languages},
volume = {8},
number = {ICFP},
year = {2024},
doi = {10.1145/3674642}
}