From 4f4e23a3b3e769a3f5119a3550547bc96e050fa5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Mar 2025 09:46:03 -0500 Subject: [PATCH] perf: avoid redundant map lookups in `GlobalShortcut` (#46229) * perf: avoid redundant map lookup in GlobalShortcut::OnKeyPressed() * perf: avoid redundant map lookup in GlobalShortcut::ExecuteCommand() --- shell/browser/api/electron_api_global_shortcut.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/shell/browser/api/electron_api_global_shortcut.cc b/shell/browser/api/electron_api_global_shortcut.cc index d78314e50ec7..d08c6f21fb88 100644 --- a/shell/browser/api/electron_api_global_shortcut.cc +++ b/shell/browser/api/electron_api_global_shortcut.cc @@ -7,6 +7,7 @@ #include #include +#include "base/containers/map_util.h" #include "base/strings/utf_string_conversions.h" #include "base/uuid.h" #include "components/prefs/pref_service.h" @@ -57,22 +58,24 @@ GlobalShortcut::~GlobalShortcut() { } void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) { - if (!accelerator_callback_map_.contains(accelerator)) { + if (auto* cb = base::FindOrNull(accelerator_callback_map_, accelerator)) { + cb->Run(); + } else { // This should never occur, because if it does, // ui::GlobalAcceleratorListener notifies us with wrong accelerator. NOTREACHED(); } - accelerator_callback_map_[accelerator].Run(); } void GlobalShortcut::ExecuteCommand(const extensions::ExtensionId& extension_id, const std::string& command_id) { - if (!command_callback_map_.contains(command_id)) { + if (auto* cb = base::FindOrNull(command_callback_map_, command_id)) { + cb->Run(); + } else { // This should never occur, because if it does, GlobalAcceleratorListener // notifies us with wrong command. NOTREACHED(); } - command_callback_map_[command_id].Run(); } bool GlobalShortcut::RegisterAll(