Expect to add a smart pointer with only strong references count

The pointer to the object.

The pointer to the object isn't next to the reference count(s) in memory.

3 Likes

For reference, Rc's definition is:

struct RcBox<T: ?Sized> {
    strong: Cell<usize>,
    weak: Cell<usize>,
    value: T,
}

pub struct Rc<T: ?Sized> {
    ptr: NonNull<RcBox<T>>,
    phantom: PhantomData<RcBox<T>>,
}
2 Likes

This is something I've played with in the past, in addition to trying to unify Arc and Rc into one generic type with a strategy parameter.

Given how perf-sensitive these types are (specifically compilation perf), it's probably a long shot to do so in std, but it's still an experiment I stand behind (though I haven't had the time to see it through personally). Perhaps I'll end up spending some more time on it again...

3 Likes

Well, but you could have the strong+weak counts in the 8 bytes instead of just a strong count. That then makes the "save memory by not having weak" not apply if the stored object is already align 8, as you mention.

the object itself has alignment tho, and most of the time its greater than 8, so would not that mean that the alignment of the hypothetical Rc<T,Cell<usize>, ()> will be greater than 8?

Why do you think? Not all objects include pointers.

you are right, not all objects use pointers. however almost every struct uses {u, f}64 etc. which may make the struct's alignments greater than 8.

There is an implementation of this in the rust/kernel library of Rust for linuxhttps://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/arc.rs#L42. It is based on the kernel's refcount_t and is relatively simple to implement.

In fact, you can implement a separate crate to prove your idea, and then talk about whether to join the standard library.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.