Commit 0ab5ecea authored by louiz’'s avatar louiz’
Browse files

Cache the result of jidprep()

Avoid doing repetitive calculations, if we call jidprep() on the same JID multiple times
parent 7bfe695c
...@@ -288,7 +288,12 @@ int main() ...@@ -288,7 +288,12 @@ int main()
// Jidprep // Jidprep
const std::string& badjid("~zigougou™@EpiK-7D9D1FDE.poez.io/Boujour/coucou/slt™"); const std::string& badjid("~zigougou™@EpiK-7D9D1FDE.poez.io/Boujour/coucou/slt™");
const std::string correctjid = jidprep(badjid); const std::string correctjid = jidprep(badjid);
std::cout << correctjid << std::endl;
assert(correctjid == "~zigougoutm@epik-7d9d1fde.poez.io/Boujour/coucou/sltTM"); assert(correctjid == "~zigougoutm@epik-7d9d1fde.poez.io/Boujour/coucou/sltTM");
// Check that the cache do not break things when we prep the same string
// multiple times
assert(jidprep(badjid) == "~zigougoutm@epik-7d9d1fde.poez.io/Boujour/coucou/sltTM");
assert(jidprep(badjid) == "~zigougoutm@epik-7d9d1fde.poez.io/Boujour/coucou/sltTM");
const std::string& badjid2("Zigougou@poez.io"); const std::string& badjid2("Zigougou@poez.io");
const std::string correctjid2 = jidprep(badjid2); const std::string correctjid2 = jidprep(badjid2);
......
#include <xmpp/jid.hpp> #include <xmpp/jid.hpp>
#include <config.h> #include <config.h>
#include <cstring> #include <cstring>
#include <map>
#ifdef LIBIDN_FOUND #ifdef LIBIDN_FOUND
#include <stringprep.h> #include <stringprep.h>
...@@ -35,7 +36,14 @@ static constexpr size_t max_jid_part_len = 1023; ...@@ -35,7 +36,14 @@ static constexpr size_t max_jid_part_len = 1023;
std::string jidprep(const std::string& original) std::string jidprep(const std::string& original)
{ {
#ifdef LIBIDN_FOUND #ifdef LIBIDN_FOUND
// TODO: cache the result using CacheType = std::map<std::string, std::string>;
static CacheType cache;
std::pair<CacheType::iterator, bool> 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:"); const std::string error_msg("Failed to convert " + original + " into a valid JID:");
Jid jid(original); Jid jid(original);
...@@ -61,7 +69,10 @@ std::string jidprep(const std::string& original) ...@@ -61,7 +69,10 @@ std::string jidprep(const std::string& original)
// If there is no resource, stop here // If there is no resource, stop here
if (jid.resource.empty()) 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 // Otherwise, also process the resource part
char resource[max_jid_part_len] = {}; char resource[max_jid_part_len] = {};
...@@ -73,7 +84,8 @@ std::string jidprep(const std::string& original) ...@@ -73,7 +84,8 @@ std::string jidprep(const std::string& original)
log_error(error_msg + stringprep_strerror(rc)); log_error(error_msg + stringprep_strerror(rc));
return ""; return "";
} }
return std::string(local) + "@" + domain + "/" + resource; std::get<0>(cached)->second = std::string(local) + "@" + domain + "/" + resource;
return std::get<0>(cached)->second;
#else #else
(void)original; (void)original;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment