# ringbuffer2 **Repository Path**: liudegui/ringbuffer2 ## Basic Information - **Project Name**: ringbuffer2 - **Description**: 优化jnk0le/Ring-Buffer - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-12-24 - **Last Updated**: 2025-04-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 来自:https://github.com/jnk0le/Ring-Buffer ## 优化要点概述 + 减少原子操作次数:将多次加载的原子变量存储在局部变量中,避免重复加载。 + 优化内存屏障:使用 std::atomic_thread_fence 替代 std::atomic_signal_fence,确保正确的内存顺序。 + 合并重复代码:使用内部辅助模板函数来合并重复的 insert 和 remove 操作。 + 批量数据操作:在 writeBuff 和 readBuff 中使用 std::memcpy 进行批量数据传输,提升缓存利用率和数据传输效率。 + 避免伪共享:通过填充结构体,确保 head 和 tail 位于不同的缓存行中。 + 内存对齐:确保 data_buff 按缓存行对齐,提高缓存命中率。 + 内联小函数:将小型函数内联,减少函数调用开销。 + 使用更高效的数据类型:根据需求调整 index_t 的类型,确保其大小与性能需求匹配。 ## 执行结果 ```.sh $ ./example.sh Inserted: 0 Inserted: 1 Inserted: 2 Inserted: 3 Inserted: 4 Inserted: 5 Inserted: 6 Inserted: 7 Inserted: 8 Inserted: 9 Buffer is Not Empty Buffer is Not Full Removed: 0 Removed: 1 Removed: 2 Removed: 3 Removed: 4 Removed: 5 Removed: 6 Removed: 7 Removed: 8 Removed: 9 Inserted: 100 Peeked Value: 100 Element at index 0: 100 ``` ```.sh $ ./test.sh Starting Functional Test... Buffer correctly identified as full. Buffer correctly identified as empty. Functional Test Passed. Starting Performance Test... Inserted and Removed 1000000 elements in 0.00361691 seconds. Throughput: 2.76479e+08 operations per second. Starting High Frequency Test... High Frequency Test: Inserted and Removed 10000000 elements in 0.0404424 seconds. Throughput: 2.47265e+08 operations per second. ```