Skip to content

Commit

Permalink
Rearrange Comments::set (open-source-parsers#1278)
Browse files Browse the repository at this point in the history
* slightly optimize Comments::set

Avoid allocation if the set is going to be rejected anyway.

Prototype suggestion from open-source-parsers#1277 review thread
  • Loading branch information
BillyDonahue authored Mar 18, 2021
1 parent 1ee39a6 commit 94cda30
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,13 +1398,11 @@ String Value::Comments::get(CommentPlacement slot) const {
}

void Value::Comments::set(CommentPlacement slot, String comment) {
if (!ptr_) {
if (slot >= CommentPlacement::numberOfCommentPlacement)
return;
if (!ptr_)
ptr_ = std::unique_ptr<Array>(new Array());
}
// check comments array boundry.
if (slot < CommentPlacement::numberOfCommentPlacement) {
(*ptr_)[slot] = std::move(comment);
}
(*ptr_)[slot] = std::move(comment);
}

void Value::setComment(String comment, CommentPlacement placement) {
Expand Down

0 comments on commit 94cda30

Please sign in to comment.