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:
CacheGuardis a standalone crate designed specifically for padding atomic types, whereas CrossbeamCachePaddedis a part of the broader crossbeam-utils ecosystem.CacheGuardis tuned for worst-case and not most common scenarios and also supports a wider range of platforms.CacheGuarduses a code generation approach to dynamically read rustc target platforms, which simplifies maintenance and allows for tailored library code generation.- This design makes
CacheGuardmore 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:
[]
= "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;
use ;
This example demonstrates wrapping two atomic counters within a struct using CacheGuard.
License
This project is licensed under the MIT License.