From 6598869268194890812
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,25827c9ceb81badc
X-Google-Attributes: gidf78e5,public
From: herbs@cntc.com (Herb Sutter)
Subject: Re: string/cow/algorithm
Date: 1998/08/25
Message-ID: <35e5e970.464926228@nr1.toronto.istar.net>#1/1
X-Deja-AN: 384389517
X-NNTP-Posting-Host: 209.89.87.103
Content-Transfer-Encoding: 7bit
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <35D9AE1C.7553@pratique.fr> <35dc79ad.108635599@nr1.toronto.istar.net> <6rplfg$m3q$1@shell7.ba.best.com>
X-UID: 0000000001
X-Status: $$$$
Content-Type: text/plain; charset=us-ascii
Organization: iSTAR internet Incorporated
Mime-Version: 1.0
Newsgroups: comp.std.c++
Originator: clamage@taumet


ncm@nospam.cantrip.org (Nathan Myers) wrote:
>Herb Sutter<herbs@cntc.com> wrote:
>>The reference-counted version is almost always less efficient (usually
>>by an order of magnitude or two, literally) in code built for possible
>>multi-threaded use, even if a particular program that calls it happens
>>to be single-threaded.
>
>Modern architectures can operate on a reference count in one
>instruction.  Allocating memory for another copy takes a long
>time, and involves a lock itself.  I think Herb's remark above
>is unsupportable for the common case.  I don't doubt there are 
>environments where it is true, but is certainly is not universal,
>nor common.

My remark is not only supportable and common, but correct. :-)

Operating on the reference count is immaterial; I assume that's fast. The
number of locks required is material, because locks are expensive:

1. The string still has to acquire the memory manager's lock when actually
making a copy. Any additional COW-induced overhead is in addition to that
lock, not instead of it.

2. The string must additionally acquire a lock for at least each possibly
mutating operation -- meaning not just copy operations, but also accessors
and mutators. Hence this locking is far more frequent than copies in the
common case.

#1 is minor; #2 dominates. In the common case, copy-on-write is an
order-of-magnitude-or-two (usually closer to two in my common experience)
pessimization in code built for possible multi-threaded use, EVEN IF the
program that calls it is single-threaded.

I'll happily post performance results if you'll agree to buy me dinner at
the Santa Cruz meeting a few weeks from now if I'm right. :-)

Herb


P.S.: See also Murray's "C++ Strategies and Tactics" (pages 70-72) for
statistics on the real utility of COW in even single-threaded programs, MT
overhead aside.


---
Herb Sutter (mailto:herbs@cntc.com)

Current Network Technologies Corp  2695 North Sheridan Way, Suite 150
www.cntc.com www.peerdirect.com    Mississauga Ontario Canada L5K 2N6


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




