Skip to content

Commit 16275d5

Browse files
committed
DeviceContainer: Use std::vector<Device*> instead std::map
Linear search is faster for smaller container sizes (below 10K).
1 parent aed576e commit 16275d5

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

device.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -943,11 +943,11 @@ std::vector<Resource *> Device::subDevices() const
943943

944944
Device *DEV_GetDevice(DeviceContainer &devices, DeviceKey key)
945945
{
946-
auto d = devices.find(key);
946+
auto d = std::find_if(devices.begin(), devices.end(), [key](Device *device){ return device->key() == key; });
947947

948948
if (d != devices.end())
949949
{
950-
return d->second;
950+
return *d;
951951
}
952952

953953
return nullptr;
@@ -956,18 +956,17 @@ Device *DEV_GetDevice(DeviceContainer &devices, DeviceKey key)
956956
Device *DEV_GetOrCreateDevice(QObject *parent, DeviceContainer &devices, DeviceKey key)
957957
{
958958
Q_ASSERT(key != 0);
959-
auto d = devices.find(key);
959+
auto d = std::find_if(devices.begin(), devices.end(), [key](Device *device){ return device->key() == key; });
960960

961961
if (d == devices.end())
962962
{
963-
auto res = devices.insert({key, new Device(key, parent)});
964-
d = res.first;
965-
Q_ASSERT(d->second);
963+
devices.push_back(new Device(key, parent));
964+
return devices.back();
966965
}
967966

968967
Q_ASSERT(d != devices.end());
969968

970-
return d->second;
969+
return *d;
971970
}
972971

973972
/*! Is used to test full Device control over: Device and sub-device creation, read, write, parse of Zigbee commands.

device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Device : public QObject,
144144
bool m_managed = false; //! a managed device doesn't rely on legacy implementation of polling etc.
145145
};
146146

147-
using DeviceContainer = std::unordered_map<DeviceKey, Device*>;
147+
using DeviceContainer = std::vector<Device*>;
148148

149149
/*! Returns a device for a given \p key.
150150

rest_devices.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ int RestDevices::getAllDevices(const ApiRequest &req, ApiResponse &rsp)
6868

6969
rsp.httpStatus = HttpStatusOk;
7070

71-
for (const auto &d : plugin->m_devices)
71+
for (const Device *d : plugin->m_devices)
7272
{
73-
if (d.second->node()) // for now only physical devices
73+
if (d) // for now only physical devices
7474
{
75-
rsp.list.push_back(d.second->item(RAttrUniqueId)->toString());
75+
rsp.list.push_back(d->item(RAttrUniqueId)->toString());
7676
}
7777
}
7878

0 commit comments

Comments
 (0)