Boost logo

Boost Users :

From: François Duranleau (duranlef_at_[hidden])
Date: 2006-08-23 11:45:23


On Wed, 23 Aug 2006, Ovanes Markarian wrote:

> I have a question regarding shared_ptr implementation:
>
>
> currently shared_ptr is implementated so, that it contains:
>
> T * px; // contained pointer
> detail::shared_count pn; // reference counter
>
>
> I am interested to know philosophical thoughts why this kind of decision
> was made to implement the shared_ptr class this way?
>
> I assumed to save space which is consumed by such a pointer one could
> move T pointer (T*) to shared_count class. All shared_ptr classes would
> contain only one instance or (may be better pointer to counter):
>
> detail::shared_count* pn; // reference counter
>
>
> and the approach to retrieve pointer would be:
>
> pn->px //since px would be moved to pn
>
> This would reduce the size of shared_ptr<T> to
> sizeof(detail::shared_count*) which is at least by
> sizeof(detail::shared_count) smaller of may be more if alignment has to
> used.
>
> Are there any arguments which prevent shared_ptr from such
> implementation scenario?

One reason could be a question of performance. Placing 'px' in 'pn' would
require an extra dereference to access the object, as you mention aboce,
and such operations can be costly (that is one reason we usually implement
multi-dimensional arrays as a one-dimensional array with computed offsets:
multiplication is usually faster than derefence).

-- 
François Duranleau
LIGUM, Université de Montréal

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net