Skip to content
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

Improve JSON.stringify perf for TypedArrays #4831

Merged
merged 2 commits into from
Mar 19, 2018

Conversation

MikeHolman
Copy link
Contributor

During enumeration, we can (and should) avoid creating PropertyRecords for TypedArrays. Instead we can use the numeric index.

In the below micro-benchmark, this improves our perf by about 45%.

const view = new Int8Array(1 << 24);
for (let i = 0; i < view.length; ++i) {
  view[i] = i|0;
}
var start = Date.now();
const str = JSON.stringify(view);
console.log(Date.now() - start);

OS: 16385822

@@ -520,18 +552,25 @@ JSONStringifier::ReadObject(_In_ RecyclableObject* obj, _In_ JSONObjectStack* ob
JavascriptStaticEnumerator enumerator;
if (obj->GetEnumerator(&enumerator, EnumeratorFlags::SnapShotSemantics | EnumeratorFlags::EphemeralReference, this->scriptContext))
{
enumerator.GetInitialPropertyCount();
Copy link
Contributor

@curtisman curtisman Mar 16, 2018

Choose a reason for hiding this comment

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

You might want to clean up the implementation in JAvascriptStaticEnumerator.h and DynamicObjectPropertyEnumerator.h as well, as I don't see other references to those. #Resolved

Copy link
Contributor

@curtisman curtisman left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Contributor

@sethbrenith sethbrenith left a comment

Choose a reason for hiding this comment

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

:shipit:

JSONObjectProperty prop;
prop.propertyName = propertyName;

Var value = JavascriptOperators::GetItem(obj, numericIndex, this->scriptContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

:: [](start = 35, length = 2)

Fantastic wins.

So if I understand - obj is typed array, so this will call to the DirectGetItem of typed array which will have unnecessary checks being detached and others . Could that be avoided (or it is already avoided)?

@akroshg
Copy link
Contributor

akroshg commented Mar 16, 2018

    if (JavascriptOperators::IsArray(valueObj))

Curious why don't we have similar case like this for typed array and read the index just like an array? #ByDesign


Refers to: lib/Runtime/Library/JSONStringifier.cpp:811 in 45caec1. [](commit_id = 45caec1, deletion_comment = False)

@akroshg
Copy link
Contributor

akroshg commented Mar 16, 2018

    if (JavascriptOperators::IsArray(valueObj))

It is perfectly fine if you don't want to rush different changes on this PR.


In reply to: 373783535 [](ancestors = 373783535)


Refers to: lib/Runtime/Library/JSONStringifier.cpp:811 in 45caec1. [](commit_id = 45caec1, deletion_comment = False)

@MikeHolman
Copy link
Contributor Author

MikeHolman commented Mar 16, 2018

    if (JavascriptOperators::IsArray(valueObj))

It's because spec has very different way they serialize normal arrays vs. other objects. Normal arrays only serialize elements up to length, but typed arrays will need to serialize all properties (e.g. if i say typedArr.prop = "blah" we need to serialize that). So I'd rather just use the normal enumerator rather than trying to be fancy


In reply to: 373793434 [](ancestors = 373793434,373783535)


Refers to: lib/Runtime/Library/JSONStringifier.cpp:811 in 45caec1. [](commit_id = 45caec1, deletion_comment = False)

@chakrabot chakrabot merged commit 1e918e2 into chakra-core:master Mar 19, 2018
chakrabot pushed a commit that referenced this pull request Mar 19, 2018
Merge pull request #4831 from MikeHolman:arrenum

During enumeration, we can (and should) avoid creating PropertyRecords for TypedArrays. Instead we can use the numeric index.

In the below micro-benchmark, this improves our perf by about 45%.

````
const view = new Int8Array(1 << 24);
for (let i = 0; i < view.length; ++i) {
  view[i] = i|0;
}
var start = Date.now();
const str = JSON.stringify(view);
console.log(Date.now() - start);
````

OS: 16385822
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants