RcLite: small, fast, and memory-friendly reference counting for Rust

Overview

RcLite: small, fast, and memory-friendly reference counting

Crates.io Documentation MIT licensed Apache 2 licensed

RcLite is a lightweight reference-counting solution for Rust that serves as an alternative to the standard library's reference-counting. It offers both multi-threaded and single-threaded reference counting options with improved performance and reduced memory overhead, boasting at least 50% and up to 100% decrease in memory overhead compared to the standard library reference counting. RcLite is a suitable option when weak references are not necessary and optimizing for performance and memory usage is a priority.

Why use RcLite?

  • It's faster and smaller
  • Uses less memory
  • It provides lightweight drop-in replacements for standard library std::sync::Arc and std::rc::Rc
  • It supports no_std with extern alloc

Why not use RcLite?

  • It does not provide weak references
  • It does not support data as DSTs
  • With RcLite in 64-bit systems, you only can have 4,294,967,296 - 256 live references to an object which requires about 32GBs of ram for holding all these references to this single object location. if you need to have 18,446,744,073,709,551,616 live references to an object, use the standard library. In other systems with smaller pointer sizes like 32-bit, you will have usize::MAX live references limitation that is the same as the standard library.

Comparison

rclite::{Arc,Rc} std::*::{Arc,Rc}
Overhead in 64-bit systems 4 bytes 16 bytes
Overhead in 32-bit systems 4 or 2 bytes 8 bytes
Overhead in 16-bit systems 2 or 1 bytes 4 bytes
Weak References
DST Support

In 64-bit systems, RcLite has an advantage over the standard library's Arc as it can utilize the memory padding area, using only 4 bytes to store the counter. This results in a reduction in memory usage, as there is less memory waste on padding. However, in situations where there is not enough padding available in the structure, RcLite will have an overhead of 8 bytes, which is still half of the standard library's overhead.

For instance, in 64-bit systems, Rc<u32> and Arc<u32> allocate the same amount of memory as Box<u32>, since the Box<u32> allocation will be padded to u64 by the allocator.

In 32-bit and 16-bit systems, the memory overhead of the RcLite will be 50% of the standard library.

RcLite's structure is similar to a Box<(T,counter)>, where the counter is stored after the data, as opposed to being the first field in the standard library. This eliminates the need for pointer calculation when accessing the data, but also restricts RcLite from supporting DSTs.

Features

By default, RcLite uses a counter size of half the word size for 64-bit systems, with the usize-for-small-platforms feature enabled. This is because overflowing a 32-bit counter is harder compared to overflowing 16-bit counters. If you wish to use the half register size on other platforms, you can disable the default features by setting default-features = false. This will result in the use of 16-bit counters on 32-bit platforms and 8-bit counters on 16-bit platforms.

Comments
  • Add Cargo/Miri Test Pipeline

    Add Cargo/Miri Test Pipeline

    The following PR adds a simple pipeline using GitHub Actions to build and test rclite using both Cargo and Miri. The current configuration runs testing on push and pull request to the main branch.

    opened by tomlegodais 7
  • `Borrow` & `make_mut`

    `Borrow` & `make_mut`

    I tried to integrate rclite in a project of mine (jaq), but I can't because jaq needs Rc::make_mut and Borrow. Could you implement these? If so, then I could integrate rclite into jaq, providing you with some real-life performance benchmarks of alloc::rc::Rc vs rclite::Rc. :)

    opened by 01mf02 7
  • Add Cargo/Miri Test Pipeline for Debug/Release

    Add Cargo/Miri Test Pipeline for Debug/Release

    Hi there @fereidani, I had to unfortunately re-create the PR as something had corrupted my previous fork. Here is all the work completed and due to the re-creation this is a single commit. Hope this satisfies all of your needs. Let me know if you need anything else!

    opened by tomlegodais 1
  • Sized Constraint

    Sized Constraint

    This crate requires Sized. The std Arc does not have a size constraint, which is a quite common requirement when using Arc<dyn MyTrait>

    pub struct Arc<T>where
        T: ?Sized{ /* private fields */ }
    
    opened by PSeitz 1
  • Use C layout for `ArcInner` and `RcInner`

    Use C layout for `ArcInner` and `RcInner`

    I'll check to see if anyone points out any issues after I take a nap, otherwise I would say this is good to merge. Both cargo test and miri are happy even with -Z randomize-layout after this change

    Sidenote: It would be good to get CI running with at least cargo test and miri since this uses unsafe

    opened by CosmicHorrorDev 1
Owner
Khashayar Fereidani
Software Engineering M.Sc., Security Researcher, Full Stack Developer
Khashayar Fereidani
Bioinformatics tool for counting guides in CRISPR-screen studies.

guide-counter A better, faster way to count guides in CRISPR screens. Overview guide-counter is a tool for processing FASTQ files from CRISPR screen e

Fulcrum Genomics 21 Oct 17, 2022
A small in-memory key value database for rust

SmollDB Small in-memory key value database for rust This is a small in-memory key-value database, which can be easly backed up in a file or stream and

null 13 Dec 15, 2022
The Rust Reference

The Rust Language Reference This document is the primary reference for the Rust programming language. This document is not normative. It may include d

The Rust Programming Language 851 Jan 2, 2023
Stardust XR Reference Server, now in Rust :D

Stardust XR Reference Server This project is a usable Linux display server that reinvents human-computer interaction for all kinds of XR, from putting

Stardust XR 9 Dec 20, 2022
Blazing fast, memory safe & modern Linux package manager written in Rust.

paket Blazing fast, memory safe & modern Linux package manager written in Rust. Roadmap Version: 0.1 Paket.toml file parsing. (#1, #2) CLI handling (p

null 4 Oct 19, 2023
A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages.

uy A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages. Usage uy not only stores the unit of a v

Lachlan Sneff 19 Aug 8, 2023
A developer-friendly framework for building user interfaces in Rust

Reading: "Fru" as in "fruit" and "i" as in "I" (I am). What is Frui? Frui is a developer-friendly UI framework that makes building user interfaces eas

Frui Framework 1.1k Jan 8, 2023
Human-friendly indexed collections

Indexical: Human-Friendly Indexed Collections Indexical is a library for conveniently and efficiently working with indexed collections of objects. "In

Will Crichton 45 Nov 1, 2023
Library and proc macro to analyze memory usage of data structures in rust.

Allocative: memory profiler for Rust This crate implements a lightweight memory profiler which allows object traversal and memory size introspection.

Meta Experimental 19 Jan 6, 2023
Rust library for concurrent data access, using memory-mapped files, zero-copy deserialization, and wait-free synchronization.

mmap-sync mmap-sync is a Rust crate designed to manage high-performance, concurrent data access between a single writer process and multiple reader pr

Cloudflare 97 Jun 26, 2023
:crab: Small exercises to get you used to reading and writing Rust code!

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

The Rust Programming Language 33.1k Jan 2, 2023
This project contains small exercises to get you used to reading and writing Rust code

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

Cynthia Tran 1 May 24, 2022
Support SIMD low-memory overhead and high-performance adaptive radix tree.

Artful Artful is an adaptive radix tree library for Rust. At a high-level, it's like a BTreeMap. It is based on the implementation of paper, see The A

future 3 Sep 7, 2022
🧪 The versatile and intuitive memory hacking framework.

?? hax ?? About hax is a Rust crate designed to make memory hacking, game hacking, cheat development, and any other low level memory based development

null 16 Dec 18, 2022
A process memory reader and debugger for Windows (x86_64)

Winreader Winreader is a process memory reader and debugger for Windows, implemented and developed in the Rust language, using the official Microsoft

null 31 Feb 15, 2023
A additional Rust compiler pass to detect memory safe bugs of Rust programs.

SafeDrop A additional Rust compiler pass to detect memory safe bugs of Rust programs. SafeDrop performs path-sensitive and field-sensitive inter-proce

Artisan-Lab  (Fn*) 5 Nov 25, 2022
A Rust implementation of HyperLogLog trying to be parsimonious with memory.

?? HyperLogLog-rs This is a Rust library that provides an implementation of the HyperLogLog (HLL) algorithm, trying to be parsimonious with memory. Wh

Luca Cappelletti 10 May 16, 2023
Super-simple, fully Rust powered "memory" (doc store + semantic search) for LLM projects, semantic search, etc.

memex Super simple "memory" for LLM projects, semantic search, etc. Running the service Note that if you're running on Apple silicon (M1/M2/etc.), it'

Spyglass Search 15 Jun 19, 2023
Fegeya Elitebuild, small, powerful build system. Written in Rust.

Fegeya Elitebuild Small, powerful, work-in-progress build system. Written in Rust. Features: No functions (all are built-ins) All variables are global

Ferhat Geçdoğan 25 Nov 9, 2022