From 237684cfd042917a26274872c4ffe1e3cdd5c3a8 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Fri, 8 Jun 2018 15:26:25 +0900 Subject: Performance optimization --- include/tgbot/EventBroadcaster.h | 6 +++--- include/tgbot/net/HttpParser.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 5a41c70..0a4ebe9 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "tgbot/types/Message.h" #include "tgbot/types/InlineQuery.h" @@ -133,7 +133,7 @@ private: } inline bool broadcastCommand(const std::string command, const Message::Ptr message) const { - std::map::const_iterator iter = _onCommandListeners.find(command); + std::unordered_map::const_iterator iter = _onCommandListeners.find(command); if (iter == _onCommandListeners.end()) { return false; } @@ -162,7 +162,7 @@ private: } std::vector _onAnyMessageListeners; - std::map _onCommandListeners; + std::unordered_map _onCommandListeners; std::vector _onUnknownCommandListeners; std::vector _onNonCommandMessageListeners; std::vector _onInlineQueryListeners; diff --git a/include/tgbot/net/HttpParser.h b/include/tgbot/net/HttpParser.h index ec4c077..d40619a 100644 --- a/include/tgbot/net/HttpParser.h +++ b/include/tgbot/net/HttpParser.h @@ -24,7 +24,7 @@ #define TGBOT_HTTPPARSER_H #include -#include +#include #include #include "tgbot/net/Url.h" @@ -43,7 +43,7 @@ public: std::string generateWwwFormUrlencoded(const std::vector& args); std::string generateResponse(const std::string& data, const std::string& mimeType = "text/plain", short unsigned statusCode = 200, const std::string& statusStr = "OK", bool isKeepAlive = false); - inline std::string parseRequest(const std::string& data, std::map& headers) { + inline std::string parseRequest(const std::string& data, std::unordered_map& headers) { return parseHttp(true, data, headers); } @@ -51,7 +51,7 @@ public: return parseHttp(true, data); } - inline std::string parseResponse(const std::string& data, std::map& headers) { + inline std::string parseResponse(const std::string& data, std::unordered_map& headers) { return parseHttp(false, data, headers); } @@ -60,7 +60,7 @@ public: } private: - std::string parseHttp(bool isRequest, const std::string& data, std::map& headers); + std::string parseHttp(bool isRequest, const std::string& data, std::unordered_map& headers); std::string parseHttp(bool isRequest, const std::string& data); }; -- cgit v1.2.3 From 093833a785cb25faf4afe3b5652c7598d778866f Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Fri, 8 Jun 2018 16:41:28 +0900 Subject: Resolve Travis-CI test --- include/tgbot/net/HttpServer.h | 4 ++-- include/tgbot/net/TgWebhookServer.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/tgbot/net/HttpServer.h b/include/tgbot/net/HttpServer.h index e760e29..34cd9c7 100644 --- a/include/tgbot/net/HttpServer.h +++ b/include/tgbot/net/HttpServer.h @@ -43,7 +43,7 @@ protected: class Connection; public: - typedef std::function)> ServerHandler; + typedef std::function)> ServerHandler; HttpServer(std::shared_ptr> acceptor, const ServerHandler& handler) : _acceptor(acceptor), _handler(handler) { } @@ -80,7 +80,7 @@ protected: void start() { data.reserve(10240); socket->async_receive(data, [this]() { - std::map headers; + std::unordered_map headers; std::string body = HttpParser::getInstance().parseResponse(data, headers); socket->async_send(_handler(body, headers)); }); diff --git a/include/tgbot/net/TgWebhookServer.h b/include/tgbot/net/TgWebhookServer.h index 9352e82..696d126 100644 --- a/include/tgbot/net/TgWebhookServer.h +++ b/include/tgbot/net/TgWebhookServer.h @@ -37,7 +37,7 @@ public: TgWebhookServer(std::shared_ptr> acceptor, const typename HttpServer::ServerHandler& handler) = delete; TgWebhookServer(std::shared_ptr> acceptor, const std::string& path, const EventHandler* eventHandler) : - HttpServer(acceptor, [eventHandler, &path](const std::string& data, const std::map& headers) -> std::string { + HttpServer(acceptor, [eventHandler, &path](const std::string& data, const std::unordered_map& headers) -> std::string { if (headers.at("method") == "POST" && headers.at("path") == path) { eventHandler->handleUpdate(TgTypeParser::getInstance().parseJsonAndGetUpdate(TgTypeParser::getInstance().parseJson(data))); } -- cgit v1.2.3 From b89c8f2fc73e25f1740c191faf0c7e8b9c18c9b9 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Tue, 12 Jun 2018 21:56:16 +0900 Subject: Use std::vector::reserve --- include/tgbot/TgTypeParser.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include') diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index a24184a..6df9f36 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -314,6 +314,7 @@ public: template std::vector> parseJsonAndGetArray(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data) const { std::vector> result; + result.reserve(data.size()); for (const std::pair& innerTreeItem : data) { result.push_back((this->*parseFunc)(innerTreeItem.second)); } @@ -327,6 +328,7 @@ public: if (treeItem == data.not_found()) { return result; } + result.reserve(treeItem->second.size()); for (const std::pair& innerTreeItem : treeItem->second) { result.push_back(parseFunc(innerTreeItem.second)); } @@ -340,6 +342,7 @@ public: if (treeItem == data.not_found()) { return result; } + result.reserve(treeItem->second.size()); for (const std::pair& innerTreeItem : treeItem->second) { result.push_back((this->*parseFunc)(innerTreeItem.second)); } @@ -353,6 +356,7 @@ public: if (treeItem == data.not_found()) { return result; } + result.reserve(treeItem->second.size()); for (const std::pair& innerTreeItem : treeItem->second) { std::vector> innerResult; for (const std::pair& innerInnerTreeItem : innerTreeItem.second) { -- cgit v1.2.3