Repository navigation
[VM] Speed up TypedArray creation from arrays #2219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
goutamadwant
wants to merge
3
commits into
meta:static_h
Choose a base branch
from
goutamadwant:fix/typed-array-from-fast-path-2215
base: static_h
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| #include "hermes/Support/FastArraySearch.h" | ||
| #include "hermes/VM/ArrayStorage.h" | ||
| #include "hermes/VM/JSArrayBuffer.h" | ||
| #include "hermes/VM/JSLib.h" | ||
| #include "hermes/VM/JSLib/Base64Util.h" | ||
| #include "hermes/VM/JSLib/Sorting.h" | ||
| #include "hermes/VM/JSTypedArray.h" | ||
|
|
@@ -30,6 +31,80 @@ namespace { | |
| /// @name Abstract Operations and Helper functions | ||
| /// @{ | ||
|
|
||
| /// Copies an array whose iterator and iterator-next methods are unmodified. | ||
| CallResult<HermesValue> iterableToArrayLikeFastPath( | ||
| Runtime &runtime, | ||
| Handle<JSArray> sourceArray) { | ||
| struct : public Locals { | ||
| PinnedValue<> nextValue; | ||
| PinnedValue<JSArray> A; | ||
| } lv; | ||
| LocalsRAII lraii{runtime, &lv}; | ||
| GCScope gcScope{runtime}; | ||
|
|
||
| uint32_t length = JSArray::getLength(*sourceArray, runtime); | ||
| if (arrayFastPathCheck(runtime, *sourceArray, nullptr, length)) { | ||
| auto arrRes = JSArray::create(runtime, length, 0); | ||
| if (LLVM_UNLIKELY(arrRes == ExecutionStatus::EXCEPTION)) { | ||
| return ExecutionStatus::EXCEPTION; | ||
| } | ||
| lv.A = std::move(*arrRes); | ||
| if (length == 0) { | ||
| return lv.A.getHermesValue(); | ||
| } | ||
|
|
||
| auto newLength = SmallHermesValue::encodeNumberValue(length, runtime); | ||
| NoAllocScope noAllocScope{runtime}; | ||
| auto *srcStorage = sourceArray->getIndexedStorageNullable(runtime); | ||
| auto *destStorage = lv.A->getIndexedStorageNullable(runtime); | ||
| assert(srcStorage && destStorage && "storage should not be null"); | ||
| destStorage->appendWithinCapacity(runtime, srcStorage); | ||
| lv.A->setElemCountUnsafe(length); | ||
| JSArray::putLengthUnsafe(lv.A.get(), runtime, newLength); | ||
| for (uint32_t k = 0; k < length; ++k) { | ||
| if (LLVM_UNLIKELY(destStorage->at(k).isEmpty())) { | ||
| destStorage->set( | ||
| k, SmallHermesValue::encodeUndefinedValue(), runtime.getHeap()); | ||
| } | ||
| } | ||
| return lv.A.getHermesValue(); | ||
| } | ||
|
|
||
| auto arrRes = JSArray::create(runtime, 0, 0); | ||
| if (LLVM_UNLIKELY(arrRes == ExecutionStatus::EXCEPTION)) { | ||
| return ExecutionStatus::EXCEPTION; | ||
| } | ||
| lv.A = std::move(*arrRes); | ||
|
|
||
| uint32_t k = 0; | ||
| while (k < JSArray::getLength(*sourceArray, runtime)) { | ||
| GCScopeMarkerRAII marker{gcScope}; | ||
|
|
||
| lv.nextValue = sourceArray->at(runtime, k).unboxToHV(runtime); | ||
| if (LLVM_UNLIKELY(lv.nextValue->isEmpty())) { | ||
| auto valueRes = getIndexed_RJS(runtime, sourceArray, k); | ||
| if (LLVM_UNLIKELY(valueRes == ExecutionStatus::EXCEPTION)) { | ||
| return ExecutionStatus::EXCEPTION; | ||
| } | ||
| lv.nextValue = std::move(*valueRes); | ||
| } | ||
|
|
||
| if (LLVM_UNLIKELY( | ||
| JSArray::setElementAt(lv.A, runtime, k, lv.nextValue) == | ||
| ExecutionStatus::EXCEPTION)) { | ||
| return ExecutionStatus::EXCEPTION; | ||
| } | ||
| ++k; | ||
| } | ||
|
|
||
| if (LLVM_UNLIKELY( | ||
| JSArray::setLengthProperty(lv.A, runtime, k) == | ||
| ExecutionStatus::EXCEPTION)) { | ||
| return ExecutionStatus::EXCEPTION; | ||
| } | ||
| return lv.A.getHermesValue(); | ||
| } | ||
|
|
||
| /// ES7 22.2.2.1.1 IterableToArrayLike | ||
| /// If items has an @@iterator method, iterate through it and collect | ||
| /// values into an array. Otherwise, return ToObject(items). | ||
|
|
@@ -58,6 +133,34 @@ CallResult<HermesValue> iterableToArrayLike(Runtime &runtime, Handle<> items) { | |
| return toObject(runtime, items); | ||
| } | ||
|
|
||
| // Avoid allocating iterator and iterator-result objects for ordinary arrays. | ||
| // Both methods must be unmodified because the generic iterator protocol | ||
| // observes each of them. | ||
| auto sourceArray = Handle<JSArray>::dyn_vmcast(items); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put this in a new method, iterableToArrayLikeFastPath? |
||
| if (sourceArray && lv.usingIterator->isObject() && | ||
| lv.usingIterator->getObject() == *runtime.arrayPrototypeValues) { | ||
| auto iteratorPrototype = | ||
| Handle<JSObject>::vmcast(&runtime.arrayIteratorPrototype); | ||
| NamedPropertyDescriptor desc; | ||
| JSObject *propObj = JSObject::getNamedDescriptorPredefined( | ||
| iteratorPrototype, runtime, Predefined::next, desc); | ||
| if (LLVM_LIKELY(propObj) && LLVM_LIKELY(!desc.flags.accessor) && | ||
| LLVM_LIKELY(!desc.flags.proxyObject) && | ||
| LLVM_LIKELY(!desc.flags.hostObject)) { | ||
| SmallHermesValue nextMethod = | ||
| JSObject::getNamedSlotValueUnsafe(propObj, runtime, desc); | ||
| auto *nativeNext = nextMethod.isObject() | ||
| ? dyn_vmcast<NativeFunction>(nextMethod.getObject(runtime)) | ||
| : nullptr; | ||
| if (LLVM_LIKELY(nativeNext) && | ||
| LLVM_LIKELY( | ||
| nativeNext->getFunctionPtr() == arrayIteratorPrototypeNext) && | ||
| LLVM_LIKELY(nativeNext->getContext() == nullptr)) { | ||
| return iterableToArrayLikeFastPath(runtime, sourceArray); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 3. Let iteratorRecord be ? GetIterator(items, sync, usingIterator). | ||
| auto iterRes = getCheckedIterator( | ||
| runtime, items, Handle<Callable>::vmcast(&lv.usingIterator)); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test covering this path.