Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/js/typedarray.js

Issue 2775503006: [builtins] Improve performance of array.prototype.filter and map (Closed)
Patch Set: fixes Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 6
7 "use strict"; 7 "use strict";
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
11 // ------------------------------------------------------------------- 11 // -------------------------------------------------------------------
12 // Imports 12 // Imports
13 13
14 // array.js has to come before typedarray.js for this to work 14 // array.js has to come before typedarray.js for this to work
15 var ArrayToString = utils.ImportNow("ArrayToString"); 15 var ArrayToString = utils.ImportNow("ArrayToString");
16 var ArrayValues; 16 var ArrayValues;
17 var GetIterator; 17 var GetIterator;
18 var GetMethod; 18 var GetMethod;
19 var GlobalArray = global.Array; 19 var GlobalArray = global.Array;
20 var GlobalArrayBuffer = global.ArrayBuffer; 20 var GlobalArrayBuffer = global.ArrayBuffer;
21 var GlobalArrayBufferPrototype = GlobalArrayBuffer.prototype; 21 var GlobalArrayBufferPrototype = GlobalArrayBuffer.prototype;
22 var GlobalObject = global.Object; 22 var GlobalObject = global.Object;
23 var InnerArrayFilter;
24 var InnerArrayFind; 23 var InnerArrayFind;
25 var InnerArrayFindIndex; 24 var InnerArrayFindIndex;
26 var InnerArrayJoin; 25 var InnerArrayJoin;
27 var InnerArraySort; 26 var InnerArraySort;
28 var InnerArrayToLocaleString; 27 var InnerArrayToLocaleString;
29 var InternalArray = utils.InternalArray; 28 var InternalArray = utils.InternalArray;
30 var MaxSimple; 29 var MaxSimple;
31 var MinSimple; 30 var MinSimple;
32 var SpeciesConstructor; 31 var SpeciesConstructor;
33 var ToPositiveInteger; 32 var ToPositiveInteger;
(...skipping 18 matching lines...) Expand all
52 endmacro 51 endmacro
53 52
54 TYPED_ARRAYS(DECLARE_GLOBALS) 53 TYPED_ARRAYS(DECLARE_GLOBALS)
55 54
56 var GlobalTypedArray = %object_get_prototype_of(GlobalUint8Array); 55 var GlobalTypedArray = %object_get_prototype_of(GlobalUint8Array);
57 56
58 utils.Import(function(from) { 57 utils.Import(function(from) {
59 ArrayValues = from.ArrayValues; 58 ArrayValues = from.ArrayValues;
60 GetIterator = from.GetIterator; 59 GetIterator = from.GetIterator;
61 GetMethod = from.GetMethod; 60 GetMethod = from.GetMethod;
62 InnerArrayFilter = from.InnerArrayFilter;
63 InnerArrayFind = from.InnerArrayFind; 61 InnerArrayFind = from.InnerArrayFind;
64 InnerArrayFindIndex = from.InnerArrayFindIndex; 62 InnerArrayFindIndex = from.InnerArrayFindIndex;
65 InnerArrayJoin = from.InnerArrayJoin; 63 InnerArrayJoin = from.InnerArrayJoin;
66 InnerArraySort = from.InnerArraySort; 64 InnerArraySort = from.InnerArraySort;
67 InnerArrayToLocaleString = from.InnerArrayToLocaleString; 65 InnerArrayToLocaleString = from.InnerArrayToLocaleString;
68 MaxSimple = from.MaxSimple; 66 MaxSimple = from.MaxSimple;
69 MinSimple = from.MinSimple; 67 MinSimple = from.MinSimple;
70 SpeciesConstructor = from.SpeciesConstructor; 68 SpeciesConstructor = from.SpeciesConstructor;
71 ToPositiveInteger = from.ToPositiveInteger; 69 ToPositiveInteger = from.ToPositiveInteger;
72 ToIndex = from.ToIndex; 70 ToIndex = from.ToIndex;
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 // ES6 draft 08-24-14, section 22.2.3.12 390 // ES6 draft 08-24-14, section 22.2.3.12
393 function TypedArrayForEach(f, receiver) { 391 function TypedArrayForEach(f, receiver) {
394 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray); 392 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
395 393
396 var length = %_TypedArrayGetLength(this); 394 var length = %_TypedArrayGetLength(this);
397 395
398 InnerTypedArrayForEach(f, receiver, this, length); 396 InnerTypedArrayForEach(f, receiver, this, length);
399 } 397 }
400 %FunctionSetLength(TypedArrayForEach, 1); 398 %FunctionSetLength(TypedArrayForEach, 1);
401 399
400 // The following functions cannot be made efficient on sparse arrays while
401 // preserving the semantics, since the calls to the receiver function can add
402 // or delete elements from the array.
403 function InnerTypedArrayFilter(f, receiver, array, length, result) {
404 var result_length = 0;
405 for (var i = 0; i < length; i++) {
406 if (i in array) {
407 var element = array[i];
408 if (%_Call(f, receiver, element, i, array)) {
409 %CreateDataProperty(result, result_length, element);
410 result_length++;
411 }
412 }
413 }
414 return result;
415 }
416
402 417
403 // ES6 draft 07-15-13, section 22.2.3.9 418 // ES6 draft 07-15-13, section 22.2.3.9
404 function TypedArrayFilter(f, thisArg) { 419 function TypedArrayFilter(f, thisArg) {
405 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray); 420 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
406 421
407 var length = %_TypedArrayGetLength(this); 422 var length = %_TypedArrayGetLength(this);
408 if (!IS_CALLABLE(f)) throw %make_type_error(kCalledNonCallable, f); 423 if (!IS_CALLABLE(f)) throw %make_type_error(kCalledNonCallable, f);
409 var result = new InternalArray(); 424 var result = new InternalArray();
410 InnerArrayFilter(f, thisArg, this, length, result); 425 InnerTypedArrayFilter(f, thisArg, this, length, result);
411 var captured = result.length; 426 var captured = result.length;
412 var output = TypedArraySpeciesCreate(this, captured); 427 var output = TypedArraySpeciesCreate(this, captured);
413 for (var i = 0; i < captured; i++) { 428 for (var i = 0; i < captured; i++) {
414 output[i] = result[i]; 429 output[i] = result[i];
415 } 430 }
416 return output; 431 return output;
417 } 432 }
418 %FunctionSetLength(TypedArrayFilter, 1); 433 %FunctionSetLength(TypedArrayFilter, 1);
419 434
420 435
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 %AddNamedProperty(GlobalNAME.prototype, 704 %AddNamedProperty(GlobalNAME.prototype,
690 "constructor", global.NAME, DONT_ENUM); 705 "constructor", global.NAME, DONT_ENUM);
691 %AddNamedProperty(GlobalNAME.prototype, 706 %AddNamedProperty(GlobalNAME.prototype,
692 "BYTES_PER_ELEMENT", ELEMENT_SIZE, 707 "BYTES_PER_ELEMENT", ELEMENT_SIZE,
693 READ_ONLY | DONT_ENUM | DONT_DELETE); 708 READ_ONLY | DONT_ENUM | DONT_DELETE);
694 endmacro 709 endmacro
695 710
696 TYPED_ARRAYS(SETUP_TYPED_ARRAY) 711 TYPED_ARRAYS(SETUP_TYPED_ARRAY)
697 712
698 }) 713 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698