Skip to content

[VM] Speed up TypedArray creation from arrays - #2219

Open
goutamadwant wants to merge 2 commits into
facebook:static_hfrom
goutamadwant:fix/typed-array-from-fast-path-2215
Open

goutamadwant wants to merge 2 commits into
facebook:static_hfrom
goutamadwant:fix/typed-array-from-fast-path-2215

Conversation

@goutamadwant

Copy link
Copy Markdown

Summary

Add a guarded fast path for creating typed arrays from plain JSArray values when both the array iterator and its next method are unchanged.

The fast path avoids allocating iterator and iterator-result objects while preserving dynamic array length, indexed getters, value snapshot semantics, and customized iterator behavior. Inputs that do not meet the guards continue through the generic iterable path.

Add regression coverage for array growth and shrinkage, conversion and mapping side effects, and customized iterator methods.

Fixes #2215

Test Plan

Avoid iterator and iterator-result allocations when a JSArray uses the built-in array iterator methods. Preserve dynamic length, indexed access, and snapshot semantics while falling back for customized iterators.

Add regression coverage for array growth and shrinkage, conversion and mapping side effects, and customized iterator methods.

Fixes facebook#2215
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Oct 3, 2026

@lavenzg lavenzg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good.

// 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this in a new method, iterableToArrayLikeFastPath?

Comment thread lib/VM/JSLib/TypedArray.cpp Outdated
while (k < JSArray::getLength(*lv.sourceArray, runtime)) {
GCScopeMarkerRAII marker{gcScope};

lv.nextValue = lv.sourceArray->at(runtime, k).unboxToHV(runtime);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally we want to to have another fast path for arrays that has no accessors and indexed properties in bound (see arrayFastPathCheck()).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypedArray.from and new TypedArray(array) are ~6× slower since #1882 for plain arrays

2 participants