[array] speed up array.forEach and friends by directly using in-operator

The in-operator has recently been optimized making our smart macros obsolete.
This should give up to 10% speedup on forEach and friends.

BUG=v8:5041

Review-Url: https://codereview.chromium.org/2013873002
Cr-Commit-Position: refs/heads/master@{#36576}
This commit is contained in:
cbruni 2016-05-30 03:01:14 -07:00 committed by Commit bot
parent 144609dae6
commit eff24bef5c
2 changed files with 14 additions and 23 deletions

View File

@ -328,10 +328,9 @@ function SparseMove(array, start_i, del_count, len, num_additional_args) {
// because the receiver is not an array (so we have no choice) or because we
// know we are not deleting or moving a lot of elements.
function SimpleSlice(array, start_i, del_count, len, deleted_elements) {
var is_array = IS_ARRAY(array);
for (var i = 0; i < del_count; i++) {
var index = start_i + i;
if (HAS_INDEX(array, index, is_array)) {
if (HAS_INDEX(array, index)) {
var current = array[index];
%CreateDataProperty(deleted_elements, i, current);
}
@ -340,7 +339,6 @@ function SimpleSlice(array, start_i, del_count, len, deleted_elements) {
function SimpleMove(array, start_i, del_count, len, num_additional_args) {
var is_array = IS_ARRAY(array);
if (num_additional_args !== del_count) {
// Move the existing elements after the elements to be deleted
// to the right position in the resulting array.
@ -348,7 +346,7 @@ function SimpleMove(array, start_i, del_count, len, num_additional_args) {
for (var i = len - del_count; i > start_i; i--) {
var from_index = i + del_count - 1;
var to_index = i + num_additional_args - 1;
if (HAS_INDEX(array, from_index, is_array)) {
if (HAS_INDEX(array, from_index)) {
array[to_index] = array[from_index];
} else {
delete array[to_index];
@ -358,7 +356,7 @@ function SimpleMove(array, start_i, del_count, len, num_additional_args) {
for (var i = start_i; i < len - del_count; i++) {
var from_index = i + del_count;
var to_index = i + num_additional_args;
if (HAS_INDEX(array, from_index, is_array)) {
if (HAS_INDEX(array, from_index)) {
array[to_index] = array[from_index];
} else {
delete array[to_index];
@ -1055,9 +1053,8 @@ function ArraySort(comparefn) {
// or delete elements from the array.
function InnerArrayFilter(f, receiver, array, length, result) {
var result_length = 0;
var is_array = IS_ARRAY(array);
for (var i = 0; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
if (%_Call(f, receiver, element, i, array)) {
%CreateDataProperty(result, result_length, element);
@ -1086,17 +1083,16 @@ function ArrayFilter(f, receiver) {
function InnerArrayForEach(f, receiver, array, length) {
if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
var is_array = IS_ARRAY(array);
if (IS_UNDEFINED(receiver)) {
for (var i = 0; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
f(element, i, array);
}
}
} else {
for (var i = 0; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
%_Call(f, receiver, element, i, array);
}
@ -1119,9 +1115,8 @@ function ArrayForEach(f, receiver) {
function InnerArraySome(f, receiver, array, length) {
if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
var is_array = IS_ARRAY(array);
for (var i = 0; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
if (%_Call(f, receiver, element, i, array)) return true;
}
@ -1146,9 +1141,8 @@ function ArraySome(f, receiver) {
function InnerArrayEvery(f, receiver, array, length) {
if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
var is_array = IS_ARRAY(array);
for (var i = 0; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
if (!%_Call(f, receiver, element, i, array)) return false;
}
@ -1176,9 +1170,8 @@ function ArrayMap(f, receiver) {
var length = TO_LENGTH(array.length);
if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
var result = ArraySpeciesCreate(array, length);
var is_array = IS_ARRAY(array);
for (var i = 0; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
%CreateDataProperty(result, i, %_Call(f, receiver, element, i, array));
}
@ -1317,11 +1310,10 @@ function InnerArrayReduce(callback, current, array, length, argumentsLength) {
throw MakeTypeError(kCalledNonCallable, callback);
}
var is_array = IS_ARRAY(array);
var i = 0;
find_initial: if (argumentsLength < 2) {
for (; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
current = array[i++];
break find_initial;
}
@ -1330,7 +1322,7 @@ function InnerArrayReduce(callback, current, array, length, argumentsLength) {
}
for (; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
current = callback(current, element, i, array);
}
@ -1357,11 +1349,10 @@ function InnerArrayReduceRight(callback, current, array, length,
throw MakeTypeError(kCalledNonCallable, callback);
}
var is_array = IS_ARRAY(array);
var i = length - 1;
find_initial: if (argumentsLength < 2) {
for (; i >= 0; i--) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
current = array[i--];
break find_initial;
}
@ -1370,7 +1361,7 @@ function InnerArrayReduceRight(callback, current, array, length,
}
for (; i >= 0; i--) {
if (HAS_INDEX(array, i, is_array)) {
if (HAS_INDEX(array, i)) {
var element = array[i];
current = callback(current, element, i, array);
}

View File

@ -123,7 +123,7 @@ macro TO_PRIMITIVE_STRING(arg) = (%_ToPrimitive_String(arg));
macro TO_NAME(arg) = (%_ToName(arg));
macro JSON_NUMBER_TO_STRING(arg) = ((%_IsSmi(%IS_VAR(arg)) || arg - arg == 0) ? %_NumberToString(arg) : "null");
macro HAS_OWN_PROPERTY(obj, key) = (%_Call(ObjectHasOwnProperty, obj, key));
macro HAS_INDEX(array, index, is_array) = ((is_array && %_HasFastPackedElements(%IS_VAR(array)) && (index < array.length)) || (index in array));
macro HAS_INDEX(array, index) = (index in array);
# Private names.
macro IS_PRIVATE(sym) = (%SymbolIsPrivate(sym));