Bug 160836 - Array.prototype.map builtin should go on the fast path when constructor===@Array
Summary: Array.prototype.map builtin should go on the fast path when constructor===@Array
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Saam Barati
URL:
Keywords:
Depends on:
Blocks: 160835
  Show dependency treegraph
 
Reported: 2016-08-12 18:49 PDT by Saam Barati
Modified: 2016-08-15 16:33 PDT (History)
12 users (show)

See Also:


Attachments
Patch (7.09 KB, patch)
2016-08-14 16:47 PDT, Caio Lima
no flags Details | Formatted Diff | Diff
Benchmark run 03 (78.44 KB, text/plain)
2016-08-14 16:49 PDT, Caio Lima
no flags Details
patch I was thinking of (609 bytes, patch)
2016-08-15 11:34 PDT, Saam Barati
no flags Details | Formatted Diff | Diff
patch (1.87 KB, patch)
2016-08-15 15:24 PDT, Saam Barati
keith_miller: review+
Details | Formatted Diff | Diff
patch for landing (2.76 KB, patch)
2016-08-15 15:37 PDT, Saam Barati
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Saam Barati 2016-08-12 18:49:31 PDT
...
Comment 1 Caio Lima 2016-08-14 16:47:54 PDT
Created attachment 286038 [details]
Patch
Comment 2 Caio Lima 2016-08-14 16:49:48 PDT
Created attachment 286039 [details]
Benchmark run 03
Comment 3 Saam Barati 2016-08-15 11:29:48 PDT
Comment on attachment 286038 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286038&action=review

> Source/JavaScriptCore/builtins/ArrayPrototype.js:264
> +    if (tempConstructor === @Array) {
> +        result = @Array(length);
> +        
> +        for (var i = 0; i < length; i++) {
> +            if (!(i in array))
> +                continue;
> +            var mappedValue = callback.@call(thisArg, array[i], i, array);
> +            @putByValDirect(result, i, mappedValue);
> +        }
> +        
> +        return result;
> +    }

I don't think this is quite right w.r.t the species symbol.
Comment 4 Saam Barati 2016-08-15 11:30:41 PDT
Comment on attachment 286038 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286038&action=review

> Source/JavaScriptCore/builtins/ArrayPrototype.js:281
> +    if (constructor === @undefined)

I think you can accomplish a speedup on ES6 bench just by making this "constructor === @Array || constructor === @undefined"
Comment 5 Saam Barati 2016-08-15 11:34:20 PDT
Created attachment 286070 [details]
patch I was thinking of

This is like 6-8% faster on ES6 bench.
Keith, does this look correct to you
Comment 6 Saam Barati 2016-08-15 11:44:26 PDT
(In reply to comment #5)
> Created attachment 286070 [details]
> patch I was thinking of
> 
> This is like 6-8% faster on ES6 bench.
> Keith, does this look correct to you

To elaborate, this will cause the FTL to recognize
that we're doing NewArrayWithSize instead
of compiling
x: JSConstant(Array)
y: Construct(@x, ...)

For example, this program will benefit from this:
```
let x = [1,2,3];
for (let i = 0; i < 10000; i++) {
   x.map(f=>f);
}
```
Comment 7 Keith Miller 2016-08-15 11:50:23 PDT
(In reply to comment #6)
> (In reply to comment #5)
> > Created attachment 286070 [details]
> > patch I was thinking of
> > 
> > This is like 6-8% faster on ES6 bench.
> > Keith, does this look correct to you
> 
> To elaborate, this will cause the FTL to recognize
> that we're doing NewArrayWithSize instead
> of compiling
> x: JSConstant(Array)
> y: Construct(@x, ...)
> 
> For example, this program will benefit from this:
> ```
> let x = [1,2,3];
> for (let i = 0; i < 10000; i++) {
>    x.map(f=>f);
> }
> ```

Yeah, I can believe that's a speedup. Although, I think we should support the Array constructor as an "intrinsic" in the DFG, it's probably a trivial change. It might also be worthwhile to make @Array a bytecode intrinsic function.
Comment 8 Saam Barati 2016-08-15 15:09:07 PDT
(In reply to comment #7)
> (In reply to comment #6)
> > (In reply to comment #5)
> > > Created attachment 286070 [details]
> > > patch I was thinking of
> > > 
> > > This is like 6-8% faster on ES6 bench.
> > > Keith, does this look correct to you
> > 
> > To elaborate, this will cause the FTL to recognize
> > that we're doing NewArrayWithSize instead
> > of compiling
> > x: JSConstant(Array)
> > y: Construct(@x, ...)
> > 
> > For example, this program will benefit from this:
> > ```
> > let x = [1,2,3];
> > for (let i = 0; i < 10000; i++) {
> >    x.map(f=>f);
> > }
> > ```
> 
> Yeah, I can believe that's a speedup. Although, I think we should support
> the Array constructor as an "intrinsic" in the DFG, it's probably a trivial
> change. It might also be worthwhile to make @Array a bytecode intrinsic
> function.
I agree. I'll do this in another patch and make it a bytecode intrinsic:
https://bugs.webkit.org/show_bug.cgi?id=160867
Comment 9 Saam Barati 2016-08-15 15:24:48 PDT
Created attachment 286096 [details]
patch
Comment 10 Keith Miller 2016-08-15 15:26:27 PDT
Comment on attachment 286096 [details]
patch

r=me.
Comment 11 Keith Miller 2016-08-15 15:27:03 PDT
I think we should we also apply this to the other Array prototype functions.
Comment 12 Saam Barati 2016-08-15 15:37:50 PDT
Created attachment 286100 [details]
patch for landing
Comment 13 WebKit Commit Bot 2016-08-15 16:33:13 PDT
Comment on attachment 286100 [details]
patch for landing

Clearing flags on attachment: 286100

Committed r204488: <http://trac.webkit.org/changeset/204488>
Comment 14 WebKit Commit Bot 2016-08-15 16:33:19 PDT
All reviewed patches have been landed.  Closing bug.