移位中间数组:比std::deque更快的替代方案?
Shift-to-Middle Array: A Faster Alternative to Std:Deque?

原始链接: https://github.com/attilatorda/Shift-To-Middle_Array

移位中点数组 (Shift-To-Middle Array) 是一种动态数组,专为优化两端插入和删除而设计,提供比 `std::deque`、`std::vector` 和链表更高性能的替代方案。它实现了两端均摊 O(1) 的插入/删除,快速的 O(1) 随机访问,并且由于其连续的内存存储,比链表具有更好的缓存局部性。与 `std::deque` 不同,它通过在调整大小期间动态地将空间重新分配到中间来避免碎片化块,从而最大限度地减少复制。 基准测试表明,根据 CPU/GPU 的能力(例如多核并行性、SIMD 优化和缓存效率),其性能优于标准数据结构。它被用于高性能队列、游戏引擎、网络和动态序列中。其实现和示例易于获取,同时还提供运行 Java 基准测试的说明。它采用 MIT 许可证授权,欢迎贡献代码。

Hacker News 的讨论主题是 AttilaT 的“移向中间数组”(Shift-to-Middle Array),这是一种旨在替代 `std::deque` 的数据结构,它在两端插入和删除操作更快,并且改善了缓存局部性。它保持连续的内存布局,并将空闲空间重新分配到中间以减少数据移动。 早期的评论提出了关于处理非平凡类型(需要析构函数或移动构造函数)的担忧,建议使用 `static_assert` 检查。其他人则要求 `begin()/end()` 迭代器。 讨论将它与 gap buffer、环形缓冲区和 ExpandingRingBuffer 进行了比较,并对连续空闲空间和非空闲空间提出了疑问。orlp 分享了一个类似的、未完成的项目“devector”,并提出了不同的空闲空间管理策略,以优化内存使用并避免过度分配。讨论还涉及到移向中间数组相对于 `VecDeque`(连续内存)的潜在优势。 一位用户指出基准测试链接已损坏。

原文

The Shift-To-Middle Array is a dynamic array designed to optimize insertions and deletions at both ends, offering a high-performance alternative to std::deque, std::vector, and linked lists. It achieves this while maintaining contiguous memory storage, improving cache locality and enabling efficient parallel processing.

Shift-To-Middle Array

Amortized O(1) insertions & deletions at both ends
Fast random access (O(1))
Better cache locality than linked lists
Supports SIMD & parallel optimizations
Efficient memory usage compared to std::deque

Unlike std::deque, which uses a fragmented block structure, the Shift-To-Middle Array dynamically redistributes space to avoid costly shifts. When resizing, elements are moved toward the middle, ensuring efficient insertions at both ends without excessive copying.

🚀 Time Complexity Comparison

The following table compares the time complexity of Shift-To-Middle Array operations with other common data structures:

Operation ArrayList (std::vector) Linked List Shift-To-Middle Array
Access (by index) O(1) O(n) O(1)
Insertion at head O(n) O(1) O(1) amortized
Insertion at tail O(1) amortized O(1) O(1) amortized
Insertion in middle O(n) O(n) O(n)
Deletion at head O(n) O(1) O(1) amortized
Deletion at tail O(1) O(1) O(1) amortized
Deletion in middle O(n) O(n) O(n)
Cache Locality Excellent Poor Excellent

🏆 Performance Benchmarks

Benchmarks comparing Shift-To-Middle Array vs. std::deque vs. ExpandingRingBuffer vs. std::queue demonstrate that performance improvements depend on CPU and GPU capabilities, such as multi-core parallelism, SIMD optimizations, and cache efficiency.

The benchmarks were compiled using GCC with the -O3 optimization flag, ensuring high-performance execution. Results vary based on hardware specifications and workload characteristics.

📂 Installation & Usage

To use Shift-To-Middle Array in your project:

#include "ShiftToMiddleArray.h"
ShiftToMiddleArray<int> stmArray;
stmArray.insert_head(42);
stmArray.insert_tail(99);
int value = stmArray.get_head();
stmArray.remove_head();
  • High-performance queue structures
  • Game engines & real-time applications
  • Networking (packet buffering, event queues)
  • Dynamic sequences in computational geometry & physics

To run the Java benchmarks, ensure you have the Trove library installed. Compile and execute using:

javac -cp trove-3.0.3.jar; ShiftToMiddleArrayBenchmarkTrove.java
java -cp trove-3.0.3.jar; ShiftToMiddleArrayBenchmarkTrove

Full API reference and benchmarks are available in the Wiki!

📊 Benchmarks & Results

For full benchmark details, check out the benchmarks report. The provided Python scripts can be used to visualize performance metrics from CSV benchmark results.

The Shift-To-Middle Array was developed as part of an effort to create a more efficient implementation strategy for lists and deques. Traditional data structures, such as std::deque and linked lists, suffer from poor cache locality or fragmented memory allocations, leading to inefficiencies. By leveraging contiguous memory, dynamic mid-shifting, and modern CPU optimizations, Shift-To-Middle Array provides a balanced solution for insertion, deletion, and access performance.

This project is licensed under the MIT License.

Contributions are welcome! Feel free to open an issue or pull request.

🚀 Try Shift-To-Middle Array today and optimize your data structures!

联系我们 contact @ memedata.com