From 5bb4555a413de397dc52757ce00e8434c96d6322 Mon Sep 17 00:00:00 2001 From: Vitaly Goldshteyn Date: Tue, 26 Nov 2024 13:09:48 -0800 Subject: [PATCH] Slightly simplify `SizeOfUnion`. Performance should be improved as well. Assembly is much shorter with less jumps. https://gcc.godbolt.org/z/oMKsYacMh PiperOrigin-RevId: 700437427 --- src/google/protobuf/extension_set.cc | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc index 383731f005cb..e6a61e1a5f2b 100644 --- a/src/google/protobuf/extension_set.cc +++ b/src/google/protobuf/extension_set.cc @@ -972,27 +972,13 @@ namespace { // already allocated and will not be going away. template 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; }