From 8412d783104e7dacc9448c4ada34899bce739272 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 26 Mar 2025 08:12:16 -0500 Subject: [PATCH] perf: avoid redundant map lookup in `AddComponentResourceEntries()` (#46255) * perf: avoid double map lookup in ElectronComponentExtensionResourceManager::AddComponentResourceEntries() * perf: move the path key when calling try_emplace() --- .../electron_component_extension_resource_manager.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/shell/browser/extensions/electron_component_extension_resource_manager.cc b/shell/browser/extensions/electron_component_extension_resource_manager.cc index d3ac6fd3aceb..0a1b2df5d61d 100644 --- a/shell/browser/extensions/electron_component_extension_resource_manager.cc +++ b/shell/browser/extensions/electron_component_extension_resource_manager.cc @@ -84,12 +84,14 @@ void ElectronComponentExtensionResourceManager::AddComponentResourceEntries( gen_folder_path = gen_folder_path.NormalizePathSeparators(); for (const auto& entry : entries) { + const int id = entry.id; base::FilePath resource_path = base::FilePath().AppendASCII(entry.path); resource_path = resource_path.NormalizePathSeparators(); if (!gen_folder_path.IsParent(resource_path)) { - DCHECK(!path_to_resource_id_.contains(resource_path)); - path_to_resource_id_[resource_path] = entry.id; + const auto [_, inserted] = + path_to_resource_id_.try_emplace(std::move(resource_path), id); + DCHECK(inserted); } else { // If the resource is a generated file, strip the generated folder's path, // so that it can be served from a normal URL (as if it were not @@ -97,8 +99,9 @@ void ElectronComponentExtensionResourceManager::AddComponentResourceEntries( base::FilePath effective_path = base::FilePath().AppendASCII(resource_path.AsUTF8Unsafe().substr( gen_folder_path.value().length())); - DCHECK(!path_to_resource_id_.contains(effective_path)); - path_to_resource_id_[effective_path] = entry.id; + const auto [_, inserted] = + path_to_resource_id_.try_emplace(std::move(effective_path), id); + DCHECK(inserted); } } }