Closed Bug 580633 Opened 11 years ago Closed 11 years ago

Do less copying when adding elements to HashMap

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: luke, Assigned: luke)

Details

(Whiteboard: fixed-in-tracemonkey)

Attachments

(1 file)

Attached patch fixSplinter Review
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+
That would totally work
Let's do that, then.  Then we get no-copy inserts as needed!
Whiteboard: fixed-in-tracemonkey
http://hg.mozilla.org/mozilla-central/rev/116265442418
Status: ASSIGNED → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.