Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
When are data structures copied
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
ShaunJ  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 23:21
Newsgroups: comp.lang.c++.moderated
From: ShaunJ <sjack...@gmail.com>
Date: Thu, 5 Nov 2009 17:21:26 CST
Local: Thurs 5 Nov 2009 23:21
Subject: When are data structures copied
Hi,

In the following code snippet, when the <string, vector> pair is
inserted into the map, is it necessary for the contents of the string
and vector to be duplicated, or does this just shuffle pointers
around?

{
string s(100, 'x');
vector<int> v(100);
map<string, vector<int>> m;
m.insert(make_pair(s, v));

}

Is the full 100 bytes of the string s duplicated and then the original
freed? Since the local variables s and v are no longer needed after
the end of the scope immediately following the insert, it seems quite
unnecessary to duplicate and then free the originals. Can this be
avoided? Do any implementations of STL implement copy-on-write
semantics for string or vector?

Thanks,
Shaun

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Seungbeom Kim  
View profile   Translate to Translated (View Original)
 More options 6 Nov, 22:27
Newsgroups: comp.lang.c++.moderated
From: Seungbeom Kim <musip...@bawi.org>
Date: Fri, 6 Nov 2009 16:27:02 CST
Local: Fri 6 Nov 2009 22:27
Subject: Re: When are data structures copied

make_pair creates a new pair object and returns it by value, so "in
principle" s and v will be copied. Of course, the compiler is allowed to
perform any optimizations that doesn't change the observable behavior,
so there's no "guarantee" that the copies will be made.

In C++0x you will be able to use make_pair(T1&&, T2&&) and "move" s and
v, avoiding the copies. As far as I know, the syntax will probably be
m.insert(make_pair(move(s), move(v))) (with all std:: omitted).

--
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ShaunJ  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 02:04
Newsgroups: comp.lang.c++.moderated
From: ShaunJ <sjack...@gmail.com>
Date: Fri, 6 Nov 2009 20:04:41 CST
Local: Sat 7 Nov 2009 02:04
Subject: Re: When are data structures copied
On Nov 6, 2:27 pm, Seungbeom Kim <musip...@bawi.org> wrote:

"In principle" then, would m.insert(pair<string, vector<int>>(s, v));
avoid making a copy? I had been treating make_pair as a syntatic
nicety, but completely equivalent to the constructor of pair.

Cheers,
Shaun

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
tohava  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 02:04
Newsgroups: comp.lang.c++.moderated
From: tohava <toh...@gmail.com>
Date: Fri, 6 Nov 2009 20:04:01 CST
Local: Sat 7 Nov 2009 02:04
Subject: Re: When are data structures copied
On Nov 6, 1:21 am, ShaunJ <sjack...@gmail.com> wrote:

> avoided? Do any implementations of STL implement copy-on-write
> semantics for string or vector?

I'm not 100% sure, but I think STLPort implements string with CoW.

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Hounsome  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 15:19
Newsgroups: comp.lang.c++.moderated
From: Nick Hounsome <nick.houns...@googlemail.com>
Date: Sat, 7 Nov 2009 09:19:31 CST
Local: Sat 7 Nov 2009 15:19
Subject: Re: When are data structures copied
On 5 Nov, 23:21, ShaunJ <sjack...@gmail.com> wrote:

> avoided? Do any implementations of STL implement copy-on-write
> semantics for string or vector?

copy-on-write doesn't work well with threads so nobody uses it much
anymore.

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Maxim Yegorushkin  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 00:23
Newsgroups: comp.lang.c++.moderated
From: Maxim Yegorushkin <maxim.yegorush...@gmail.com>
Date: Sat, 7 Nov 2009 18:23:27 CST
Local: Sun 8 Nov 2009 00:23
Subject: Re: When are data structures copied
On 07/11/09 02:04, tohava wrote:

> On Nov 6, 1:21 am, ShaunJ<sjack...@gmail.com>  wrote:
>> avoided? Do any implementations of STL implement copy-on-write
>> semantics for string or vector?

> I'm not 100% sure, but I think STLPort implements string with CoW.

Current STLPort-5.2.1 and STLPort 4 bundled with the latest Sun C++
compilers do not not use CoW. One most disappointing feature of STLPort
std::string is that the default constructor allocates storage, in other
words, a memory allocation is performed even for empty strings.

GNU std::string uses CoW. I hear that the standard interface of
std::string can not be possibly satisfied by a CoW implementation of
std::string, nevertheless I find CoW implementations of std::string the
most practical.

GNU C++ library also provides another string class
__gnu_cxx::__versa_string, which does not do CoW and I've heard that
there are plans to make it the default std::string implementation in the
future, although the CoW std::string implementation will still be
available under a different name.

--
Max

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
yeroen  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 14:46
Newsgroups: comp.lang.c++.moderated
From: yeroen <gmrehb...@gmail.com>
Date: Mon, 9 Nov 2009 08:46:50 CST
Local: Mon 9 Nov 2009 14:46
Subject: Re: When are data structures copied

> copy-on-write doesn't work well with threads so nobody uses it much
> anymore.

It is perfectly possible to combine CoW and threads when the CoW
classes use atomic-reference counting. This is precisely how the Qt
framework provides fully reentrant implementations of string,
container, and other implicitly shared classes.

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Flinn  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 19:58
Newsgroups: comp.lang.c++.moderated
From: Jeff Flinn <TriumphSprint2...@hotmail.com>
Date: Mon, 9 Nov 2009 13:58:18 CST
Local: Mon 9 Nov 2009 19:58
Subject: Re: When are data structures copied
Do any implementations of STL implement copy-on-write

> semantics for string or vector?

MSVC(VC6)/Dinkumware used to do COW for std::string, but found that
small string optimization provided better overall performance.

Jeff

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SG  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 20:27
Newsgroups: comp.lang.c++.moderated
From: SG <s.gesem...@gmail.com>
Date: Mon, 9 Nov 2009 14:27:05 CST
Local: Mon 9 Nov 2009 20:27
Subject: Re: When are data structures copied
On 7 Nov., 03:04, ShaunJ <sjack...@gmail.com> wrote:

> On Nov 6, 2:27 pm, Seungbeom Kim <musip...@bawi.org> wrote:
> > ShaunJ wrote:

> > > {
> > > string s(100, 'x');
> > > vector<int> v(100);
> > > map<string, vector<int>> m;
> > > m.insert(make_pair(s, v));
> > > }

> > make_pair creates a new pair object and returns it by value, so "in
> > principle" s and v will be copied. Of course, the compiler is allowed to
> > perform any optimizations that doesn't change the observable behavior,
> > so there's no "guarantee" that the copies will be made.

Since s and v are lvalues they will be copied. Now, if that means that
all the characters of that string object are duplicated is another
questions and depends on whether the implementation uses CoW or not.
Recent libstdc++ Versions still use CoW (in a thread-safe way) for
std::string. I'm not sure about std::vector. Probably not.

> > In C++0x you will be able to use make_pair(T1&&, T2&&) and "move" s and
> > v, avoiding the copies. As far as I know, the syntax will probably be
> > m.insert(make_pair(move(s), move(v))) (with all std:: omitted).

> "In principle" then, would m.insert(pair<string, vector<int>>(s, v));
> avoid making a copy?

No. They have to be copied since s and v are lvalues. Bug again, that
doesn't imply that the string's elements are copied (due to CoW). But
CoW is not mandated, only a possibility.

In C++0x you will be able to write

   m.emplace(move(s), move(v));

without having to worry about any copying. The pair object will be
directly constructed "into the map" using its templated constructor
that forwards the rvalues references of move(s) and move(v) to the
constructors of std::string and std::vector.

> I had been treating make_pair as a syntatic nicety, but completely
> equivalent to the constructor of pair.

I think it's safe to say that is is if your compiler supports RVO
(return value optimization) and inlining. You can expect make_pair to
be as efficient as a direct constructor call. If you're concerned
about the performance you could do something like this:

   if (m.find(s)==m.end())
     m[s].swap(v);

This will create a pair with a copied string as key and a default-
constructed vector. The new vector is immediately swapped with v.

Cheers,
SG

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Schwab  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 02:43
Newsgroups: comp.lang.c++.moderated
From: Jeff Schwab <j...@schwabcenter.com>
Date: Mon, 9 Nov 2009 20:43:02 CST
Local: Tues 10 Nov 2009 02:43
Subject: Re: When are data structures copied

Jeff Flinn wrote:
> Do any implementations of STL implement copy-on-write
>> semantics for string or vector?

> MSVC(VC6)/Dinkumware used to do COW for std::string, but found that
> small string optimization provided better overall performance.

How does that preclude CoW for non-small strings?

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
tohava  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 14:22
Newsgroups: comp.lang.c++.moderated
From: tohava <toh...@gmail.com>
Date: Tue, 10 Nov 2009 08:22:05 CST
Local: Tues 10 Nov 2009 14:22
Subject: Re: When are data structures copied
On Nov 9, 10:27 pm, SG <s.gesem...@gmail.com> wrote:

> In C++0x you will be able to write

>    m.emplace(move(s), move(v));

While I agree that this is the best solution, I think it would also
make sense to have container::insert(const T &&value), which makes use
of the move constructor, making m.insert(make_pair(move(s), move(v))
almost as efficient. (I do not remember if C++0X has this or not).

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bo Persson  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 21:26
Newsgroups: comp.lang.c++.moderated
From: "Bo Persson" <b...@gmb.dk>
Date: Tue, 10 Nov 2009 15:26:18 CST
Local: Tues 10 Nov 2009 21:26
Subject: Re: When are data structures copied

Jeff Schwab wrote:
> Jeff Flinn wrote:
>> Do any implementations of STL implement copy-on-write
>>> semantics for string or vector?

>> MSVC(VC6)/Dinkumware used to do COW for std::string, but found that
>> small string optimization provided better overall performance.

> How does that preclude CoW for non-small strings?

It doesn't, it is a separate optimization, but with an overlap in
effect for short strings.

The original use of CoW for std::string was shown not to work in
practice. Due to the semantics it becomes copy-on-potential-write,
which is most often a net loss - especially for multi-threaded use.

Herb Sutter did the measures that surprised most people:

http://www.gotw.ca/gotw/045.htm

Bo Persson

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Seungbeom Kim  
View profile   Translate to Translated (View Original)
 More options 11 Nov, 05:14
Newsgroups: comp.lang.c++.moderated
From: Seungbeom Kim <musip...@bawi.org>
Date: Tue, 10 Nov 2009 23:14:37 CST
Local: Wed 11 Nov 2009 05:14
Subject: Re: When are data structures copied

tohava wrote:
> On Nov 9, 10:27 pm, SG <s.gesem...@gmail.com> wrote:
>> In C++0x you will be able to write

>>    m.emplace(move(s), move(v));

> While I agree that this is the best solution, I think it would also
> make sense to have container::insert(const T &&value), which makes use
> of the move constructor, making m.insert(make_pair(move(s), move(v))
> almost as efficient. (I do not remember if C++0X has this or not).

The latest draft, N2960, has insert member functions that take
rvalue references (P&&, not const P&&, though) for all containers
that have insert member functions.

--
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google