Skip to content

Commit 67cf7ea

Browse files
Reduce cost of symbol lookup.
Avoid constructing a temporary std::string object while doing the lookup. PiperOrigin-RevId: 746118518
1 parent 1a58e5d commit 67cf7ea

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/google/protobuf/descriptor_database.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "absl/strings/str_cat.h"
3131
#include "absl/strings/str_replace.h"
3232
#include "absl/strings/string_view.h"
33+
#include "absl/strings/strip.h"
3334
#include "google/protobuf/descriptor.pb.h"
3435
#include "google/protobuf/io/coded_stream.h"
3536
#include "google/protobuf/parse_context.h"
@@ -492,6 +493,18 @@ class EncodedDescriptorDatabase::DescriptorIndex {
492493
auto p = package(index);
493494
return absl::StrCat(p, p.empty() ? "" : ".", symbol(index));
494495
}
496+
497+
bool IsSubSymbolOf(const DescriptorIndex& index,
498+
absl::string_view super_symbol) const {
499+
const auto consume_part = [&](absl::string_view part) {
500+
if (!absl::ConsumePrefix(&super_symbol, part)) return false;
501+
return super_symbol.empty() || absl::ConsumePrefix(&super_symbol, ".");
502+
};
503+
if (auto p = package(index); !p.empty()) {
504+
if (!consume_part(p)) return false;
505+
}
506+
return consume_part(symbol(index));
507+
}
495508
};
496509

497510
struct SymbolCompare {
@@ -793,8 +806,7 @@ EncodedDescriptorDatabase::DescriptorIndex::FindSymbolOnlyFlat(
793806
auto iter =
794807
FindLastLessOrEqual(&by_symbol_flat_, name, by_symbol_.key_comp());
795808

796-
return iter != by_symbol_flat_.end() &&
797-
IsSubSymbol(iter->AsString(*this), name)
809+
return iter != by_symbol_flat_.end() && iter->IsSubSymbolOf(*this, name)
798810
? all_values_[iter->data_offset].value()
799811
: Value();
800812
}

0 commit comments

Comments
 (0)