Closed Bug 1430800 Opened 6 years ago Closed 6 years ago

Optimize String.raw by using arguments object instead of rest-arguments

Categories

(Core :: JavaScript: Standard Library, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla60
Tracking Status
firefox59 --- wontfix
firefox60 --- fixed

People

(Reporter: anba, Assigned: anba)

Details

Attachments

(1 file, 1 obsolete file)

Using the arguments object instead of rest-arguments seems to generate faster code when String.raw is called with substitutions. 

µ-benchmarks I've used for testing: (The String.raw calls always compute empty strings to avoid benchmarking string allocations. Hopefully this doesn't perturb the results in any way.)
---
// zeroSubst: 20ms -> 20ms
// oneSubst: 370ms -> 125ms
// twoSubst: 450ms -> 215ms

function zeroSubst() {
    var q = 0;
    var t = dateNow();
    for (var i = 0; i < 10000000; ++i) {
        q += String.raw ``.length;
    }
    return [dateNow() - t, q];
}

function oneSubst() {
    var xs = ["", ""];
    var q = 0;
    var t = dateNow();
    for (var i = 0; i < 10000000; ++i) {
        q += String.raw `${xs[i & 1]}`.length;
    }
    return [dateNow() - t, q];
}

function twoSubst() {
    var q = 0;
    var t = dateNow();
    for (var i = 0; i < 10000000; ++i) {
        q += String.raw `${xs[i & 1]}${xs[i & 1]}`.length;
    }
    return [dateNow() - t, q];
}
---
Assignee: nobody → andrebargull
Status: NEW → ASSIGNED
Attached patch bug1430800.patch (obsolete) — Splinter Review
Jan, do you think it's worthwhile to change the String.raw implementation to avoid rest-parameters or is there anything we can do in the JITs to achieve a similar performance improvement like with this patch?
Attachment #8942927 - Flags: feedback?(jdemooij)
Comment on attachment 8942927 [details] [diff] [review]
bug1430800.patch

Review of attachment 8942927 [details] [diff] [review]:
-----------------------------------------------------------------

Good find! Yeah this makes sense because we optimize |arguments| more than rest parameters atm.
Attachment #8942927 - Flags: feedback?(jdemooij) → feedback+
Attached patch bug1430800.patchSplinter Review
This patch replaces the rest-parameter usage in String.raw with an arguments object based on the performance results in comment #0. 

I've also updated the step comments, changed the comparison in step 6 from |literalSegments <= 0| to |literalSegments === 0| (I'll create a spec PR to upstream this change), and added a special-case for |literalSegments === 1| to avoid a performance regression with the new code when String.raw is called with a single literal segment, i.e. String.raw `<literal>`.
Attachment #8942927 - Attachment is obsolete: true
Attachment #8943612 - Flags: review?(evilpies)
Comment on attachment 8943612 [details] [diff] [review]
bug1430800.patch

Review of attachment 8943612 [details] [diff] [review]:
-----------------------------------------------------------------

Nice improvement
Attachment #8943612 - Flags: review?(evilpies) → review+
Pushed by rgurzau@mozilla.com:
https://hg.mozilla.org/integration/mozilla-inbound/rev/1e5c359b9fe3
Optimize String.raw by avoiding rest-parameter array allocation. r=evilpie
Keywords: checkin-needed
https://hg.mozilla.org/mozilla-central/rev/1e5c359b9fe3
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla60
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: