--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/0a126eec-3d9a-4103-ac0e-c837722b62a5%40chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CABWS%2BBwGkYGydmLEy3TgNzWSVhc7R7s4E48sKe3Avp7XEEtDXw%40mail.gmail.com.
> I'm proposing to replace map<Key, std::unique_ptr<Value>> with map<Key, Value>
This means that Value has to be copiable or moveable. If the object is not cheaply moveable that means deep moving lot of bytes on insertions/removals. It's not immediate to me why such replacement would be beneficial.
Furthermore, in many of those cases where a container holds a unique_ptr, the lifetime of the owned object is typically larger than its presence in the container (e.g. base::Value). In other words, in many cases people need to std::move() the ownership in and out of the container.
One thing I have not fully understood is: why the difference between map<Key, std::unique_ptr<Value>> and map<Key, Value> isn't just sizeof(std::unique_ptr) i.e. a pointer?
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CA%2ByH71eRhR27f%3DQmMMes46QcfPjR64mdAMyOCHo9ZLF4nz5jag%40mail.gmail.com.
On Fri, May 19, 2017 at 9:23 AM, 'Primiano Tucci' via cxx <c...@chromium.org> wrote:> I'm proposing to replace map<Key, std::unique_ptr<Value>> with map<Key, Value>This means that Value has to be copiable or moveable. If the object is not cheaply moveable that means deep moving lot of bytes on insertions/removals. It's not immediate to me why such replacement would be beneficial.Using map.emplace() means insertions don't have to move anything.I'm also skeptical of the claim that folks are doing this because they have an expensive-to-move type. Do you have examples?
> I'm proposing to replace map<Key, std::unique_ptr<Value>> with map<Key, Value>
This means that Value has to be copiable or moveable. If the object is not cheaply moveable that means deep moving lot of bytes on insertions/removals. It's not immediate to me why such replacement would be beneficial.Furthermore, in many of those cases where a container holds a unique_ptr, the lifetime of the owned object is typically larger than its presence in the container (e.g. base::Value). In other words, in many cases people need to std::move() the ownership in and out of the container.
On Fri, May 19, 2017 at 9:45 AM Jeffrey Yasskin <jyas...@chromium.org> wrote:On Fri, May 19, 2017 at 9:23 AM, 'Primiano Tucci' via cxx <c...@chromium.org> wrote:> I'm proposing to replace map<Key, std::unique_ptr<Value>> with map<Key, Value>This means that Value has to be copiable or moveable. If the object is not cheaply moveable that means deep moving lot of bytes on insertions/removals. It's not immediate to me why such replacement would be beneficial.Using map.emplace() means insertions don't have to move anything.I'm also skeptical of the claim that folks are doing this because they have an expensive-to-move type. Do you have examples?The first 5 I got from codesearch are:1) src/ppapi/host/ppapi_host.h: ResourceHost has a vector2) src/crypto/nss_util.cc: ChromeOSUserData has a vector, a callback, two unique_ptrs3) tools/gn/loader.h: ToolchainRecord has a vector, and an inner class with bunch of strings and other nested objects.
4) ui/gfx/image/image.cc: RepresentationType seems just an enum, not sure why this is a unique_ptr5) net/dns/record_parsed.h: RecordParsed has a string, 3 ints, a unique_ptr, and a base::Time. Not sure how expensive this is to move, but definitely not trivial.
On Fri, May 19, 2017 at 9:45 AM Jeffrey Yasskin <jyas...@chromium.org> wrote:On Fri, May 19, 2017 at 9:23 AM, 'Primiano Tucci' via cxx <c...@chromium.org> wrote:> I'm proposing to replace map<Key, std::unique_ptr<Value>> with map<Key, Value>This means that Value has to be copiable or moveable. If the object is not cheaply moveable that means deep moving lot of bytes on insertions/removals. It's not immediate to me why such replacement would be beneficial.Using map.emplace() means insertions don't have to move anything.I'm also skeptical of the claim that folks are doing this because they have an expensive-to-move type. Do you have examples?The first 5 I got from codesearch are:1) src/ppapi/host/ppapi_host.h: ResourceHost has a vector2) src/crypto/nss_util.cc: ChromeOSUserData has a vector, a callback, two unique_ptrs3) tools/gn/loader.h: ToolchainRecord has a vector, and an inner class with bunch of strings and other nested objects.4) ui/gfx/image/image.cc: RepresentationType seems just an enum, not sure why this is a unique_ptr5) net/dns/record_parsed.h: RecordParsed has a string, 3 ints, a unique_ptr, and a base::Time. Not sure how expensive this is to move, but definitely not trivial.
On Fri, May 19, 2017 at 10:13 AM, Primiano Tucci <prim...@google.com> wrote:On Fri, May 19, 2017 at 9:45 AM Jeffrey Yasskin <jyas...@chromium.org> wrote:On Fri, May 19, 2017 at 9:23 AM, 'Primiano Tucci' via cxx <c...@chromium.org> wrote:> I'm proposing to replace map<Key, std::unique_ptr<Value>> with map<Key, Value>This means that Value has to be copiable or moveable. If the object is not cheaply moveable that means deep moving lot of bytes on insertions/removals. It's not immediate to me why such replacement would be beneficial.Using map.emplace() means insertions don't have to move anything.I'm also skeptical of the claim that folks are doing this because they have an expensive-to-move type. Do you have examples?The first 5 I got from codesearch are:1) src/ppapi/host/ppapi_host.h: ResourceHost has a vector2) src/crypto/nss_util.cc: ChromeOSUserData has a vector, a callback, two unique_ptrs3) tools/gn/loader.h: ToolchainRecord has a vector, and an inner class with bunch of strings and other nested objects.4) ui/gfx/image/image.cc: RepresentationType seems just an enum, not sure why this is a unique_ptr5) net/dns/record_parsed.h: RecordParsed has a string, 3 ints, a unique_ptr, and a base::Time. Not sure how expensive this is to move, but definitely not trivial.#4 is polymorphic case, ImageRep is an abstract class.Regarding vectors - why are they not movable? For example vector<shared_ptr<Foo>> is certainly movable.My motivation behind all this was to reduce number of allocations, and to use memory more efficiently. For example map's node has 4 pointers for internal purposes (libc++), so with payload of pair<Key*, Value*> we're utilizing just 30% of node memory. If Value is embedded directly, the utilization gets higher, reflecting more efficient use of memory. For sets that is even worse, i.e. a set<Value*> allocates 20 bytes, but uses only 4 for the payload.I think that cost of additional allocation / free that unqiue_ptr incurs will almost always be higher than the cost of moving things around. Cost of allocation / free is also very unpredictable, while moving things around is way more trivial and stable.