Skip to content
forked from v8/v8

Commit

Permalink
[json] Postpone stack operation
Browse files Browse the repository at this point in the history
This CL can improve micro case in Bug: v8:13955 by ~5.5%.

Bug: v8:13955
Change-Id: I01dad874d85453b941c477e2c7e0b84d5b26da9a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4728921
Reviewed-by: Jakob Kummerow <[email protected]>
Commit-Queue: Tao Pan <[email protected]>
Cr-Commit-Position: refs/heads/main@{#89690}
  • Loading branch information
PanTao2 authored and V8 LUCI CQ committed Aug 30, 2023
1 parent edb8673 commit 79330c6
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/json/json-stringifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JsonStringifier {
Handle<Object> gap);

private:
enum Result { UNCHANGED, SUCCESS, EXCEPTION };
enum Result { UNCHANGED, SUCCESS, EXCEPTION, NEED_STACK };

bool InitializeReplacer(Handle<Object> replacer);
bool InitializeGap(Handle<Object> gap);
Expand Down Expand Up @@ -345,7 +345,9 @@ class JsonStringifier {
int indent_;
int part_length_;
int current_index_;
int stack_nesting_level_;
bool overflowed_;
bool need_stack_;

using KeyObject = std::pair<Handle<Object>, Handle<Object>>;
std::vector<KeyObject> stack_;
Expand Down Expand Up @@ -439,7 +441,9 @@ JsonStringifier::JsonStringifier(Isolate* isolate)
indent_(0),
part_length_(kInitialPartLength),
current_index_(0),
stack_nesting_level_(0),
overflowed_(false),
need_stack_(false),
stack_(),
key_cache_(isolate) {
one_byte_ptr_ = one_byte_array_;
Expand All @@ -458,6 +462,11 @@ MaybeHandle<Object> JsonStringifier::Stringify(Handle<Object> object,
return MaybeHandle<Object>();
}
Result result = SerializeObject(object);
if (result == NEED_STACK) {
indent_ = 0;
current_index_ = 0;
result = SerializeObject(object);
}
if (result == UNCHANGED) return factory()->undefined_value();
if (result == SUCCESS) {
if (overflowed_ || current_index_ > String::kMaxLength) {
Expand Down Expand Up @@ -619,6 +628,14 @@ Handle<JSReceiver> JsonStringifier::CurrentHolder(

JsonStringifier::Result JsonStringifier::StackPush(Handle<Object> object,
Handle<Object> key) {
if (!need_stack_) {
++stack_nesting_level_;
if V8_UNLIKELY (stack_nesting_level_ > 10) {
need_stack_ = true;
return NEED_STACK;
}
return SUCCESS;
}
StackLimitCheck check(isolate_);
if (check.HasOverflowed()) {
isolate_->StackOverflow();
Expand All @@ -645,7 +662,13 @@ JsonStringifier::Result JsonStringifier::StackPush(Handle<Object> object,
return SUCCESS;
}

void JsonStringifier::StackPop() { stack_.pop_back(); }
void JsonStringifier::StackPop() {
if V8_LIKELY (!need_stack_) {
--stack_nesting_level_;
return;
}
stack_.pop_back();
}

class CircularStructureMessageBuilder {
public:
Expand Down Expand Up @@ -761,8 +784,9 @@ Handle<String> JsonStringifier::ConstructCircularStructureErrorMessage(
bool MayHaveInterestingProperties(Isolate* isolate, JSReceiver object) {
for (PrototypeIterator iter(isolate, object, kStartAtReceiver);
!iter.IsAtEnd(); iter.Advance()) {
if (iter.GetCurrent()->map()->may_have_interesting_properties())
if (iter.GetCurrent()->map()->may_have_interesting_properties()) {
return true;
}
}
return false;
}
Expand All @@ -785,11 +809,17 @@ JsonStringifier::Result JsonStringifier::Serialize_(Handle<Object> object,
if ((InstanceTypeChecker::IsJSReceiver(instance_type) &&
MayHaveInterestingProperties(isolate_, JSReceiver::cast(*object))) ||
InstanceTypeChecker::IsBigInt(instance_type)) {
if (!need_stack_ && stack_nesting_level_ > 0) {
need_stack_ = true;
return NEED_STACK;
}
need_stack_ = true;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate_, object, ApplyToJsonFunction(object, key), EXCEPTION);
}
}
if (!replacer_function_.is_null()) {
need_stack_ = true;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate_, object, ApplyReplacerFunction(object, key, initial_value),
EXCEPTION);
Expand Down Expand Up @@ -1141,7 +1171,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSObject(
}
Result result = SerializeProperty(property, comma, key_name);
if (!comma && result == SUCCESS) comma = true;
if (result == EXCEPTION) return result;
if (result == EXCEPTION || result == NEED_STACK) return result;
}
Unindent();
if (comma) NewLine();
Expand Down Expand Up @@ -1172,7 +1202,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSReceiverSlow(
EXCEPTION);
Result result = SerializeProperty(property, comma, key);
if (!comma && result == SUCCESS) comma = true;
if (result == EXCEPTION) return result;
if (result == EXCEPTION || result == NEED_STACK) return result;
}
Unindent();
if (comma) NewLine();
Expand Down

0 comments on commit 79330c6

Please sign in to comment.