Closed Bug 1927405 Opened 4 months ago Closed 3 months ago

Map/Set clear function is slow

Categories

(Core :: JavaScript Engine, task, P1)

task

Tracking

()

RESOLVED FIXED
134 Branch
Tracking Status
firefox134 --- fixed

People

(Reporter: jandem, Assigned: jandem)

References

Details

(Whiteboard: [sp3])

Attachments

(1 file)

If the Map or Set is not empty, clear() always does a malloc + free. We should change this to instead destroy all elements and then only shrink the table if needed, similar to how remove works.

This also avoids calling init() after the table has been initialized and this simplifies bug 1851662.

Changing this improves the micro-benchmark below from ~105 ms to ~35 ms.

function f() {
    var s = new Set;
    var t = new Date;
    for (var i = 0; i < 1_000_000; i++) {
        s.add(1);
        s.clear();
    }
    print(new Date - t);
    return s;
}
f();

Change clear to destroy all elements and then shrink the table only if needed,
similar to how remove is implemented.

This eliminates a free and malloc if there are only a few elements but it's also
simpler because it avoids calling init more than once which is a footgun.

Severity: -- → N/A
Priority: -- → P1
Pushed by jdemooij@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/a07d438ea22a Simplify and optimize OrderedHashTable::clear. r=jonco
Status: ASSIGNED → RESOLVED
Closed: 3 months ago
Resolution: --- → FIXED
Target Milestone: --- → 134 Branch
Whiteboard: [sp3]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: