Read this. The customer is always right, but they should first read the user manual ![]()
& and Rc is not about speed, because & will be faster all the time.
& is only memory address. On 64 bit machine it will be 64 bit, and on 32 bit machine it will be 32 bit.
Rc have memory address and two counters of type usize. This means Rc size is 64 * 3 = 192 bit.
Any time copy of & is created 64 bit of data will be created. Any time Rc is created 192 bit of data will be created. So, Rc will take more memory and will be slower compared to &
But Rc can be used everywhere, but & only can be used if you can guaranty that data owner can live longer than data borrowers.
What gdennie comment have to do with your answer to giang299 question?
Also, I don't see how your code corelates to gdennie message.
This is not correct. The reference counters are stored in the allocation the Rc points to, not in the Rc value itself, and are never copied. (If they were copied, something would have to keep all the copies of the counters in sync, which is impossible.) Rc<T>, Box<T>, and &T all have the same size.
The cost of Rc is in updating the counters, not copying them.
You are right. Rc only needs to increase counter on clone(), and decrease counter and check if counter is 0 on Drop()
I think this is one important point not mentioned in the official tutorial. At least I am quite sure it was not mentioned two years ago when I read it, and not mentioning makes it hard to understand how RC types actually can work internally if we do not know it already from other languages like C++. I just checked -- seems to be correct in my book ![]()
IIRC shared_pointer in C++ can store either a pointer to the data and the counter together, when it was created via make_shared(), or it can store two heap pointers to the data and the counter separately, if you use the constructor. That probably contributes to this misunderstanding.
thank you.
Rc is more complex, than that.
Rc is made from outer Rc and inner Rc.
Inner Rc have strong count, weak count and data.
Outer Rc have pointer to inner Rc, phantom data and allocator.
Pointer to inner Rc is 64 bit (on 64 bit system). But how much is phantom data and allocator? I have read about PhantomData, and as I understud it does not take any space as this is only indicator to compiler. At last this is how I understud it. But how much data takes allocator? Is it like PhantomData? Or takes some space?
The allocator will - without using the unstable sockets allocator feature (which requires using nightly) be the empty type Global which just forwards to the global allocator, which will by default be System.
In terms of layout, in practice Rc is just a pointer.
Note that this is pretty much exactly the same behavior as c++'s Deleter parameter to the shared_ptr constructor.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.