[builtins] Reduce resolve element closure overhead in Promise.all.
In Promise.all we used to allocate a fresh closure plus a fresh context for each individual element, which is quite a lot of overhead, especially since this could be shared in a single context for all elements. The only bit of information that is needed for each resolve element closure is the index under which to store the resulting value. With this change we move this index to the "identity hash" field of the JSFunction, which doesn't care about the concrete value anyways, as long as it's not zero (the "no hash" sentinel), and share the rest of the fields in a single outer context for all resolve element closures. This limits the maximum number of elements for Promise.all to 2^21 for now, but that should be fine. Shall we ever see the need for more than this, we can add machinery to overflow to separate context for indices larger than 2^21. This significantly reduces the overhead due to Promise.all on the parallel-async-es2017-native test, with execution time dropping from around 148ms to 133ms, so overall a steady 10% improvement on this benchmark. Bug: v8:7253 Change-Id: I1092da771c4919f3db7129d2b0a244fc26a7b144 Reviewed-on: https://chromium-review.googlesource.com/973283 Reviewed-by: Yang Guo <yangguo@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Cr-Commit-Position: refs/heads/master@{#52134}
This commit is contained in:
parent
80df03e31f
commit
d8658177ba
@ -198,6 +198,50 @@ Node* PromiseBuiltinsAssembler::CreatePromiseContext(Node* native_context,
|
||||
return context;
|
||||
}
|
||||
|
||||
Node* PromiseBuiltinsAssembler::CreatePromiseAllResolveElementContext(
|
||||
Node* promise_capability, Node* native_context) {
|
||||
CSA_ASSERT(this, IsNativeContext(native_context));
|
||||
|
||||
// TODO(bmeurer): Manually fold this into a single allocation.
|
||||
Node* const array_map = LoadContextElement(
|
||||
native_context, Context::JS_ARRAY_PACKED_ELEMENTS_MAP_INDEX);
|
||||
Node* const values_array = AllocateJSArray(PACKED_ELEMENTS, array_map,
|
||||
IntPtrConstant(0), SmiConstant(0));
|
||||
|
||||
Node* const context =
|
||||
CreatePromiseContext(native_context, kPromiseAllResolveElementLength);
|
||||
StoreContextElementNoWriteBarrier(
|
||||
context, kPromiseAllResolveElementRemainingSlot, SmiConstant(1));
|
||||
StoreContextElementNoWriteBarrier(
|
||||
context, kPromiseAllResolveElementCapabilitySlot, promise_capability);
|
||||
StoreContextElementNoWriteBarrier(
|
||||
context, kPromiseAllResolveElementValuesArraySlot, values_array);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
Node* PromiseBuiltinsAssembler::CreatePromiseAllResolveElementFunction(
|
||||
Node* context, Node* index, Node* native_context) {
|
||||
CSA_ASSERT(this, TaggedIsSmi(index));
|
||||
CSA_ASSERT(this, SmiGreaterThan(index, SmiConstant(0)));
|
||||
CSA_ASSERT(this, SmiLessThanOrEqual(
|
||||
index, SmiConstant(PropertyArray::HashField::kMax)));
|
||||
CSA_ASSERT(this, IsNativeContext(native_context));
|
||||
|
||||
Node* const map = LoadContextElement(
|
||||
native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
|
||||
Node* const resolve_info = LoadContextElement(
|
||||
native_context, Context::PROMISE_ALL_RESOLVE_ELEMENT_SHARED_FUN);
|
||||
Node* const resolve =
|
||||
AllocateFunctionWithMapAndContext(map, resolve_info, context);
|
||||
|
||||
STATIC_ASSERT(PropertyArray::kNoHashSentinel == 0);
|
||||
StoreObjectFieldNoWriteBarrier(resolve, JSFunction::kPropertiesOrHashOffset,
|
||||
index);
|
||||
|
||||
return resolve;
|
||||
}
|
||||
|
||||
Node* PromiseBuiltinsAssembler::CreatePromiseResolvingFunctionsContext(
|
||||
Node* promise, Node* debug_event, Node* native_context) {
|
||||
Node* const context =
|
||||
@ -1749,26 +1793,23 @@ Node* PromiseBuiltinsAssembler::PerformPromiseAll(
|
||||
const IteratorRecord& iterator, Label* if_exception,
|
||||
Variable* var_exception) {
|
||||
IteratorBuiltinsAssembler iter_assembler(state());
|
||||
Label close_iterator(this);
|
||||
|
||||
Node* const instrumenting = IsDebugActive();
|
||||
Node* const native_context = LoadNativeContext(context);
|
||||
|
||||
// For catch prediction, don't treat the .then calls as handling it;
|
||||
// instead, recurse outwards.
|
||||
SetForwardingHandlerIfTrue(
|
||||
context, instrumenting,
|
||||
native_context, instrumenting,
|
||||
LoadObjectField(capability, PromiseCapability::kRejectOffset));
|
||||
|
||||
Node* const native_context = LoadNativeContext(context);
|
||||
Node* const array_map = LoadContextElement(
|
||||
native_context, Context::JS_ARRAY_PACKED_ELEMENTS_MAP_INDEX);
|
||||
Node* const values_array = AllocateJSArray(PACKED_ELEMENTS, array_map,
|
||||
IntPtrConstant(0), SmiConstant(0));
|
||||
Node* const remaining_elements = AllocateSmiCell(1);
|
||||
Node* const resolve_element_context =
|
||||
CreatePromiseAllResolveElementContext(capability, native_context);
|
||||
|
||||
VARIABLE(var_index, MachineRepresentation::kTagged, SmiConstant(0));
|
||||
|
||||
Label loop(this, &var_index), break_loop(this);
|
||||
VARIABLE(var_index, MachineRepresentation::kTagged, SmiConstant(1));
|
||||
Label loop(this, &var_index), done_loop(this),
|
||||
too_many_elements(this, Label::kDeferred),
|
||||
close_iterator(this, Label::kDeferred);
|
||||
Goto(&loop);
|
||||
BIND(&loop);
|
||||
{
|
||||
@ -1778,114 +1819,147 @@ Node* PromiseBuiltinsAssembler::PerformPromiseAll(
|
||||
Node* const fast_iterator_result_map =
|
||||
LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
|
||||
Node* const next = iter_assembler.IteratorStep(
|
||||
context, iterator, &break_loop, fast_iterator_result_map, if_exception,
|
||||
var_exception);
|
||||
native_context, iterator, &done_loop, fast_iterator_result_map,
|
||||
if_exception, var_exception);
|
||||
|
||||
// Let nextValue be IteratorValue(next).
|
||||
// If nextValue is an abrupt completion, set iteratorRecord.[[Done]] to
|
||||
// true.
|
||||
// ReturnIfAbrupt(nextValue).
|
||||
Node* const next_value = iter_assembler.IteratorValue(
|
||||
context, next, fast_iterator_result_map, if_exception, var_exception);
|
||||
native_context, next, fast_iterator_result_map, if_exception,
|
||||
var_exception);
|
||||
|
||||
// Let nextPromise be ? Invoke(constructor, "resolve", « nextValue »).
|
||||
Node* const next_promise =
|
||||
InvokeResolve(native_context, constructor, next_value, &close_iterator,
|
||||
var_exception);
|
||||
|
||||
// Let resolveElement be a new built-in function object as defined in
|
||||
// Promise.all Resolve Element Functions.
|
||||
Node* const resolve_context =
|
||||
CreatePromiseContext(native_context, kPromiseAllResolveElementLength);
|
||||
StoreContextElementNoWriteBarrier(
|
||||
resolve_context, kPromiseAllResolveElementIndexSlot, var_index.value());
|
||||
StoreContextElementNoWriteBarrier(
|
||||
resolve_context, kPromiseAllResolveElementRemainingElementsSlot,
|
||||
remaining_elements);
|
||||
StoreContextElementNoWriteBarrier(
|
||||
resolve_context, kPromiseAllResolveElementCapabilitySlot, capability);
|
||||
StoreContextElementNoWriteBarrier(resolve_context,
|
||||
kPromiseAllResolveElementValuesArraySlot,
|
||||
values_array);
|
||||
|
||||
Node* const map = LoadContextElement(
|
||||
native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
|
||||
Node* const resolve_info = LoadContextElement(
|
||||
native_context, Context::PROMISE_ALL_RESOLVE_ELEMENT_SHARED_FUN);
|
||||
Node* const resolve =
|
||||
AllocateFunctionWithMapAndContext(map, resolve_info, resolve_context);
|
||||
// Check if we reached the limit.
|
||||
Node* const index = var_index.value();
|
||||
GotoIf(SmiEqual(index, SmiConstant(PropertyArray::HashField::kMax)),
|
||||
&too_many_elements);
|
||||
|
||||
// Set remainingElementsCount.[[Value]] to
|
||||
// remainingElementsCount.[[Value]] + 1.
|
||||
{
|
||||
Label if_outofrange(this, Label::kDeferred), done(this);
|
||||
IncrementSmiCell(remaining_elements, &if_outofrange);
|
||||
Goto(&done);
|
||||
Node* const remaining_elements_count = LoadContextElement(
|
||||
resolve_element_context, kPromiseAllResolveElementRemainingSlot);
|
||||
StoreContextElementNoWriteBarrier(
|
||||
resolve_element_context, kPromiseAllResolveElementRemainingSlot,
|
||||
SmiAdd(remaining_elements_count, SmiConstant(1)));
|
||||
|
||||
BIND(&if_outofrange);
|
||||
{
|
||||
// If the incremented value is out of Smi range, crash.
|
||||
Abort(AbortReason::kOffsetOutOfRange);
|
||||
}
|
||||
|
||||
BIND(&done);
|
||||
}
|
||||
// Let resolveElement be CreateBuiltinFunction(steps,
|
||||
// « [[AlreadyCalled]],
|
||||
// [[Index]],
|
||||
// [[Values]],
|
||||
// [[Capability]],
|
||||
// [[RemainingElements]] »).
|
||||
// Set resolveElement.[[AlreadyCalled]] to a Record { [[Value]]: false }.
|
||||
// Set resolveElement.[[Index]] to index.
|
||||
// Set resolveElement.[[Values]] to values.
|
||||
// Set resolveElement.[[Capability]] to resultCapability.
|
||||
// Set resolveElement.[[RemainingElements]] to remainingElementsCount.
|
||||
Node* const resolve_element_fun = CreatePromiseAllResolveElementFunction(
|
||||
resolve_element_context, index, native_context);
|
||||
|
||||
// Perform ? Invoke(nextPromise, "then", « resolveElement,
|
||||
// resultCapability.[[Reject]] »).
|
||||
Node* const then =
|
||||
GetProperty(context, next_promise, factory()->then_string());
|
||||
GetProperty(native_context, next_promise, factory()->then_string());
|
||||
GotoIfException(then, &close_iterator, var_exception);
|
||||
|
||||
Node* const then_call = CallJS(
|
||||
CodeFactory::Call(isolate(), ConvertReceiverMode::kNotNullOrUndefined),
|
||||
context, then, next_promise, resolve,
|
||||
native_context, then, next_promise, resolve_element_fun,
|
||||
LoadObjectField(capability, PromiseCapability::kRejectOffset));
|
||||
GotoIfException(then_call, &close_iterator, var_exception);
|
||||
|
||||
// For catch prediction, mark that rejections here are semantically
|
||||
// handled by the combined Promise.
|
||||
SetPromiseHandledByIfTrue(context, instrumenting, then_call, [=]() {
|
||||
SetPromiseHandledByIfTrue(native_context, instrumenting, then_call, [=]() {
|
||||
// Load promiseCapability.[[Promise]]
|
||||
return LoadObjectField(capability, PromiseCapability::kPromiseOffset);
|
||||
});
|
||||
|
||||
// Set index to index + 1
|
||||
var_index.Bind(NumberInc(var_index.value()));
|
||||
// Set index to index + 1.
|
||||
var_index.Bind(SmiAdd(index, SmiConstant(1)));
|
||||
Goto(&loop);
|
||||
}
|
||||
|
||||
BIND(&too_many_elements);
|
||||
{
|
||||
// If there are too many elements (currently more than 2**21-1), raise a
|
||||
// RangeError here (which is caught directly and turned into a rejection)
|
||||
// of the resulting promise. We could gracefully handle this case as well
|
||||
// and support more than this number of elements by going to a separate
|
||||
// function and pass the larger indices via a separate context, but it
|
||||
// doesn't seem likely that we need this, and it's unclear how the rest
|
||||
// of the system deals with 2**21 live Promises anyways.
|
||||
Node* const result =
|
||||
CallRuntime(Runtime::kThrowRangeError, native_context,
|
||||
SmiConstant(MessageTemplate::kTooManyElementsInPromiseAll));
|
||||
GotoIfException(result, &close_iterator, var_exception);
|
||||
Unreachable();
|
||||
}
|
||||
|
||||
BIND(&close_iterator);
|
||||
{
|
||||
// Exception must be bound to a JS value.
|
||||
CSA_ASSERT(this, IsNotTheHole(var_exception->value()));
|
||||
iter_assembler.IteratorCloseOnException(context, iterator, if_exception,
|
||||
var_exception);
|
||||
iter_assembler.IteratorCloseOnException(native_context, iterator,
|
||||
if_exception, var_exception);
|
||||
}
|
||||
|
||||
BIND(&break_loop);
|
||||
BIND(&done_loop);
|
||||
{
|
||||
Label resolve_promise(this), return_promise(this);
|
||||
Label resolve_promise(this, Label::kDeferred), return_promise(this);
|
||||
// Set iteratorRecord.[[Done]] to true.
|
||||
// Set remainingElementsCount.[[Value]] to
|
||||
// remainingElementsCount.[[Value]] - 1.
|
||||
Node* const remaining = DecrementSmiCell(remaining_elements);
|
||||
Branch(SmiEqual(remaining, SmiConstant(0)), &resolve_promise,
|
||||
&return_promise);
|
||||
Node* remaining_elements_count = LoadContextElement(
|
||||
resolve_element_context, kPromiseAllResolveElementRemainingSlot);
|
||||
remaining_elements_count = SmiSub(remaining_elements_count, SmiConstant(1));
|
||||
StoreContextElementNoWriteBarrier(resolve_element_context,
|
||||
kPromiseAllResolveElementRemainingSlot,
|
||||
remaining_elements_count);
|
||||
GotoIf(SmiEqual(remaining_elements_count, SmiConstant(0)),
|
||||
&resolve_promise);
|
||||
|
||||
// Pre-allocate the backing store for the {values_array} to the desired
|
||||
// capacity here. We may already have elements here in case of some
|
||||
// fancy Thenable that calls the resolve callback immediately, so we need
|
||||
// to handle that correctly here.
|
||||
Node* const values_array = LoadContextElement(
|
||||
resolve_element_context, kPromiseAllResolveElementValuesArraySlot);
|
||||
Node* const old_elements = LoadElements(values_array);
|
||||
Node* const old_capacity = LoadFixedArrayBaseLength(old_elements);
|
||||
Node* const new_capacity = var_index.value();
|
||||
GotoIf(SmiGreaterThanOrEqual(old_capacity, new_capacity), &return_promise);
|
||||
Node* const new_elements =
|
||||
AllocateFixedArray(PACKED_ELEMENTS, new_capacity, SMI_PARAMETERS,
|
||||
AllocationFlag::kAllowLargeObjectAllocation);
|
||||
CopyFixedArrayElements(PACKED_ELEMENTS, old_elements, PACKED_ELEMENTS,
|
||||
new_elements, SmiConstant(0), old_capacity,
|
||||
new_capacity, UPDATE_WRITE_BARRIER, SMI_PARAMETERS);
|
||||
StoreObjectField(values_array, JSArray::kElementsOffset, new_elements);
|
||||
Goto(&return_promise);
|
||||
|
||||
// If remainingElementsCount.[[Value]] is 0, then
|
||||
// Let valuesArray be CreateArrayFromList(values).
|
||||
// Perform ? Call(resultCapability.[[Resolve]], undefined,
|
||||
// « valuesArray »).
|
||||
BIND(&resolve_promise);
|
||||
|
||||
Node* const resolve =
|
||||
LoadObjectField(capability, PromiseCapability::kResolveOffset);
|
||||
Node* const resolve_call = CallJS(
|
||||
CodeFactory::Call(isolate(), ConvertReceiverMode::kNullOrUndefined),
|
||||
context, resolve, UndefinedConstant(), values_array);
|
||||
GotoIfException(resolve_call, if_exception, var_exception);
|
||||
Goto(&return_promise);
|
||||
{
|
||||
Node* const resolve =
|
||||
LoadObjectField(capability, PromiseCapability::kResolveOffset);
|
||||
Node* const values_array = LoadContextElement(
|
||||
resolve_element_context, kPromiseAllResolveElementValuesArraySlot);
|
||||
Node* const resolve_call = CallJS(
|
||||
CodeFactory::Call(isolate(), ConvertReceiverMode::kNullOrUndefined),
|
||||
native_context, resolve, UndefinedConstant(), values_array);
|
||||
GotoIfException(resolve_call, if_exception, var_exception);
|
||||
Goto(&return_promise);
|
||||
}
|
||||
|
||||
// Return resultCapability.[[Promise]].
|
||||
BIND(&return_promise);
|
||||
@ -1896,31 +1970,6 @@ Node* PromiseBuiltinsAssembler::PerformPromiseAll(
|
||||
return promise;
|
||||
}
|
||||
|
||||
Node* PromiseBuiltinsAssembler::IncrementSmiCell(Node* cell,
|
||||
Label* if_overflow) {
|
||||
CSA_SLOW_ASSERT(this, HasInstanceType(cell, CELL_TYPE));
|
||||
Node* value = LoadCellValue(cell);
|
||||
CSA_SLOW_ASSERT(this, TaggedIsSmi(value));
|
||||
|
||||
if (if_overflow != nullptr) {
|
||||
GotoIf(SmiEqual(value, SmiConstant(Smi::kMaxValue)), if_overflow);
|
||||
}
|
||||
|
||||
Node* result = SmiAdd(value, SmiConstant(1));
|
||||
StoreCellValue(cell, result, SKIP_WRITE_BARRIER);
|
||||
return result;
|
||||
}
|
||||
|
||||
Node* PromiseBuiltinsAssembler::DecrementSmiCell(Node* cell) {
|
||||
CSA_SLOW_ASSERT(this, HasInstanceType(cell, CELL_TYPE));
|
||||
Node* value = LoadCellValue(cell);
|
||||
CSA_SLOW_ASSERT(this, TaggedIsSmi(value));
|
||||
|
||||
Node* result = SmiSub(value, SmiConstant(1));
|
||||
StoreCellValue(cell, result, SKIP_WRITE_BARRIER);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ES#sec-promise.all
|
||||
// Promise.all ( iterable )
|
||||
TF_BUILTIN(PromiseAll, PromiseBuiltinsAssembler) {
|
||||
@ -1977,64 +2026,96 @@ TF_BUILTIN(PromiseAll, PromiseBuiltinsAssembler) {
|
||||
TF_BUILTIN(PromiseAllResolveElementClosure, PromiseBuiltinsAssembler) {
|
||||
Node* const value = Parameter(Descriptor::kValue);
|
||||
Node* const context = Parameter(Descriptor::kContext);
|
||||
|
||||
CSA_ASSERT(this, SmiEqual(LoadFixedArrayBaseLength(context),
|
||||
SmiConstant(kPromiseAllResolveElementLength)));
|
||||
|
||||
Node* const index =
|
||||
LoadContextElement(context, kPromiseAllResolveElementIndexSlot);
|
||||
Node* const values_array =
|
||||
LoadContextElement(context, kPromiseAllResolveElementValuesArraySlot);
|
||||
Node* const function = LoadFromFrame(StandardFrameConstants::kFunctionOffset);
|
||||
|
||||
Label already_called(this, Label::kDeferred), resolve_promise(this);
|
||||
GotoIf(SmiLessThan(index, SmiConstant(Smi::kZero)), &already_called);
|
||||
StoreContextElementNoWriteBarrier(context, kPromiseAllResolveElementIndexSlot,
|
||||
SmiConstant(-1));
|
||||
|
||||
// Set element in FixedArray
|
||||
Label runtime_set_element(this), did_set_element(this);
|
||||
GotoIfNot(TaggedIsPositiveSmi(index), &runtime_set_element);
|
||||
// We use the {function}s context as the marker to remember whether this
|
||||
// resolve element closure was already called. It points to the resolve
|
||||
// element context (which is a FunctionContext) until it was called the
|
||||
// first time, in which case we make it point to the native context here
|
||||
// to mark this resolve element closure as done.
|
||||
GotoIf(IsNativeContext(context), &already_called);
|
||||
CSA_ASSERT(this, SmiEqual(LoadFixedArrayBaseLength(context),
|
||||
SmiConstant(kPromiseAllResolveElementLength)));
|
||||
Node* const native_context = LoadNativeContext(context);
|
||||
StoreObjectField(function, JSFunction::kContextOffset, native_context);
|
||||
|
||||
// Determine the index from the {function}.
|
||||
Label unreachable(this, Label::kDeferred);
|
||||
STATIC_ASSERT(PropertyArray::kNoHashSentinel == 0);
|
||||
Node* const identity_hash =
|
||||
LoadJSReceiverIdentityHash(function, &unreachable);
|
||||
CSA_ASSERT(this, IntPtrGreaterThan(identity_hash, IntPtrConstant(0)));
|
||||
Node* const index = IntPtrSub(identity_hash, IntPtrConstant(1));
|
||||
|
||||
// Check if we need to grow the [[ValuesArray]] to store {value} at {index}.
|
||||
Node* const values_array =
|
||||
LoadContextElement(context, kPromiseAllResolveElementValuesArraySlot);
|
||||
Node* const elements = LoadElements(values_array);
|
||||
Node* const values_length =
|
||||
LoadAndUntagObjectField(values_array, JSArray::kLengthOffset);
|
||||
Label if_inbounds(this), if_outofbounds(this), done(this);
|
||||
Branch(IntPtrLessThan(index, values_length), &if_inbounds, &if_outofbounds);
|
||||
|
||||
BIND(&if_outofbounds);
|
||||
{
|
||||
VARIABLE(var_elements, MachineRepresentation::kTagged,
|
||||
LoadElements(values_array));
|
||||
// Check that the {values_array} is still in fast mode.
|
||||
Node* const elements_kind = LoadMapElementsKind(LoadMap(values_array));
|
||||
GotoIf(Word32Equal(elements_kind, Int32Constant(DICTIONARY_ELEMENTS)),
|
||||
&runtime_set_element);
|
||||
PossiblyGrowElementsCapacity(SMI_PARAMETERS, PACKED_ELEMENTS, values_array,
|
||||
index, &var_elements, SmiConstant(1),
|
||||
&runtime_set_element);
|
||||
StoreFixedArrayElement(var_elements.value(), index, value,
|
||||
UPDATE_WRITE_BARRIER, 0, SMI_PARAMETERS);
|
||||
// Check if we need to grow the backing store.
|
||||
Node* const new_length = IntPtrAdd(index, IntPtrConstant(1));
|
||||
Node* const elements_length =
|
||||
LoadAndUntagObjectField(elements, FixedArray::kLengthOffset);
|
||||
Label if_grow(this, Label::kDeferred), if_nogrow(this);
|
||||
Branch(IntPtrLessThan(index, elements_length), &if_nogrow, &if_grow);
|
||||
|
||||
// Update array length
|
||||
Label did_set_length(this);
|
||||
Node* const length = LoadJSArrayLength(values_array);
|
||||
GotoIfNot(TaggedIsPositiveSmi(length), &did_set_length);
|
||||
Node* const new_length = SmiAdd(index, SmiConstant(1));
|
||||
GotoIfNot(SmiLessThan(length, new_length), &did_set_length);
|
||||
StoreObjectFieldNoWriteBarrier(values_array, JSArray::kLengthOffset,
|
||||
new_length);
|
||||
// Assert that valuesArray.[[Length]] is less than or equal to the
|
||||
// elements backing-store length.e
|
||||
CSA_SLOW_ASSERT(
|
||||
this, SmiAboveOrEqual(LoadFixedArrayBaseLength(var_elements.value()),
|
||||
new_length));
|
||||
Goto(&did_set_length);
|
||||
BIND(&did_set_length);
|
||||
BIND(&if_grow);
|
||||
{
|
||||
// We need to grow the backing store to fit the {index} as well.
|
||||
Node* const new_elements_length =
|
||||
IntPtrMin(CalculateNewElementsCapacity(new_length),
|
||||
IntPtrConstant(PropertyArray::HashField::kMax + 1));
|
||||
CSA_ASSERT(this, IntPtrLessThan(index, new_elements_length));
|
||||
CSA_ASSERT(this, IntPtrLessThan(elements_length, new_elements_length));
|
||||
Node* const new_elements = AllocateFixedArray(
|
||||
PACKED_ELEMENTS, new_elements_length, INTPTR_PARAMETERS,
|
||||
AllocationFlag::kAllowLargeObjectAllocation);
|
||||
CopyFixedArrayElements(PACKED_ELEMENTS, elements, PACKED_ELEMENTS,
|
||||
new_elements, elements_length,
|
||||
new_elements_length);
|
||||
StoreFixedArrayElement(new_elements, index, value);
|
||||
|
||||
// Update backing store and "length" on {values_array}.
|
||||
StoreObjectField(values_array, JSArray::kElementsOffset, new_elements);
|
||||
StoreObjectFieldNoWriteBarrier(values_array, JSArray::kLengthOffset,
|
||||
SmiTag(new_length));
|
||||
Goto(&done);
|
||||
}
|
||||
|
||||
BIND(&if_nogrow);
|
||||
{
|
||||
// The {index} is within bounds of the {elements} backing store, so
|
||||
// just store the {value} and update the "length" of the {values_array}.
|
||||
StoreObjectFieldNoWriteBarrier(values_array, JSArray::kLengthOffset,
|
||||
SmiTag(new_length));
|
||||
StoreFixedArrayElement(elements, index, value);
|
||||
Goto(&done);
|
||||
}
|
||||
}
|
||||
Goto(&did_set_element);
|
||||
BIND(&runtime_set_element);
|
||||
// New-space filled up or index too large, set element via runtime
|
||||
CallRuntime(Runtime::kCreateDataProperty, context, values_array, index,
|
||||
value);
|
||||
Goto(&did_set_element);
|
||||
BIND(&did_set_element);
|
||||
|
||||
Node* const remaining_elements = LoadContextElement(
|
||||
context, kPromiseAllResolveElementRemainingElementsSlot);
|
||||
Node* const result = DecrementSmiCell(remaining_elements);
|
||||
GotoIf(SmiEqual(result, SmiConstant(0)), &resolve_promise);
|
||||
BIND(&if_inbounds);
|
||||
{
|
||||
// The {index} is in bounds of the {values_array},
|
||||
// just store the {value} and continue.
|
||||
StoreFixedArrayElement(elements, index, value);
|
||||
Goto(&done);
|
||||
}
|
||||
|
||||
BIND(&done);
|
||||
Node* remaining_elements_count =
|
||||
LoadContextElement(context, kPromiseAllResolveElementRemainingSlot);
|
||||
remaining_elements_count = SmiSub(remaining_elements_count, SmiConstant(1));
|
||||
StoreContextElement(context, kPromiseAllResolveElementRemainingSlot,
|
||||
remaining_elements_count);
|
||||
GotoIf(SmiEqual(remaining_elements_count, SmiConstant(0)), &resolve_promise);
|
||||
Return(UndefinedConstant());
|
||||
|
||||
BIND(&resolve_promise);
|
||||
@ -2048,6 +2129,9 @@ TF_BUILTIN(PromiseAllResolveElementClosure, PromiseBuiltinsAssembler) {
|
||||
|
||||
BIND(&already_called);
|
||||
Return(UndefinedConstant());
|
||||
|
||||
BIND(&unreachable);
|
||||
Unreachable();
|
||||
}
|
||||
|
||||
// ES#sec-promise.race
|
||||
|
@ -30,11 +30,8 @@ class PromiseBuiltinsAssembler : public CodeStubAssembler {
|
||||
|
||||
protected:
|
||||
enum PromiseAllResolveElementContextSlots {
|
||||
// Index into the values array, or -1 if the callback was already called
|
||||
kPromiseAllResolveElementIndexSlot = Context::MIN_CONTEXT_SLOTS,
|
||||
|
||||
// Remaining elements count (mutable HeapNumber)
|
||||
kPromiseAllResolveElementRemainingElementsSlot,
|
||||
// Remaining elements count
|
||||
kPromiseAllResolveElementRemainingSlot = Context::MIN_CONTEXT_SLOTS,
|
||||
|
||||
// Promise capability from Promise.all
|
||||
kPromiseAllResolveElementCapabilitySlot,
|
||||
@ -105,6 +102,18 @@ class PromiseBuiltinsAssembler : public CodeStubAssembler {
|
||||
|
||||
Node* PromiseHasHandler(Node* promise);
|
||||
|
||||
// Creates the context used by all Promise.all resolve element closures,
|
||||
// together with the values array. Since all closures for a single Promise.all
|
||||
// call use the same context, we need to store the indices for the individual
|
||||
// closures somewhere else (we put them into the identity hash field of the
|
||||
// closures), and we also need to have a separate marker for when the closure
|
||||
// was called already (we slap the native context onto the closure in that
|
||||
// case to mark it's done).
|
||||
Node* CreatePromiseAllResolveElementContext(Node* promise_capability,
|
||||
Node* native_context);
|
||||
Node* CreatePromiseAllResolveElementFunction(Node* context, Node* index,
|
||||
Node* native_context);
|
||||
|
||||
Node* CreatePromiseResolvingFunctionsContext(Node* promise, Node* debug_event,
|
||||
Node* native_context);
|
||||
|
||||
@ -170,9 +179,6 @@ class PromiseBuiltinsAssembler : public CodeStubAssembler {
|
||||
const IteratorRecord& record, Label* if_exception,
|
||||
Variable* var_exception);
|
||||
|
||||
Node* IncrementSmiCell(Node* cell, Label* if_overflow = nullptr);
|
||||
Node* DecrementSmiCell(Node* cell);
|
||||
|
||||
void SetForwardingHandlerIfTrue(Node* context, Node* condition,
|
||||
const NodeGenerator& object);
|
||||
inline void SetForwardingHandlerIfTrue(Node* context, Node* condition,
|
||||
|
@ -496,7 +496,6 @@ bool Builtins::IsIsolateIndependent(int index) {
|
||||
case kOrdinaryHasInstance:
|
||||
case kOrdinaryToPrimitive_Number:
|
||||
case kOrdinaryToPrimitive_String:
|
||||
case kPromiseAll:
|
||||
case kPromiseCapabilityDefaultReject:
|
||||
case kPromiseCapabilityDefaultResolve:
|
||||
case kPromiseCatchFinally:
|
||||
|
@ -695,6 +695,7 @@ class ErrorUtils : public AllStatic {
|
||||
T(TooManySpreads, \
|
||||
"Literal containing too many nested spreads (up to 65534 allowed)") \
|
||||
T(TooManyVariables, "Too many variables declared (only 4194303 allowed)") \
|
||||
T(TooManyElementsInPromiseAll, "Too many elements passed to Promise.all") \
|
||||
T(TypedArrayTooShort, \
|
||||
"Derived TypedArray constructor created an array which was too small") \
|
||||
T(UnexpectedEOS, "Unexpected end of input") \
|
||||
|
19
test/mjsunit/es6/promise-all-overflow-1.js
Normal file
19
test/mjsunit/es6/promise-all-overflow-1.js
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright 2018 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flags: --allow-natives-syntax
|
||||
|
||||
// Make sure we properly throw a RangeError when overflowing the maximum
|
||||
// number of elements for Promise.all, which is capped at 2^21 bits right
|
||||
// now, since we store the indices as identity hash on the resolve element
|
||||
// closures.
|
||||
const a = new Array(2 ** 21 - 1);
|
||||
const p = Promise.resolve(1);
|
||||
for (let i = 0; i < a.length; ++i) a[i] = p;
|
||||
testAsync(assert => {
|
||||
assert.plan(1);
|
||||
Promise.all(a).then(assert.unreachable, reason => {
|
||||
assert.equals(true, reason instanceof RangeError);
|
||||
});
|
||||
});
|
16
test/mjsunit/es6/promise-all-overflow-2.js
Normal file
16
test/mjsunit/es6/promise-all-overflow-2.js
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2018 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flags: --allow-natives-syntax
|
||||
|
||||
// Test that pre-allocation of the result array works even if it needs to be
|
||||
// allocated in large object space.
|
||||
const a = new Array(64 * 1024);
|
||||
a.fill(Promise.resolve(1));
|
||||
testAsync(assert => {
|
||||
assert.plan(1);
|
||||
Promise.all(a).then(b => {
|
||||
assert.equals(a.length, b.length);
|
||||
});
|
||||
});
|
84
test/mjsunit/es6/promise-all.js
Normal file
84
test/mjsunit/es6/promise-all.js
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright 2018 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flags: --allow-natives-syntax
|
||||
|
||||
// We store the index in the hash code field of the Promise.all resolve
|
||||
// element closures, so make sure we properly handle the cases where this
|
||||
// magical field turns into a PropertyArray later.
|
||||
(function() {
|
||||
class MyPromise extends Promise {
|
||||
then(resolve, reject) {
|
||||
this.resolve = resolve;
|
||||
}
|
||||
};
|
||||
const myPromise = new MyPromise(() => {});
|
||||
MyPromise.all([myPromise]);
|
||||
myPromise.resolve.x = 1;
|
||||
myPromise.resolve(1);
|
||||
})();
|
||||
|
||||
// Same test as above, but for PropertyDictionary.
|
||||
(function() {
|
||||
class MyPromise extends Promise {
|
||||
then(resolve, reject) {
|
||||
this.resolve = resolve;
|
||||
}
|
||||
};
|
||||
const myPromise = new MyPromise(() => {});
|
||||
MyPromise.all([myPromise]);
|
||||
for (let i = 0; i < 1025; ++i) {
|
||||
myPromise.resolve[`x${i}`] = i;
|
||||
}
|
||||
myPromise.resolve(1);
|
||||
})();
|
||||
|
||||
// Test that we return a proper array even if (custom) "then" invokes the
|
||||
// resolve callbacks right away.
|
||||
(function() {
|
||||
class MyPromise extends Promise {
|
||||
constructor(executor, id) {
|
||||
super(executor);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
then(resolve, reject) {
|
||||
if (this.id) return resolve(this.id);
|
||||
return super.then(resolve, reject)
|
||||
}
|
||||
};
|
||||
const a = new MyPromise(() => {}, 'a');
|
||||
const b = new MyPromise(() => {}, 'b');
|
||||
testAsync(assert => {
|
||||
assert.plan(1);
|
||||
MyPromise.all([a, b]).then(
|
||||
v => assert.equals(['a', 'b'], v),
|
||||
assert.unexpectedRejection());
|
||||
});
|
||||
})();
|
||||
|
||||
// Test that we properly handle holes introduced into the resulting array
|
||||
// by resolving some late elements immediately.
|
||||
(function() {
|
||||
class MyPromise extends Promise {
|
||||
then(resolve, reject) {
|
||||
if (this.immediately) {
|
||||
resolve(42);
|
||||
} else {
|
||||
super.then(resolve, reject);
|
||||
}
|
||||
}
|
||||
};
|
||||
const a = new Array(1024);
|
||||
a.fill(MyPromise.resolve(1));
|
||||
const p = MyPromise.resolve(0);
|
||||
p.immediately = true;
|
||||
a.push(p);
|
||||
testAsync(assert => {
|
||||
assert.plan(1);
|
||||
MyPromise.all(a).then(
|
||||
b => assert.equals(42, b[1024]),
|
||||
assert.unexpectedRejection());
|
||||
});
|
||||
})();
|
@ -105,6 +105,7 @@
|
||||
'migrations': [SKIP],
|
||||
'array-functions-prototype-misc': [PASS, SLOW, ['mode == debug', SKIP]],
|
||||
'compiler/regress-808472': [PASS, ['mode == debug', SKIP]],
|
||||
'es6/promise-all-overflow-*': [PASS, SLOW, ['mode == debug', SKIP]],
|
||||
|
||||
##############################################################################
|
||||
# This test sets the umask on a per-process basis and hence cannot be
|
||||
|
Loading…
Reference in New Issue
Block a user