Prev: One question about table driven and function pointer
Next: Using (return val of) member function as default parameter of member function
From: ShaunJ on 5 Nov 2009 06:21 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! ]
From: Seungbeom Kim on 6 Nov 2009 05:27 ShaunJ wrote: > > 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? 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! ]
From: ShaunJ on 6 Nov 2009 09:04 On Nov 6, 2:27 pm, Seungbeom Kim <musip...(a)bawi.org> wrote: > ShaunJ wrote: > > > 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? > > 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). "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! ]
From: tohava on 6 Nov 2009 09:04 On Nov 6, 1:21 am, ShaunJ <sjack...(a)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! ]
From: Nick Hounsome on 6 Nov 2009 22:19
On 5 Nov, 23:21, ShaunJ <sjack...(a)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! ] |