perf: avoid redundant map lookups in GlobalShortcut (#46262)

* perf: avoid redundant map lookup in GlobalShortcut::OnKeyPressed()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* perf: avoid redundant map lookup in GlobalShortcut::ExecuteCommand()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-03-25 13:08:54 -05:00 committed by GitHub
parent 7d27b2c542
commit d38c2d507e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@
#include <string>
#include <vector>
#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(