-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 1 commit
Commits
Show all changes
2 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 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 |
---|---|---|
|
@@ -198,7 +198,6 @@ JSONStringifier::Stringify(_In_ ScriptContext* scriptContext, _In_ Var value, _I | |
wrapper, | ||
prop, | ||
value, | ||
scriptContext->GetThreadContext()->GetEmptyStringPropertyRecord(), | ||
&objStack); | ||
|
||
if (prop->type == JSONContentType::Undefined) | ||
|
@@ -340,7 +339,7 @@ JSONStringifier::ReadArrayElement(uint32 index, _In_ RecyclableObject* arr, _Out | |
JavascriptOperators::GetItem(arr, index, &value, this->scriptContext); | ||
} | ||
JavascriptString* indexString = this->scriptContext->GetIntegerString(index); | ||
this->ReadProperty(indexString, arr, prop, value, nullptr, objectStack); | ||
this->ReadProperty(indexString, arr, prop, value, objectStack); | ||
} | ||
|
||
JSONArray* | ||
|
@@ -386,19 +385,13 @@ JSONStringifier::ReadArray(_In_ RecyclableObject* arr, _In_ JSONObjectStack* obj | |
} | ||
|
||
void | ||
JSONStringifier::ReadObjectElement( | ||
JSONStringifier::AppendObjectElement( | ||
_In_ JavascriptString* propertyName, | ||
_In_opt_ PropertyRecord const* propertyRecord, | ||
_In_ RecyclableObject* obj, | ||
_In_ JSONObject* jsonObject, | ||
_In_ JSONObjectStack* objectStack) | ||
_In_ JSONObjectProperty* prop) | ||
{ | ||
JSONObjectProperty prop; | ||
prop.propertyName = propertyName; | ||
this->ReadProperty(propertyName, obj, &prop.propertyValue, nullptr, propertyRecord, objectStack); | ||
|
||
// Undefined result is not concatenated | ||
if (prop.propertyValue.type != JSONContentType::Undefined) | ||
if (prop->propertyValue.type != JSONContentType::Undefined) | ||
{ | ||
// Increase length for the name of the property | ||
this->totalStringLength = UInt32Math::Add(this->totalStringLength, CalculateStringElementLength(propertyName)); | ||
|
@@ -409,10 +402,49 @@ JSONStringifier::ReadObjectElement( | |
// If gap is specified, a space is appended | ||
UInt32Math::Inc(this->totalStringLength); | ||
} | ||
jsonObject->Push(prop); | ||
} | ||
} | ||
|
||
void | ||
JSONStringifier::ReadObjectElement( | ||
_In_ JavascriptString* propertyName, | ||
_In_ uint32 numericIndex, | ||
_In_ RecyclableObject* obj, | ||
_In_ JSONObject* jsonObject, | ||
_In_ JSONObjectStack* objectStack) | ||
{ | ||
JSONObjectProperty prop; | ||
prop.propertyName = propertyName; | ||
|
||
Var value = JavascriptOperators::GetItem(obj, numericIndex, this->scriptContext); | ||
|
||
this->ReadProperty(propertyName, obj, &prop.propertyValue, value, objectStack); | ||
|
||
this->AppendObjectElement(propertyName, jsonObject, &prop); | ||
|
||
jsonObject->Push(prop); | ||
} | ||
|
||
void | ||
JSONStringifier::ReadObjectElement( | ||
_In_ JavascriptString* propertyName, | ||
_In_opt_ PropertyRecord const* propertyRecord, | ||
_In_ RecyclableObject* obj, | ||
_In_ JSONObject* jsonObject, | ||
_In_ JSONObjectStack* objectStack) | ||
{ | ||
JSONObjectProperty prop; | ||
prop.propertyName = propertyName; | ||
|
||
Var value = this->ReadValue(propertyName, propertyRecord, obj); | ||
|
||
this->ReadProperty(propertyName, obj, &prop.propertyValue, value, objectStack); | ||
|
||
this->AppendObjectElement(propertyName, jsonObject, &prop); | ||
|
||
jsonObject->Push(prop); | ||
} | ||
|
||
// Calculates how many additional characters are needed for printing the Object/Array structure | ||
// This includes commas, brackets, and gap (if any) | ||
void | ||
|
@@ -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(); | ||
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. 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 |
||
JavascriptString* propertyName = nullptr; | ||
PropertyId nextKey = Constants::NoProperty; | ||
while ((propertyName = enumerator.MoveAndGetNext(nextKey)) != nullptr) | ||
{ | ||
PropertyRecord const * propertyRecord = nullptr; | ||
if (nextKey == Constants::NoProperty) | ||
const uint32 numericIndex = enumerator.GetCurrentItemIndex(); | ||
if (numericIndex != Constants::InvalidSourceIndex) | ||
{ | ||
this->scriptContext->GetOrAddPropertyRecord(propertyName, &propertyRecord); | ||
nextKey = propertyRecord->GetPropertyId(); | ||
this->ReadObjectElement(propertyName, numericIndex, obj, jsonObject, &stack); | ||
} | ||
else | ||
{ | ||
PropertyRecord const * propertyRecord = nullptr; | ||
if (nextKey == Constants::NoProperty) | ||
{ | ||
this->scriptContext->GetOrAddPropertyRecord(propertyName, &propertyRecord); | ||
nextKey = propertyRecord->GetPropertyId(); | ||
} | ||
this->ReadObjectElement(propertyName, propertyRecord, obj, jsonObject, &stack); | ||
} | ||
this->ReadObjectElement(propertyName, propertyRecord, obj, jsonObject, &stack); | ||
} | ||
} | ||
} | ||
|
@@ -728,17 +767,10 @@ JSONStringifier::ReadProperty( | |
_In_ JavascriptString* key, | ||
_In_opt_ RecyclableObject* holder, | ||
_Out_ JSONProperty* prop, | ||
_In_opt_ Var value, | ||
_In_opt_ const PropertyRecord* propertyRecord, | ||
_In_ Var value, | ||
_In_ JSONObjectStack* objectStack) | ||
{ | ||
PROBE_STACK(this->scriptContext, Constants::MinStackDefault); | ||
if (value == nullptr) | ||
{ | ||
// If we don't have a value, we must have an object from which we can read it | ||
AnalysisAssert(holder != nullptr); | ||
value = this->ReadValue(key, propertyRecord, holder); | ||
} | ||
|
||
// Save these to avoid having to recheck conditions unless value is modified by ToJSON or a replacer function | ||
RecyclableObject* valueObj = JavascriptOperators::TryFromVar<RecyclableObject>(value); | ||
|
This file contains 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
This file contains 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
This file contains 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.
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)?