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 | 63 | |
| 64 | 64 | |
| 65 | 65 | SubscriptionStore::SubscriptionStore() : |
| 66 | - root(new SubscriptionNode("root")), | |
| 66 | + root("root"), | |
| 67 | 67 | sessionsByIdConst(sessionsById) |
| 68 | 68 | { |
| 69 | 69 | |
| ... | ... | @@ -76,7 +76,7 @@ void SubscriptionStore::addSubscription(std::shared_ptr<Client> &client, const s |
| 76 | 76 | RWLockGuard lock_guard(&subscriptionsRwlock); |
| 77 | 77 | lock_guard.wrlock(); |
| 78 | 78 | |
| 79 | - SubscriptionNode *deepestNode = root.get(); | |
| 79 | + SubscriptionNode *deepestNode = &root; | |
| 80 | 80 | for(const std::string &subtopic : subtopics) |
| 81 | 81 | { |
| 82 | 82 | std::unique_ptr<SubscriptionNode> *selectedChildren = nullptr; |
| ... | ... | @@ -123,7 +123,7 @@ void SubscriptionStore::removeSubscription(std::shared_ptr<Client> &client, cons |
| 123 | 123 | lock_guard.wrlock(); |
| 124 | 124 | |
| 125 | 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 | 127 | for(const std::string &subtopic : subtopics) |
| 128 | 128 | { |
| 129 | 129 | std::unique_ptr<SubscriptionNode> *selectedChildren = nullptr; |
| ... | ... | @@ -228,7 +228,7 @@ void SubscriptionStore::publishNonRecursively(const MqttPacket &packet, const st |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 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 | 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 | 252 | const auto &sub_node = this_node->children.find(cur_subtop); |
| 253 | 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 | 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 | 268 | RWLockGuard lock_guard(&subscriptionsRwlock); |
| 269 | 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 | 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 | 392 | |
| 393 | 393 | logger->logf(LOG_NOTICE, "Rebuilding subscription tree"); |
| 394 | 394 | |
| 395 | - root->cleanSubscriptions(); | |
| 395 | + root.cleanSubscriptions(); | |
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 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 | 69 | |
| 70 | 70 | class SubscriptionStore |
| 71 | 71 | { |
| 72 | - std::unique_ptr<SubscriptionNode> root; | |
| 72 | + SubscriptionNode root; | |
| 73 | 73 | pthread_rwlock_t subscriptionsRwlock = PTHREAD_RWLOCK_INITIALIZER; |
| 74 | 74 | std::unordered_map<std::string, std::shared_ptr<Session>> sessionsById; |
| 75 | 75 | const std::unordered_map<std::string, std::shared_ptr<Session>> &sessionsByIdConst; |
| ... | ... | @@ -81,7 +81,7 @@ class SubscriptionStore |
| 81 | 81 | |
| 82 | 82 | void publishNonRecursively(const MqttPacket &packet, const std::vector<Subscription> &subscribers) const; |
| 83 | 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 | 86 | public: |
| 87 | 87 | SubscriptionStore(); | ... | ... |