-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider mutable representation for heap variant #12
Comments
How does one even allocate a struct like this? #[repr(C)]
struct Inner {
rc: AtomicUsize,
cap: usize,
data: str,
} as I get to the point where Otherwise, I have a little prototype for a version that doesn't point directly to a Which does generate offset instructions on deref, unfortunately. |
In today's Rust, via toil, pain, and suffering. One way is to make it just If you want to keep it a proper DST you might want to look at https://github.com/rust-analyzer/rowan/blob/master/src/arc.rs which does a similar thing (the code originated in triomphe). @CAD97 also authored https://github.com/CAD97/pointer-utils which is a bunch of proper safe abstractions around these ideas. |
@novacrazy You can take a look at triomphe, which povides a Arc and ThinArc (can store DST with the same size of Arc) that can store ad create DST easily and safely. |
I think this is a great idea, I'm wondering if we can store the ref count on the stack though, ideally make cloning even faster, e.g.
|
Uh, no. The whole point of the ref count is that it's shared, as it tracks how many owners exist to the heap memory, and all owners need to see consistent state. The ref count needs to be shared, thus on the heap. |
Lol great point, I forgot about that |
Closing this out because a I switched from reference counted strings to a normal heap/box string after reading about C++'s woes in making reference counted strings, this series of blog posts goes into detail: I'm not opposed to introducing a mutable reference counted version of |
Update Dockerfile
Not my fastest reply ever but … it seems that issues raised in those posts are C++ specific and are not applicable to Rust? I am 0.95 sure that Rust’s COW string would work just fine without any locks. The Arc::make_mut is basically all we need here. The specific thing which would be different between C++ and Rust is that Rust wouldn’t need the |
Currently, the heap variant is
Arc<str>
. This has a couple drawbacks:Arc<str>
points to the start of the ArcInner, not to the start of str, so there's extra offset during deref.An ideal representation would be to store
*const str
, which points:This should allow us to just mutate the string just fine (using
Arc::make_mut
-like API).Admittedly, this increases the complexity of the implementation quit a bit.
The text was updated successfully, but these errors were encountered: