mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-12 02:40:00 +01:00
Performance improvement for transliteration
Tracing CPU usage identified the cost of ICU transliteration: using a static dictionnary to improve performance
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <unicode/ustream.h>
|
#include <unicode/ustream.h>
|
||||||
@@ -52,6 +53,13 @@ std::string BaseToolkit::uniqueName(const std::string &basename, std::list<std::
|
|||||||
|
|
||||||
std::string BaseToolkit::transliterate(const std::string &input)
|
std::string BaseToolkit::transliterate(const std::string &input)
|
||||||
{
|
{
|
||||||
|
// because icu::Transliterator is slow, we keep a dictionnary of already
|
||||||
|
// transliterated texts to be faster during repeated calls (update of user interface)
|
||||||
|
static std::map<std::string, std::string> dictionnary_;
|
||||||
|
std::map<std::string, std::string>::const_iterator existingentry = dictionnary_.find(input);
|
||||||
|
|
||||||
|
if (existingentry == dictionnary_.cend()) {
|
||||||
|
|
||||||
auto ucs = icu::UnicodeString::fromUTF8(input);
|
auto ucs = icu::UnicodeString::fromUTF8(input);
|
||||||
|
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
@@ -68,7 +76,12 @@ std::string BaseToolkit::transliterate(const std::string &input)
|
|||||||
std::ostringstream output;
|
std::ostringstream output;
|
||||||
output << ucs;
|
output << ucs;
|
||||||
|
|
||||||
return output.str();
|
// remember for future
|
||||||
|
dictionnary_[input] = output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
// return remembered transliterated text
|
||||||
|
return dictionnary_[input];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user