-
Notifications
You must be signed in to change notification settings - Fork 13.3k
pointer guide, rc and arc #16756
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
pointer guide, rc and arc #16756
Conversation
|
||
`Rc<T>` is named 'arr cee' after the first two letters of 'reference counting.' | ||
`Arc<T>` (said like 'ark') provides an 'atomic reference count'ed type. The | ||
difference is that `Arc<T>` is threadsafe, but more expensive. The types are |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably worth noting the only way in which Arc
is more expensive than Rc
is when actually touching the reference count, i.e. clone
and when dropping (and, I suppose, when calling downgrade
or upgrade
). For general use/Deref
, Arc is just as cheap as Rc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Dunno if this is the sort of example you want, but you can write a nice little persistent list with Rc pretty simply (minimal adaption of my own impl elsewhere):
|
The rc module itself also features some ok examples. |
7486562
to
1ea7d12
Compare
A persistent list is a great example, especially because you can easily create a concurrent abstraction with |
@reem: Your version will blow the stack with recursive destructors if it becomes too large, right? (Not sure if Arc's are more magic than that) |
@gankro probably. I haven't had much time to work on making adamantium a really good library yet. How would you suggest I fix that? |
@reem Check out the unsafe destructor in the sample I posted. Basically (assuming I wrote it write), you have to manually walk the nodes and claim ownership of them one at a time, so that when the parent node gets destroyed, it doesn't trigger the destructor of the child. Thankfully, you only have to walk to the next node while the reference you hold is unique, because any other kind of [A]rc won't trigger a recursive destructor on the child. Thus, even though you potentially walk the whole list every time, it's only amortized O(1) in the number of persistent appends you perform. |
Was doing more work on this, and ran into #16821, so this is on hold until that gets fixed. I am actually not happy with this draft anyway, so I'm just going to close this. Thanks though! |
internal: Adjust a few things for trait assoc item hovers rust-lang/rust-analyzer#15938 (minor turned major wrt diff because of test changes)
Adding a little bit of text to the pointer guide on reference counting.
I'd like some more examples, but I'm not sure of good ones.