fix several perf issues in textthread

This commit is contained in:
Akash Mozumdar 2018-11-09 04:24:33 -05:00
parent 22bb1420c1
commit e489c38990
2 changed files with 16 additions and 12 deletions

View File

@ -44,20 +44,24 @@ void TextThread::Push(const BYTE* data, int len)
void TextThread::Flush() void TextThread::Flush()
{ {
std::wstring sentence; std::unique_lock locker(threadMutex);
if (buffer.empty()) return;
if (buffer.size() > maxBufferSize || GetTickCount() - lastPushTime > flushDelay)
{ {
LOCK(threadMutex); std::wstring sentence = buffer;
if (buffer.empty()) return;
if (buffer.size() < maxBufferSize && GetTickCount() - lastPushTime < flushDelay) return;
sentence = buffer;
buffer.clear(); buffer.clear();
bool hasRepetition = false; locker.unlock(); // This algorithm might take a while
for (std::wsmatch results; std::regex_search(sentence, results, std::wregex(L"([^\\x00]{6,})\\1\\1")); hasRepetition = true) sentence = results[1]; std::unordered_set<wchar_t> repeatingChars;
if (hasRepetition) repeatingChars = std::unordered_set<wchar_t>(sentence.begin(), sentence.end()); for (std::wsmatch results; std::regex_search(sentence, results, std::wregex(L"([^\\x00]{6,})\\1\\1")); sentence = results[1])
else repeatingChars.clear(); repeatingChars = std::unordered_set(sentence.begin(), sentence.end());
locker.lock();
this->repeatingChars = repeatingChars;
locker.unlock();
AddSentence(sentence);
} }
AddSentence(sentence);
} }
// EOF // EOF

View File

@ -14,8 +14,8 @@ public:
inline static OutputCallback Output; inline static OutputCallback Output;
inline static int flushDelay = 250; // flush every 250ms by default inline static int flushDelay = 400; // flush every 400ms by default
inline static int maxBufferSize = 200; inline static int maxBufferSize = 1000;
inline static int threadCounter = 0; inline static int threadCounter = 0;
TextThread(ThreadParam tp, HookParam hp, std::wstring name); TextThread(ThreadParam tp, HookParam hp, std::wstring name);