1 unstable release
new 0.1.0 | Apr 2, 2025 |
---|
#458 in Concurrency
96 downloads per month
14KB
213 lines
CacheGuard
** A lightweight cache guard that pads atomics to prevent false sharing in concurrent Rust systems **
CacheGuard is primarily used to pad atomic variables to prevent cache invalidation that can occur due to atomic operations. By aligning memory according to the target architecture's cache size rules, CacheGuard
mitigates false sharing and enhances performance, particularly in concurrent environments.
Difference with Crossbeam CachePadded
While both CacheGuard
and Crossbeam CachePadded
aim to mitigate cache invalidation issues, there are key differences:
CacheGuard
is a standalone crate designed specifically for padding atomic types, whereas CrossbeamCachePadded
is a part of the broader crossbeam-utils ecosystem.CacheGuard
is tuned for worst-case and not most common scenarios and also supports a wider range of platforms.CacheGuard
uses a code generation approach to dynamically read rustc target platforms, which simplifies maintenance and allows for tailored library code generation.- This design makes
CacheGuard
more specialized and easier to maintain as it directly targets the needs of preventing cache invalidation in concurrent environments.
Usage
Add CacheGuard
to your Cargo.toml
:
[dependencies]
cacheguard = "0.1"
Wrap your atomic pointers with CacheGuard
to ensure proper memory alignment. For example, you can create a struct that holds two atomic pointers wrapped in CacheGuard
as follows:
use cacheguard::CacheGuard;
use std::sync::atomic::{AtomicUsize, Ordering};
struct ConcurrentStructure {
counter1: CacheGuard<AtomicUsize>,
counter2: CacheGuard<AtomicUsize>,
}
This example demonstrates wrapping two atomic counters within a struct using CacheGuard.
License
This project is licensed under the MIT License.