From: s5n on 14 Apr 2010 03:44 Hello, all! My question might be slightly off-topic, as it is specific to the STL (and in fact specific to implementation-defined features), but this is the best place i know to ask... std::string typically uses reference counting and CoW to reduce real allocations to a minimum. i understand that this is an implementation- defined detail, not standardized, but in my experience STL implementations do this. What i don't know is: do STL implementations also (typically) do the same for std::vector? i ask because i have a local UTF16 string class for which i was using std::vector<uint16_t> as the underlying container. i switched to std::basic_string<uint16_t>, which is not only semantically closer to my use-case but allows me to take advantage of any refcount/CoW provided by my local std::string implementation. If CoW is also typically implemented for vectors then i can switch back (i'm not yet sure if there's a good reason to, though). The choice of containers doesn't actually affect my public API - my goal here is simply getting the smallest memory footprint i can while still having a relatively high-level interface into the string data (i.e. not a raw array). :-? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 14 Apr 2010 07:59 On 14 avr, 19:44, s5n <sgb...(a)googlemail.com> wrote: > std::string typically uses reference counting and CoW to reduce real > allocations to a minimum. i understand that this is an implementation- > defined detail, not standardized, but in my experience STL > implementations do this. A few did in C++03, but in C++0x none should do it anymore. > What i don't know is: do STL implementations > also (typically) do the same for std::vector? None that I know of. > If CoW is also > typically implemented for vectors then i can switch back (i'm not yet > sure if there's a good reason to, though). The choice of containers > doesn't actually affect my public API - my goal here is simply getting > the smallest memory footprint i can while still having a relatively > high-level interface into the string data (i.e. not a raw array). Well, what about not copying the data uselessly? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: danadam on 14 Apr 2010 08:00 On Apr 14, 8:44 pm, s5n <sgb...(a)googlemail.com> wrote: > > What i don't know is: do STL implementations > also (typically) do the same for std::vector? > I'm not 100% sure, but I think I read in "Effective STL" that std::vector cannot use reference counting. What I am sure, is that std::vector in gcc/g++ doesn't use it (at least in v4.4.1): #include <cstdio> #include <string> #include <vector> using namespace std; int main() { string s1, s2; vector<int> v1, v2; s1 = "abc"; v1.push_back(1); s1 = s2; v2 = v1; printf("s1 = %p\ns2 = %p\n", s1.c_str(), s2.c_str()); printf("\n"); printf("v1 = %p\nv2 = %p\n", &v1[0], &v2[0]); } output: s1 = 0x7f6083d20178 s2 = 0x7f6083d20178 v1 = 0x18ea040 v2 = 0x18ea060 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 14 Apr 2010 08:38 On 14 Apr., 20:44, s5n <sgb...(a)googlemail.com> wrote: > My question might be slightly off-topic, as it is specific to the STL > (and in fact specific to implementation-defined features), but this is > the best place i know to ask... The question is not off-topic at all. > std::string typically uses reference counting and CoW to reduce real > allocations to a minimum. i understand that this is an implementation- > defined detail, not standardized, but in my experience STL > implementations do this. Yes, this is a valid implementation technique for C++03, but no longer valid for C++0x. Todays implementations often take advantage of the small-string optimization, though. > What i don't know is: do STL implementations > also (typically) do the same for std::vector? No, that would contradict the specification. If such implementation were intended to be supported, much more member functions of std::vector would need to allow for invalidation of pointers, iterators, and references, e.g the non-const overloads of at, operator[], etc. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nick Hounsome on 15 Apr 2010 07:34
On 14 Apr, 19:44, s5n <sgb...(a)googlemail.com> wrote: > Hello, all! > > My question might be slightly off-topic, as it is specific to the STL > (and in fact specific to implementation-defined features), but this is > the best place i know to ask... > > std::string typically uses reference counting and CoW to reduce real > allocations to a minimum. i understand that this is an implementation- > defined detail, not standardized, but in my experience STL > implementations do this. What i don't know is: do STL implementations > also (typically) do the same for std::vector? > > i ask because i have a local UTF16 string class for which i was using > std::vector<uint16_t> as the underlying container. i switched to > std::basic_string<uint16_t>, which is not only semantically closer to > my use-case but allows me to take advantage of any refcount/CoW > provided by my local std::string implementation. If CoW is also > typically implemented for vectors then i can switch back (i'm not yet > sure if there's a good reason to, though). The choice of containers > doesn't actually affect my public API - my goal here is simply getting > the smallest memory footprint i can while still having a relatively > high-level interface into the string data (i.e. not a raw array). If you really want CoW you'll have to do it yourself explicitly. Nobody does CoW anymore because it doesn't mix well with threads and everyones into threads these days. You could probably do something fairly general with a smart pointer with explicit copy method: CoWPtr<X> p; p->SomeConstOperation(); // operator -> returns const X* p.CoW()->SomeMutatingOp(); // Cow makes copy if reference count > 1 and returns X* It's not the prettiest solution nor the safest but it is simple and general. Mostly I find that just passing around const string& is perfectly adequate for saving space. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |