wrc

Crates.iowrc
lib.rswrc
version2.1.0
created_at2017-08-25 10:20:04.375709+00
updated_at2026-01-22 07:53:50.378434+00
descriptionA thread-safe weighted reference counting smart-pointer for Rust.
homepagehttps://harton.dev/james/wrc
repositoryhttps://harton.dev/james/wrc
max_upload_size
id28978
size71,441
James Harton (jimsynz)

documentation

https://docs.rs/crate/wrc/

README

WRC

Build Status

A thread-safe weighted reference counting smart-pointer for Rust.

Overview

By using weights instead of direct reference counting WRC requires roughly half as many synchronisation operations and writes to the heap. Every time a WRC is cloned its weight is split in two, with half allocated to the parent and half allocated to the child. When a WRC is dropped its weight is removed from the total. When the total weight declines to zero then the referenced object is dropped.

Features

  • Thread-safe.
  • Efficient.
  • Lightweight.

Examples

Sharing some immutable data between threads:

use wrc::Wrc;
use std::thread;

let five = Wrc::new(5);

for _ in 0..10 {
    let five = five.clone();
    thread::spawn(move || {
        println!("{:?}", five);
    });
}

Sharing a mutable AtomicUsize:

use wrc::Wrc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

let val = Wrc::new(AtomicUsize::new(5));

for _ in 0..10 {
    let val = val.clone();

    thread::spawn(move || {
        let v = val.fetch_add(1, Ordering::SeqCst);
        println!("{:?}", v);
    });
}

Benchmarks

Simple benchmarks have been built using Criterion. Feel free to run cargo bench to compare them. Each benchmark allocates an owned string and places it within the smart pointer before cloning and dropping the pointer 32 times.

Example results (2026, Linux):

arc 32                  time:   [393.03 ns 394.70 ns 396.35 ns]
rc 32                   time:   [50.165 ns 50.917 ns 51.705 ns]
wrc 32                  time:   [302.98 ns 305.44 ns 308.14 ns]

Wrc is faster than Arc because weight-splitting avoids atomic operations on most clones. Results will vary by machine and workload.

License

This software is licensed under the terms of the HL3-FULL, see the LICENSE.md file included with this package for the terms.

This license actively proscribes this software being used by and for some industries, countries and activities. If your usage of this software doesn't comply with the terms of this license, then contact me with the details of your use-case to organise the purchase of a license - the cost of which may include a donation to a suitable charity or NGO.

Commit count: 0

cargo fmt