-
Notifications
You must be signed in to change notification settings - Fork 2.7k
optimize JsonWriter::validate #1172
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1198,34 +1198,30 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const { | |
endingLineFeedSymbol, usf, emitUTF8, pre, | ||
precisionType); | ||
} | ||
static void getValidWriterKeys(std::set<String>* valid_keys) { | ||
valid_keys->clear(); | ||
valid_keys->insert("indentation"); | ||
valid_keys->insert("commentStyle"); | ||
valid_keys->insert("enableYAMLCompatibility"); | ||
valid_keys->insert("dropNullPlaceholders"); | ||
valid_keys->insert("useSpecialFloats"); | ||
valid_keys->insert("emitUTF8"); | ||
valid_keys->insert("precision"); | ||
valid_keys->insert("precisionType"); | ||
} | ||
|
||
bool StreamWriterBuilder::validate(Json::Value* invalid) const { | ||
Json::Value my_invalid; | ||
if (!invalid) | ||
invalid = &my_invalid; // so we do not need to test for NULL | ||
Json::Value& inv = *invalid; | ||
std::set<String> valid_keys; | ||
getValidWriterKeys(&valid_keys); | ||
Value::Members keys = settings_.getMemberNames(); | ||
size_t n = keys.size(); | ||
for (size_t i = 0; i < n; ++i) { | ||
String const& key = keys[i]; | ||
if (valid_keys.find(key) == valid_keys.end()) { | ||
inv[key] = settings_[key]; | ||
} | ||
static const auto& valid_keys = *new std::set<String>{ | ||
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. There's no sense making a fresh valid_keys set each time validate is called. |
||
"indentation", | ||
"commentStyle", | ||
"enableYAMLCompatibility", | ||
"dropNullPlaceholders", | ||
"useSpecialFloats", | ||
"emitUTF8", | ||
"precision", | ||
"precisionType", | ||
}; | ||
for (auto si = settings_.begin(); si != settings_.end(); ++si) { | ||
auto key = si.name(); | ||
if (valid_keys.count(key)) | ||
continue; | ||
if (invalid) | ||
(*invalid)[std::move(key)] = *si; | ||
else | ||
return false; | ||
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. If |
||
} | ||
return inv.empty(); | ||
return invalid ? invalid->empty() : true; | ||
} | ||
|
||
Value& StreamWriterBuilder::operator[](const String& key) { | ||
return settings_[key]; | ||
} | ||
|
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.
This object is always a waste.
In the
invalid==nullptr
case, we populate this Value just to throw it away.In the
invalid!=nullptr
case, we create it and never use it.