Do less copying when adding elements to HashMap

RESOLVED FIXED

Status

()

RESOLVED FIXED
8 years ago
8 years ago

People

(Reporter: luke, Assigned: luke)

Tracking

Trunk
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: fixed-in-tracemonkey)

Attachments

(1 attachment)

(Assignee)

Description

8 years ago
Created attachment 459012 [details] [diff] [review]
fix

Although it boils away for builtin types, if you are storing a key or value with a non-trivial operator=/copy-constructor, HashMap will copy more than it needs to when inserting elements.  The solution is to make HashTable's interface a bit more fragile, which is fine since its an implementation detail of HashMap/HashSet, and let the caller do the assignment directly.
Attachment #459012 - Flags: review?(bzbarsky)
Comment on attachment 459012 [details] [diff] [review]
fix

Looks fine, but for HashMap (not HashSet, where the operation makes no sense), would it make sense to also add this function:

    bool add(AddPtr &p, const Key &k) {
        Entry *pentry;
        if (!impl.add(p, &pentry))
            return false;
        const_cast<Key &>(pentry->key) = k;
        return true;
    }

(relying on pentry->value being default-constructed already)?  Or would that not work for some reason?
Attachment #459012 - Flags: review?(bzbarsky) → review+
(Assignee)

Comment 2

8 years ago
That would totally work
Let's do that, then.  Then we get no-copy inserts as needed!
(Assignee)

Updated

8 years ago
Whiteboard: fixed-in-tracemonkey

Comment 5

8 years ago
http://hg.mozilla.org/mozilla-central/rev/116265442418
Status: ASSIGNED → RESOLVED
Last Resolved: 8 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.