From dc671804daf4d8d1190f6db4c028038685143976 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 13 Jul 2023 03:16:25 -0500 Subject: [PATCH] perf: avoid redundant lookup in BluetoothChooser::AddOrUpdateDevice() (#39059) perf: avoid redundant map lookup in BluetoothChooser::AddOrUpdateDevice() --- shell/browser/lib/bluetooth_chooser.cc | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/shell/browser/lib/bluetooth_chooser.cc b/shell/browser/lib/bluetooth_chooser.cc index 7d84cff45e2c..2bd9310679f6 100644 --- a/shell/browser/lib/bluetooth_chooser.cc +++ b/shell/browser/lib/bluetooth_chooser.cc @@ -110,13 +110,10 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id, // an event return; } - bool changed = false; - auto entry = device_map_.find(device_id); - if (entry == device_map_.end()) { - device_map_[device_id] = device_name; - changed = true; - } else if (should_update_name) { - entry->second = device_name; + + auto [iter, changed] = device_map_.try_emplace(device_id, device_name); + if (!changed && should_update_name) { + iter->second = device_name; changed = true; } @@ -139,11 +136,8 @@ std::vector BluetoothChooser::GetDeviceList() { std::vector vec; vec.reserve(device_map_.size()); - for (const auto& it : device_map_) { - DeviceInfo info = {it.first, it.second}; - vec.push_back(info); - } - + for (const auto& [device_id, device_name] : device_map_) + vec.emplace_back(device_id, device_name); return vec; }