perf: avoid redundant lookup in BluetoothChooser::AddOrUpdateDevice() (#39059)

perf: avoid redundant map lookup in BluetoothChooser::AddOrUpdateDevice()
This commit is contained in:
Charles Kerr 2023-07-13 03:16:25 -05:00 committed by GitHub
parent b14b876d50
commit dc671804da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -110,13 +110,10 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
// an event // an event
return; return;
} }
bool changed = false;
auto entry = device_map_.find(device_id); auto [iter, changed] = device_map_.try_emplace(device_id, device_name);
if (entry == device_map_.end()) { if (!changed && should_update_name) {
device_map_[device_id] = device_name; iter->second = device_name;
changed = true;
} else if (should_update_name) {
entry->second = device_name;
changed = true; changed = true;
} }
@ -139,11 +136,8 @@ std::vector<electron::BluetoothChooser::DeviceInfo>
BluetoothChooser::GetDeviceList() { BluetoothChooser::GetDeviceList() {
std::vector<electron::BluetoothChooser::DeviceInfo> vec; std::vector<electron::BluetoothChooser::DeviceInfo> vec;
vec.reserve(device_map_.size()); vec.reserve(device_map_.size());
for (const auto& it : device_map_) { for (const auto& [device_id, device_name] : device_map_)
DeviceInfo info = {it.first, it.second}; vec.emplace_back(device_id, device_name);
vec.push_back(info);
}
return vec; return vec;
} }