Skip to content

Commit e1163c1

Browse files
ripsawridgeCommit Bot
authored andcommitted
[Builtins] Array.prototype.forEach perf regression on dictionaries
An unnecessary call to ToString() on the array index caused trips to the runtime. The fix also includes performance micro-benchmarks so we'll have a harder time regressing this case in future. [email protected] Bug: v8:8112 Change-Id: I781e8b1bbe2eb56db961cf33b0dca8523868b83d Reviewed-on: https://chromium-review.googlesource.com/1213207 Commit-Queue: Michael Stanton <[email protected]> Reviewed-by: Michael Stanton <[email protected]> Reviewed-by: Tobias Tebbi <[email protected]> Cr-Commit-Position: refs/heads/master@{#55733}
1 parent b4b2daf commit e1163c1

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/builtins/array-foreach.tq

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
module array {
66
macro ArrayForEachTorqueContinuation(
77
context: Context, o: JSReceiver, len: Number, callbackfn: Callable,
8-
thisArg: Object, initial_k: Smi): Object {
8+
thisArg: Object, initial_k: Number): Object {
99
// 5. Let k be 0.
1010
// 6. Repeat, while k < len
11-
for (let k: Smi = initial_k; k < len; k = k + 1) {
11+
for (let k: Number = initial_k; k < len; k = k + 1) {
1212
// 6a. Let Pk be ! ToString(k).
13-
const pK: String = ToString_Inline(context, k);
13+
// k is guaranteed to be a positive integer, hence ToString is
14+
// side-effect free and HasProperty/GetProperty do the conversion inline.
1415

1516
// 6b. Let kPresent be ? HasProperty(O, Pk).
16-
const kPresent: Boolean = HasProperty(context, o, pK);
17+
const kPresent: Boolean = HasProperty_Inline(context, o, k);
1718

1819
// 6c. If kPresent is true, then
1920
if (kPresent == True) {
2021
// 6c. i. Let kValue be ? Get(O, Pk).
21-
const kValue: Object = GetProperty(context, o, pK);
22+
const kValue: Object = GetProperty(context, o, k);
2223

2324
// 6c. ii. Perform ? Call(callbackfn, T, <kValue, k, O>).
2425
Call(context, callbackfn, thisArg, kValue, k, o);
@@ -60,7 +61,7 @@ module array {
6061
try {
6162
const callbackfn: Callable =
6263
cast<Callable>(callback) otherwise Unexpected;
63-
const k: Smi = cast<Smi>(initialK) otherwise Unexpected;
64+
const k: Number = cast<Number>(initialK) otherwise Unexpected;
6465
const number_length: Number = cast<Number>(length) otherwise Unexpected;
6566

6667
return ArrayForEachTorqueContinuation(
@@ -159,7 +160,7 @@ module array {
159160
const thisArg: Object = arguments.length > 1 ? arguments[1] : Undefined;
160161

161162
// Special cases.
162-
let k: Smi = 0;
163+
let k: Number = 0;
163164
try {
164165
return FastArrayForEach(context, o, len, callbackfn, thisArg)
165166
otherwise Bailout;

src/builtins/base.tq

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ extern macro GetProperty(Context, Object, Object): Object;
191191
extern builtin SetProperty(Context, Object, Object, Object);
192192
extern builtin DeleteProperty(Context, Object, Object, LanguageMode);
193193
extern builtin HasProperty(Context, JSReceiver, Object): Boolean;
194+
extern macro HasProperty_Inline(Context, JSReceiver, Object): Boolean;
194195

195196
extern macro ThrowRangeError(Context, constexpr MessageTemplate): never;
196197
extern macro ThrowTypeError(Context, constexpr MessageTemplate): never;

src/code-stub-assembler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,14 @@ class V8_EXPORT_PRIVATE CodeStubAssembler : public compiler::CodeAssembler {
27392739
SloppyTNode<Object> key,
27402740
HasPropertyLookupMode mode);
27412741

2742+
// Due to naming conflict with the builtin function namespace.
2743+
TNode<Oddball> HasProperty_Inline(TNode<Context> context,
2744+
TNode<JSReceiver> object,
2745+
TNode<Object> key) {
2746+
return HasProperty(context, object, key,
2747+
HasPropertyLookupMode::kHasProperty);
2748+
}
2749+
27422750
Node* Typeof(Node* value);
27432751

27442752
TNode<Object> GetSuperConstructor(SloppyTNode<Context> context,

0 commit comments

Comments
 (0)