× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Do I have to be concerned about the overhead of `Rc`?

Chris Morgan
Jul 15, 2015
<p><code>Rc&lt;T&gt;</code> is very, very cheap. It’s not as cheap as <code>T</code> by quite a bit (boxing values is comparatively expensive in micro-optimisation terms), but scarcely less efficient than <code>Box&lt;T&gt;</code>.</p> <p>It’s just like <code>Box</code>, but with an additional couple of words for the strong and weak reference counts, and the only things that need to touch that are creating an <code>Rc</code> (initialises the values), cloning an <code>Rc</code> (increments the refcount) and dropping an <code>Rc</code> (decrements the refcount and runs the destructor if appropriate). In unstable code there’s also downgrading to/upgrading from <code>Weak</code> (similarly trivial stuff).</p> <p>Dereferencing is a simple memory operation just like it is with <code>Box</code>.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31264670/Do%20I%20have%20to%20be%20concerned%20about%20the%20overhead%20of%20%60Rc%60?/31265275">Stack Overflow</a>.</p>
comments powered by Disqus