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
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<electron::BluetoothChooser::DeviceInfo>
BluetoothChooser::GetDeviceList() {
std::vector<electron::BluetoothChooser::DeviceInfo> 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;
}