Closed Bug 1687025 Opened 4 years ago Closed 4 years ago

Inline Array#forEach, Array#map etc.

Categories

(Core :: JavaScript Engine: JIT, enhancement, P2)

enhancement

Tracking

()

RESOLVED FIXED
89 Branch
Tracking Status
firefox89 --- fixed

People

(Reporter: evilpie, Assigned: iain)

References

(Blocks 1 open bug)

Details

(Whiteboard: [sp3])

Attachments

(1 file)

With bug 1666009 we can mark these array functions taking a callback as "LargeInlinableFunction". However we still can't inline them afterwards.

The problem seems to be the line:

var T = arguments.length > 1 ? arguments[1] : void 0;

In particular the arguments[1] access marks the script as uninlineable: https://searchfox.org/mozilla-central/source/js/src/jit/IonAnalysis.cpp#3800
As far as I know we use arguments[1] instead of having a second thisArg parameter because otherwise function.length would be 2. I don't really know what we should be doing here.

Severity: -- → N/A
Priority: -- → P2

Bug 1688033 is adding support for inlining arguments[1]. (It's currently behind the --scalar-replace-arguments flag, but it should land fairly soon.) If I mark ArrayForEach as inlinable in my local build, the following microbenchmark goes from ~18s to ~7s:

function foo(arr) {
    var sum = 0;
    arr.forEach(x => sum += x);
    return sum;
}

function bar(arr) {
    var sum = 0;
    arr.forEach(x => sum += x);
    return sum;
}

with ({}) {}

var input = [];
for (var i = 0; i < 100; i++) {
    input.push(i);
}

for (var i = 0; i < 10000000; i++) {
    assertEq(foo(input), 4950);
    assertEq(bar(input), 4950);
}

(There are two different functions calling forEach because otherwise we can inline the lambda function into the self-hosted code, which is not realistic outside of microbenchmarks.)

At the very least we should mark Array#forEach, Array#map, Array#some, and Array#every as inlinable. Not sure if filter/reduce/find are too big, or if we should inline those too.

Depends on: 1688033

This was ~3x faster in a microbenchmark for forEach, every, and some, and ~2x faster for map.

Assignee: nobody → iireland
Status: NEW → ASSIGNED
Pushed by iireland@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/b4c5cd4f2327 Inline Array#forEach and friends r=evilpie
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → 89 Branch
Blocks: 1801189
Whiteboard: [sp3]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: