一句话:提出为并发集合与字典添加线性一致、wait-free 的 size 操作的方法,性能开销低。
并发程序中高效获得数据结构正确大小(元素个数)并非易事:已有方案要么对数据结构做完整快照来计数,要么在所有更新和 size 操作上加全局锁,效率低下。本文提出一种为集合与字典添加并发线性一致 size 操作的方法。理论上,该 size 操作是 wait-free 的,其渐近复杂度与线程数线性相关,且与数据结构规模无关。实践上,作者在 Java 中为跳表、哈希表与树等多种并发数据结构加入 size 操作并评估性能。与快照方案相比,所提线性一致 size 操作的执行速度快数个数量级,同时对原数据结构操作的吞吐量仅造成 1%–20% 的损失。
The size of a data structure (i.e., the number of elements in it) is a widely used property of a data set. However, for concurrent programs, obtaining a correct size efficiently is non-trivial. In fact, the literature does not offer a mechanism to obtain a correct (linearizable) size of a concurrent data set without resorting to inefficient solutions, such as taking a full snapshot of the data structure to count the elements, or acquiring one global lock in all update and size operations. This paper presents a methodology for adding a concurrent linearizable size operation to sets and dictionaries with a relatively low performance overhead. Theoretically, the proposed size operation is wait-free with asymptotic complexity linear in the number of threads (independently of data-structure size). Practically, we evaluated the performance overhead by adding size to various concurrent data structures in Java−a skip list, a hash table and a tree. The proposed linearizable size operation executes faster by orders of magnitude compared to the existing option of taking a snapshot, while incurring a throughput loss of 1%−20% on the original data structure’s operations.