Skip to content

Commit

Permalink
Slightly simplify SizeOfUnion. Performance should be improved as well.
Browse files Browse the repository at this point in the history
Assembly is much shorter with less jumps.
https://gcc.godbolt.org/z/oMKsYacMh

PiperOrigin-RevId: 700437427
  • Loading branch information
goldvitaly authored and copybara-github committed Nov 26, 2024
1 parent 4cfeb66 commit 5bb4555
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/google/protobuf/extension_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -972,27 +972,13 @@ namespace {
// already allocated and will not be going away.
template <typename ItX, typename ItY>
size_t SizeOfUnion(ItX it_dest, ItX end_dest, ItY it_source, ItY end_source) {
size_t result = 0;
while (it_dest != end_dest && it_source != end_source) {
if (it_dest->first < it_source->first) {
++result;
++it_dest;
} else if (it_dest->first == it_source->first) {
++result;
++it_dest;
++it_source;
} else {
if (!it_source->second.is_cleared) {
++result;
}
++it_source;
}
}
result += std::distance(it_dest, end_dest);
size_t result = std::distance(it_dest, end_dest);
for (; it_source != end_source; ++it_source) {
if (!it_source->second.is_cleared) {
++result;
while (it_dest != end_dest && it_dest->first < it_source->first) {
++it_dest;
}
result += (it_dest == end_dest || it_dest->first > it_source->first) &&
!it_source->second.is_cleared;
}
return result;
}
Expand Down

0 comments on commit 5bb4555

Please sign in to comment.