cacheguard

Crates.iocacheguard
lib.rscacheguard
version0.1.0
created_at2025-04-02 18:35:18.357597+00
updated_at2025-04-02 18:35:18.357597+00
descriptionA lightweight cache guard that pads atomics to prevent false sharing in concurrent Rust systems
homepage
repositoryhttps://github.com/fereidani/cacheguard
max_upload_size
id1616903
size14,424
Khashayar Fereidani (fereidani)

documentation

https://docs.rs/cacheguard

README

CacheGuard

** A lightweight cache guard that pads atomics to prevent false sharing in concurrent Rust systems **

Crates.io Documentation MIT licensed

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 Crossbeam CachePadded 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.

Commit count: 2

cargo fmt