From: Kornel Benko Date: Sun, 31 Jan 2021 08:53:06 +0000 (+0100) Subject: FindAdv: Optimization X-Git-Tag: 2.4.0-alpha3~149 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=3a1b19c5c363428f424180270e32bc8b468ea54f;p=lyx.git FindAdv: Optimization Using unordered_map instead of map. Reasons: 1.) The relevant maps contain 166(Keys) and 649(Accents) entries. This mean that average access with 'map' needs 8 to 10 compares to find the value. 2.) Since we are using at least c++11, the unordered_map is available 2.) increasing the maps (in future) needs not to be considered anymore, because the access-time will not increase. --- diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index 2507b4617b..93e454fcf1 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -50,7 +50,7 @@ #include "support/lstrings.h" #include "support/textutils.h" -#include +#include #include //#define ResultsDebug @@ -72,8 +72,9 @@ using namespace lyx::support; namespace lyx { -typedef map AccentsMap; -static AccentsMap accents = map(); +typedef unordered_map AccentsMap; +typedef unordered_map::const_iterator AccentsIterator; +static AccentsMap accents = unordered_map(); // Helper class for deciding what should be ignored class IgnoreFormats { @@ -1452,8 +1453,9 @@ void Intervall::removeAccents() for (sregex_iterator itacc(par.begin(), par.end(), accre), end; itacc != end; ++itacc) { sub = *itacc; string key = sub.str(1); - if (accents.find(key) != accents.end()) { - string val = accents[key]; + AccentsIterator it_ac = accents.find(key); + if (it_ac != accents.end()) { + string val = it_ac->second; size_t pos = sub.position(size_t(0)); for (size_t i = 0; i < val.size(); i++) { par[pos+i] = val[i]; @@ -1528,9 +1530,10 @@ int Intervall::nextNotIgnored(int start) const return start; } -typedef map KeysMap; +typedef unordered_map KeysMap; +typedef unordered_map::const_iterator KeysIterator; typedef vector< KeyInfo> Entries; -static KeysMap keys = map(); +static KeysMap keys = unordered_map(); class LatexInfo { private: @@ -1790,9 +1793,10 @@ void LatexInfo::buildEntries(bool isPatternString) key = sub.str(2); } } - if (keys.find(key) != keys.end()) { - if (keys[key].keytype == KeyInfo::headRemove) { - KeyInfo found1 = keys[key]; + KeysIterator it_key = keys.find(key); + if (it_key != keys.end()) { + if (it_key->second.keytype == KeyInfo::headRemove) { + KeyInfo found1 = it_key->second; found1.disabled = true; found1.head = "\\" + key + "{"; found1._tokenstart = sub.position(size_t(2)); @@ -1826,7 +1830,7 @@ void LatexInfo::buildEntries(bool isPatternString) mi.incrEntry(); math_pos = mi.getStartPos(); } - if (keys.find(key) == keys.end()) { + if (it_key == keys.end()) { found = KeyInfo(KeyInfo::isStandard, 0, true); LYXERR(Debug::INFO, "Undefined key " << key << " ==> will be used as text"); found = KeyInfo(KeyInfo::isText, 0, false);