From b8e382b011e524ed4577fbc5b0871f86f1c196d4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 23 Jan 2014 22:18:40 +0100 Subject: db/upnp/Discovery: use std::list instead of std::map There will only be very few items, and that does not justify the bloat of std::map. --- src/db/upnp/Discovery.cxx | 36 +++++++++++++++++++++++++----------- src/db/upnp/Discovery.hxx | 13 ++++++++----- 2 files changed, 33 insertions(+), 16 deletions(-) (limited to 'src/db/upnp') diff --git a/src/db/upnp/Discovery.cxx b/src/db/upnp/Discovery.cxx index 4aeb381fd..5203dba83 100644 --- a/src/db/upnp/Discovery.cxx +++ b/src/db/upnp/Discovery.cxx @@ -54,19 +54,32 @@ isMSDevice(const char *st) } inline void -UPnPDeviceDirectory::LockAdd(std::string &&id, ContentDirectoryDescriptor &&d) +UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d) { const ScopeLock protect(mutex); - directories[std::move(id)] = std::move(d); + + for (auto &i : directories) { + if (i.id == d.id) { + i = std::move(d); + return; + } + } + + directories.emplace_back(std::move(d)); } inline void UPnPDeviceDirectory::LockRemove(const std::string &id) { const ScopeLock protect(mutex); - auto i = directories.find(id); - if (i != directories.end()) - directories.erase(i); + + for (auto i = directories.begin(), end = directories.end(); + i != end; ++i) { + if (i->id == id) { + directories.erase(i); + break; + } + } } inline void @@ -91,7 +104,8 @@ UPnPDeviceDirectory::discoExplorer() } // Update or insert the device - ContentDirectoryDescriptor d(MonotonicClockS(), tsk->expires); + ContentDirectoryDescriptor d(std::move(tsk->deviceId), + MonotonicClockS(), tsk->expires); { Error error2; @@ -104,7 +118,7 @@ UPnPDeviceDirectory::discoExplorer() } } - LockAdd(std::move(tsk->deviceId), std::move(d)); + LockAdd(std::move(d)); delete tsk; } } @@ -180,7 +194,7 @@ UPnPDeviceDirectory::expireDevices(Error &error) for (auto it = directories.begin(); it != directories.end();) { - if (now > it->second.expires) { + if (now > it->expires) { it = directories.erase(it); didsomething = true; } else { @@ -263,9 +277,9 @@ UPnPDeviceDirectory::getDirServices(std::vector &out, for (auto dit = directories.begin(); dit != directories.end(); dit++) { - for (const auto &service : dit->second.device.services) { + for (const auto &service : dit->device.services) { if (isCDService(service.serviceType.c_str())) { - out.emplace_back(dit->second.device, service); + out.emplace_back(dit->device, service); } } } @@ -285,7 +299,7 @@ UPnPDeviceDirectory::getServer(const char *friendlyName, const ScopeLock protect(mutex); for (const auto &i : directories) { - const auto &device = i.second.device; + const auto &device = i.device; if (device.friendlyName != friendlyName) continue; diff --git a/src/db/upnp/Discovery.hxx b/src/db/upnp/Discovery.hxx index fec9fe1fb..4c64fe420 100644 --- a/src/db/upnp/Discovery.hxx +++ b/src/db/upnp/Discovery.hxx @@ -27,7 +27,7 @@ #include -#include +#include #include #include @@ -63,6 +63,8 @@ class UPnPDeviceDirectory { */ class ContentDirectoryDescriptor { public: + std::string id; + UPnPDevice device; /** @@ -73,8 +75,9 @@ class UPnPDeviceDirectory { ContentDirectoryDescriptor() = default; - ContentDirectoryDescriptor(unsigned last, int exp) - :expires(last + exp + 20) {} + ContentDirectoryDescriptor(std::string &&_id, + unsigned last, int exp) + :id(std::move(_id)), expires(last + exp + 20) {} bool Parse(const std::string &url, const char *description, Error &_error) { @@ -85,7 +88,7 @@ class UPnPDeviceDirectory { LibUPnP *const lib; Mutex mutex; - std::map directories; + std::list directories; WorkQueue discoveredQueue; /** @@ -129,7 +132,7 @@ private: */ bool expireDevices(Error &error); - void LockAdd(std::string &&id, ContentDirectoryDescriptor &&d); + void LockAdd(ContentDirectoryDescriptor &&d); void LockRemove(const std::string &id); /** -- cgit v1.2.3