From 0ab5ecea133cef5a009bc34ba42c4eaacde6a7dd Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 24 Nov 2014 03:38:10 +0100 Subject: Cache the result of jidprep() Avoid doing repetitive calculations, if we call jidprep() on the same JID multiple times --- src/xmpp/jid.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/xmpp/jid.cpp') diff --git a/src/xmpp/jid.cpp b/src/xmpp/jid.cpp index e3b0c8f..c51e011 100644 --- a/src/xmpp/jid.cpp +++ b/src/xmpp/jid.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #ifdef LIBIDN_FOUND #include @@ -35,7 +36,14 @@ static constexpr size_t max_jid_part_len = 1023; std::string jidprep(const std::string& original) { #ifdef LIBIDN_FOUND - // TODO: cache the result + using CacheType = std::map; + static CacheType cache; + std::pair cached = cache.insert({original, {}}); + if (std::get<1>(cached) == false) + { // Insertion failed: the result is already in the cache, return it + return std::get<0>(cached)->second; + } + const std::string error_msg("Failed to convert " + original + " into a valid JID:"); Jid jid(original); @@ -61,7 +69,10 @@ std::string jidprep(const std::string& original) // If there is no resource, stop here if (jid.resource.empty()) - return std::string(local) + "@" + domain; + { + std::get<0>(cached)->second = std::string(local) + "@" + domain; + return std::get<0>(cached)->second; + } // Otherwise, also process the resource part char resource[max_jid_part_len] = {}; @@ -73,7 +84,8 @@ std::string jidprep(const std::string& original) log_error(error_msg + stringprep_strerror(rc)); return ""; } - return std::string(local) + "@" + domain + "/" + resource; + std::get<0>(cached)->second = std::string(local) + "@" + domain + "/" + resource; + return std::get<0>(cached)->second; #else (void)original; -- cgit v1.2.3