Code Monkey home page Code Monkey logo

rclite's Introduction

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.

rclite's People

Contributors

cosmichorrordev avatar fereidani avatar novacrazy avatar tomlegodais avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rclite's Issues

Unable to compile in no_std project

I am trying to use rclite in a rp2040 project that is no_std. However, the library won't compile:

   Compiling rclite v0.2.4
error[E0599]: no method named `fetch_sub` found for struct `AtomicUsize` in the current scope
   --> /Users/michael/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rclite-0.2.4/src/arc.rs:434:26
    |
434 |         if inner.counter.fetch_sub(1, Ordering::Release) != 1 {
    |                          ^^^^^^^^^ method not found in `AtomicUsize`

error[E0599]: no method named `fetch_add` found for struct `AtomicUsize` in the current scope
   --> /Users/michael/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rclite-0.2.4/src/arc.rs:592:42
    |
592 |         let count = self.inner().counter.fetch_add(1, Ordering::Relaxed);
    |                                          ^^^^^^^^^ method not found in `AtomicUsize`

error[E0599]: no method named `fetch_sub` found for struct `AtomicUsize` in the current scope
   --> /Users/michael/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rclite-0.2.4/src/arc.rs:609:33
    |
609 |         if self.inner().counter.fetch_sub(1, Ordering::Release) != 1 {
    |                                 ^^^^^^^^^ method not found in `AtomicUsize`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `rclite` (lib) due to 3 previous errors

I am using an extern allocator embedded-alloc. Any thoughts on what might be going wrong here?

`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. :)

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 */ }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.