Open Bug 1927405 Opened 5 days ago Updated 4 days ago

Map/Set clear function is slow

Categories

(Core :: JavaScript Engine, task, P1)

task

Tracking

()

ASSIGNED

People

(Reporter: jandem, Assigned: jandem)

References

(Blocks 1 open bug)

Details

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
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: