Commit 21c266c4a72c4b0abf143f9a703ea4d1ba2c73fb
1 parent
05b38f94
Save a memory access for the subscription root
Showing
2 changed files
with
10 additions
and
10 deletions
subscriptionstore.cpp
| @@ -63,7 +63,7 @@ void SubscriptionNode::removeSubscriber(const std::shared_ptr<Session> &subscrib | @@ -63,7 +63,7 @@ void SubscriptionNode::removeSubscriber(const std::shared_ptr<Session> &subscrib | ||
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | SubscriptionStore::SubscriptionStore() : | 65 | SubscriptionStore::SubscriptionStore() : |
| 66 | - root(new SubscriptionNode("root")), | 66 | + root("root"), |
| 67 | sessionsByIdConst(sessionsById) | 67 | sessionsByIdConst(sessionsById) |
| 68 | { | 68 | { |
| 69 | 69 | ||
| @@ -76,7 +76,7 @@ void SubscriptionStore::addSubscription(std::shared_ptr<Client> &client, const s | @@ -76,7 +76,7 @@ void SubscriptionStore::addSubscription(std::shared_ptr<Client> &client, const s | ||
| 76 | RWLockGuard lock_guard(&subscriptionsRwlock); | 76 | RWLockGuard lock_guard(&subscriptionsRwlock); |
| 77 | lock_guard.wrlock(); | 77 | lock_guard.wrlock(); |
| 78 | 78 | ||
| 79 | - SubscriptionNode *deepestNode = root.get(); | 79 | + SubscriptionNode *deepestNode = &root; |
| 80 | for(const std::string &subtopic : subtopics) | 80 | for(const std::string &subtopic : subtopics) |
| 81 | { | 81 | { |
| 82 | std::unique_ptr<SubscriptionNode> *selectedChildren = nullptr; | 82 | std::unique_ptr<SubscriptionNode> *selectedChildren = nullptr; |
| @@ -123,7 +123,7 @@ void SubscriptionStore::removeSubscription(std::shared_ptr<Client> &client, cons | @@ -123,7 +123,7 @@ void SubscriptionStore::removeSubscription(std::shared_ptr<Client> &client, cons | ||
| 123 | lock_guard.wrlock(); | 123 | lock_guard.wrlock(); |
| 124 | 124 | ||
| 125 | // TODO: because it's so similar to adding a subscription, make a function to retrieve the deepest node? | 125 | // TODO: because it's so similar to adding a subscription, make a function to retrieve the deepest node? |
| 126 | - SubscriptionNode *deepestNode = root.get(); | 126 | + SubscriptionNode *deepestNode = &root; |
| 127 | for(const std::string &subtopic : subtopics) | 127 | for(const std::string &subtopic : subtopics) |
| 128 | { | 128 | { |
| 129 | std::unique_ptr<SubscriptionNode> *selectedChildren = nullptr; | 129 | std::unique_ptr<SubscriptionNode> *selectedChildren = nullptr; |
| @@ -228,7 +228,7 @@ void SubscriptionStore::publishNonRecursively(const MqttPacket &packet, const st | @@ -228,7 +228,7 @@ void SubscriptionStore::publishNonRecursively(const MqttPacket &packet, const st | ||
| 228 | } | 228 | } |
| 229 | 229 | ||
| 230 | void SubscriptionStore::publishRecursively(std::vector<std::string>::const_iterator cur_subtopic_it, std::vector<std::string>::const_iterator end, | 230 | void SubscriptionStore::publishRecursively(std::vector<std::string>::const_iterator cur_subtopic_it, std::vector<std::string>::const_iterator end, |
| 231 | - std::unique_ptr<SubscriptionNode> &this_node, const MqttPacket &packet) const | 231 | + SubscriptionNode *this_node, const MqttPacket &packet) const |
| 232 | { | 232 | { |
| 233 | if (cur_subtopic_it == end) // This is the end of the topic path, so look for subscribers here. | 233 | if (cur_subtopic_it == end) // This is the end of the topic path, so look for subscribers here. |
| 234 | { | 234 | { |
| @@ -252,12 +252,12 @@ void SubscriptionStore::publishRecursively(std::vector<std::string>::const_itera | @@ -252,12 +252,12 @@ void SubscriptionStore::publishRecursively(std::vector<std::string>::const_itera | ||
| 252 | const auto &sub_node = this_node->children.find(cur_subtop); | 252 | const auto &sub_node = this_node->children.find(cur_subtop); |
| 253 | if (sub_node != this_node->children.end()) | 253 | if (sub_node != this_node->children.end()) |
| 254 | { | 254 | { |
| 255 | - publishRecursively(next_subtopic, end, sub_node->second, packet); | 255 | + publishRecursively(next_subtopic, end, sub_node->second.get(), packet); |
| 256 | } | 256 | } |
| 257 | 257 | ||
| 258 | if (this_node->childrenPlus) | 258 | if (this_node->childrenPlus) |
| 259 | { | 259 | { |
| 260 | - publishRecursively(next_subtopic, end, this_node->childrenPlus, packet); | 260 | + publishRecursively(next_subtopic, end, this_node->childrenPlus.get(), packet); |
| 261 | } | 261 | } |
| 262 | } | 262 | } |
| 263 | 263 | ||
| @@ -268,7 +268,7 @@ void SubscriptionStore::queuePacketAtSubscribers(const std::vector<std::string> | @@ -268,7 +268,7 @@ void SubscriptionStore::queuePacketAtSubscribers(const std::vector<std::string> | ||
| 268 | RWLockGuard lock_guard(&subscriptionsRwlock); | 268 | RWLockGuard lock_guard(&subscriptionsRwlock); |
| 269 | lock_guard.rdlock(); | 269 | lock_guard.rdlock(); |
| 270 | 270 | ||
| 271 | - publishRecursively(subtopics.begin(), subtopics.end(), root, packet); | 271 | + publishRecursively(subtopics.begin(), subtopics.end(), &root, packet); |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | void SubscriptionStore::giveClientRetainedMessages(const std::shared_ptr<Session> &ses, const std::string &subscribe_topic, char max_qos) | 274 | void SubscriptionStore::giveClientRetainedMessages(const std::shared_ptr<Session> &ses, const std::string &subscribe_topic, char max_qos) |
| @@ -392,7 +392,7 @@ void SubscriptionStore::removeExpiredSessionsClients() | @@ -392,7 +392,7 @@ void SubscriptionStore::removeExpiredSessionsClients() | ||
| 392 | 392 | ||
| 393 | logger->logf(LOG_NOTICE, "Rebuilding subscription tree"); | 393 | logger->logf(LOG_NOTICE, "Rebuilding subscription tree"); |
| 394 | 394 | ||
| 395 | - root->cleanSubscriptions(); | 395 | + root.cleanSubscriptions(); |
| 396 | } | 396 | } |
| 397 | 397 | ||
| 398 | // QoS is not used in the comparision. This means you upgrade your QoS by subscribing again. The | 398 | // QoS is not used in the comparision. This means you upgrade your QoS by subscribing again. The |
subscriptionstore.h
| @@ -69,7 +69,7 @@ public: | @@ -69,7 +69,7 @@ public: | ||
| 69 | 69 | ||
| 70 | class SubscriptionStore | 70 | class SubscriptionStore |
| 71 | { | 71 | { |
| 72 | - std::unique_ptr<SubscriptionNode> root; | 72 | + SubscriptionNode root; |
| 73 | pthread_rwlock_t subscriptionsRwlock = PTHREAD_RWLOCK_INITIALIZER; | 73 | pthread_rwlock_t subscriptionsRwlock = PTHREAD_RWLOCK_INITIALIZER; |
| 74 | std::unordered_map<std::string, std::shared_ptr<Session>> sessionsById; | 74 | std::unordered_map<std::string, std::shared_ptr<Session>> sessionsById; |
| 75 | const std::unordered_map<std::string, std::shared_ptr<Session>> &sessionsByIdConst; | 75 | const std::unordered_map<std::string, std::shared_ptr<Session>> &sessionsByIdConst; |
| @@ -81,7 +81,7 @@ class SubscriptionStore | @@ -81,7 +81,7 @@ class SubscriptionStore | ||
| 81 | 81 | ||
| 82 | void publishNonRecursively(const MqttPacket &packet, const std::vector<Subscription> &subscribers) const; | 82 | void publishNonRecursively(const MqttPacket &packet, const std::vector<Subscription> &subscribers) const; |
| 83 | void publishRecursively(std::vector<std::string>::const_iterator cur_subtopic_it, std::vector<std::string>::const_iterator end, | 83 | void publishRecursively(std::vector<std::string>::const_iterator cur_subtopic_it, std::vector<std::string>::const_iterator end, |
| 84 | - std::unique_ptr<SubscriptionNode> &next, const MqttPacket &packet) const; | 84 | + SubscriptionNode *this_node, const MqttPacket &packet) const; |
| 85 | 85 | ||
| 86 | public: | 86 | public: |
| 87 | SubscriptionStore(); | 87 | SubscriptionStore(); |