Store StringId in array instead of map
diff --git a/src/trace_processor/importers/proto/chrome_string_lookup.cc b/src/trace_processor/importers/proto/chrome_string_lookup.cc index 9a4e11d..dc39a31 100644 --- a/src/trace_processor/importers/proto/chrome_string_lookup.cc +++ b/src/trace_processor/importers/proto/chrome_string_lookup.cc
@@ -247,38 +247,42 @@ ChromeStringLookup::ChromeStringLookup( TraceStorage* storage, bool ignore_predefined_names_for_testing) { - for (int32_t i = chrome_enums::ProcessType_MIN; - i <= chrome_enums::ProcessType_MAX; ++i) { + for (int32_t type = kProcessTypeMin; type <= kProcessTypeMax; ++type) { const char* name = - GetProcessNameString(i, ignore_predefined_names_for_testing); - chrome_process_name_ids_[i] = + GetProcessNameString(type, ignore_predefined_names_for_testing); + const auto idx = static_cast<size_t>(type - kProcessTypeMin); + chrome_process_name_ids_[idx] = name ? storage->InternString(name) : kNullStringId; } - for (int32_t i = chrome_enums::ThreadType_MIN; - i <= chrome_enums::ThreadType_MAX; ++i) { + for (int32_t type = kThreadTypeMin; type <= kThreadTypeMax; ++type) { const char* name = - GetThreadNameString(i, ignore_predefined_names_for_testing); - chrome_thread_name_ids_[i] = + GetThreadNameString(type, ignore_predefined_names_for_testing); + const auto idx = static_cast<size_t>(type - kThreadTypeMin); + chrome_thread_name_ids_[idx] = name ? storage->InternString(name) : kNullStringId; } } StringId ChromeStringLookup::GetProcessName(int32_t process_type) const { - auto process_name_it = chrome_process_name_ids_.find(process_type); - if (process_name_it != chrome_process_name_ids_.end()) - return process_name_it->second; - + if (process_type >= kProcessTypeMin) { + const auto idx = static_cast<size_t>(process_type - kProcessTypeMin); + if (idx < chrome_process_name_ids_.size()) { + return chrome_process_name_ids_[idx]; + } + } PERFETTO_DLOG("GetProcessName error: Unknown Chrome process type %u", process_type); return kNullStringId; } StringId ChromeStringLookup::GetThreadName(int32_t thread_type) const { - auto thread_name_it = chrome_thread_name_ids_.find(thread_type); - if (thread_name_it != chrome_thread_name_ids_.end()) - return thread_name_it->second; - + if (thread_type >= kThreadTypeMin) { + const auto idx = static_cast<size_t>(thread_type - kThreadTypeMin); + if (idx < chrome_thread_name_ids_.size()) { + return chrome_thread_name_ids_[idx]; + } + } PERFETTO_DLOG("GetThreadName error: Unknown Chrome thread type %u", thread_type); return kNullStringId;
diff --git a/src/trace_processor/importers/proto/chrome_string_lookup.h b/src/trace_processor/importers/proto/chrome_string_lookup.h index e4ed3fb..881ba21 100644 --- a/src/trace_processor/importers/proto/chrome_string_lookup.h +++ b/src/trace_processor/importers/proto/chrome_string_lookup.h
@@ -17,18 +17,26 @@ #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_CHROME_STRING_LOOKUP_H_ #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_CHROME_STRING_LOOKUP_H_ -#include <map> +#include <array> -#include <optional> +#include "protos/third_party/chromium/chrome_enums.pbzero.h" #include "src/trace_processor/storage/trace_storage.h" -#include "perfetto/ext/base/string_view.h" - namespace perfetto { namespace trace_processor { class ChromeStringLookup { public: + // Min and max known values for process and thread types. + constexpr static int32_t kProcessTypeMin = + ::perfetto::protos::chrome_enums::pbzero::ProcessType_MIN; + constexpr static int32_t kProcessTypeMax = + ::perfetto::protos::chrome_enums::pbzero::ProcessType_MAX; + constexpr static int32_t kThreadTypeMin = + ::perfetto::protos::chrome_enums::pbzero::ThreadType_MIN; + constexpr static int32_t kThreadTypeMax = + ::perfetto::protos::chrome_enums::pbzero::ThreadType_MAX; + explicit ChromeStringLookup(TraceStorage* storage, bool ignore_predefined_names_for_testing = false); @@ -36,9 +44,9 @@ StringId GetThreadName(int32_t thread_type) const; private: - std::map<int32_t /* ChromeProcessDescriptor::ProcessType */, StringId> + std::array<StringId, kProcessTypeMax - kProcessTypeMin + 1> chrome_process_name_ids_; - std::map<int32_t /* ChromeThreadDescriptor::ThreadType */, StringId> + std::array<StringId, kThreadTypeMax - kThreadTypeMin + 1> chrome_thread_name_ids_; };