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

avoid allocating memory if all extension are cleared #9345

Merged
merged 5 commits into from
Mar 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update extension_set.cc
avoid allocating memory if all extension are cleared
  • Loading branch information
liuzhijiang authored Dec 26, 2021
commit 961242aef103b6e3b42b37b777f58a3e2d071179
20 changes: 20 additions & 0 deletions src/google/protobuf/extension_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,26 @@ size_t SizeOfUnion(ItX it_xs, ItX end_xs, ItY it_ys, ItY end_ys) {

void ExtensionSet::MergeFrom(const MessageLite* extendee,
const ExtensionSet& other) {
// check if the other is cleared
bool is_other_all_cleared = true;
if (PROTOBUF_PREDICT_TRUE(!other.is_large())){
for (const KeyValue* it = other.flat_begin(); it != other.flat_end(); it++){
is_other_all_cleared = is_other_all_cleared && it->second.is_cleared;
if (!is_other_all_cleared){
break;
}
}
} else {
for (LargeMap::iterator it = other.map_.large->begin(); it != other.map_.large->end(); it++){
is_other_all_cleared = is_other_all_cleared && it->second.is_cleared;
if (!is_other_all_cleared){
break;
}
}
}
if (is_other_all_cleared){
return;
}
if (PROTOBUF_PREDICT_TRUE(!is_large())) {
if (PROTOBUF_PREDICT_TRUE(!other.is_large())) {
GrowCapacity(SizeOfUnion(flat_begin(), flat_end(), other.flat_begin(),
Expand Down