Initialize with zeros for Array.of

The last CL created an array with length zero and enough capacity, and let FastCreateDataProperty to append values. But, there are unnecessary checks in FastCreateDataProperty when appending values. Thus, it's more efficient to create an array filled with smi zero, and fill the values.

Bug: chromium:1395728
Change-Id: Ibe52c688c260637993983ab25f069ee80b212895
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4086126
Commit-Queue: Choongwoo Han <choongwoo.han@microsoft.com>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84745}
This commit is contained in:
Choongwoo Han 2022-12-07 13:31:47 -08:00 committed by V8 LUCI CQ
parent f5fc2d7fe2
commit 517cd16355
2 changed files with 16 additions and 7 deletions

View File

@ -23,13 +23,7 @@ ArrayOf(
// Allocate an array with PACKED elements kind for fast-path rather than
// calling the constructor which creates an array with HOLEY kind.
if (c != GetArrayFunction()) goto CreateWithConstructor;
if (len > kMaxFastArrayLength) goto CreateWithConstructor;
const smiLen: Smi = 0;
const capacity: intptr = Convert<intptr>(len);
const map: Map = GetFastPackedSmiElementsJSArrayMap();
a = AllocateJSArray(
ElementsKind::PACKED_SMI_ELEMENTS, map, capacity, smiLen,
AllocationFlag::kAllowLargeObjectAllocation);
a = NewJSArrayFilledWithZero(SmiUntag(len)) otherwise CreateWithConstructor;
} label CreateWithConstructor {
typeswitch (c) {
case (c: Constructor): {

View File

@ -87,6 +87,21 @@ macro NewJSArray(implicit context: Context)(): JSArray {
};
}
macro NewJSArrayFilledWithZero(implicit context: Context)(length: intptr):
JSArray labels Slow {
if (length == 0) return NewJSArray();
if (length > kMaxFastArrayLength) goto Slow;
const map: Map = GetFastPackedSmiElementsJSArrayMap();
const elements: FixedArrayBase = AllocateFixedArray(
ElementsKind::PACKED_SMI_ELEMENTS, length,
AllocationFlag::kAllowLargeObjectAllocation);
FillFixedArrayWithSmiZero(
ElementsKind::PACKED_SMI_ELEMENTS, UnsafeCast<FixedArray>(elements), 0,
length);
return NewJSArray(map, elements);
}
// A HeapObject with a JSArray map, and either fast packed elements, or fast
// holey elements when the global NoElementsProtector is not invalidated.
transient type FastJSArray extends JSArray;