From 494fcd24c3736de6d976181727f266c13d9f364a Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sun, 2 Jun 2019 23:05:01 -0400 Subject: [PATCH] performance improvement. or maybe not idk my benchmarks make no sense but i think it's better --- GUI/host/textthread.cpp | 13 +++++++++---- GUI/host/textthread.h | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index b32223a..e7a130a 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -23,7 +23,7 @@ void TextThread::Stop() void TextThread::AddSentence(std::wstring&& sentence) { - queuedSentences->emplace_back(std::move(sentence)); + queuedSentences.push(std::move(sentence)); } void TextThread::Push(BYTE* data, int length) @@ -51,15 +51,20 @@ void TextThread::Push(BYTE* data, int length) buffer.clear(); } } + + if (flushDelay == 0 && hp.type & USING_STRING) + { + AddSentence(std::move(buffer)); + buffer.clear(); + } } void TextThread::Flush() { if (storage->size() > 10'000'000) storage->erase(0, 8'000'000); // https://github.com/Artikash/Textractor/issues/127#issuecomment-486882983 - std::vector sentences; - queuedSentences->swap(sentences); - for (auto& sentence : sentences) + std::wstring sentence; + while (queuedSentences.try_pop(sentence)) { sentence.erase(std::remove(sentence.begin(), sentence.end(), L'\0')); if (Output(*this, sentence)) storage->append(sentence); diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 3f546d1..75acb7f 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -2,6 +2,7 @@ #include "common.h" #include "types.h" +#include class TextThread { @@ -36,7 +37,7 @@ private: std::unordered_set repeatingChars; std::mutex bufferMutex; DWORD lastPushTime = 0; - ThreadSafe> queuedSentences; + concurrency::concurrent_queue queuedSentences; struct TimerDeleter { void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } }; AutoHandle timer = NULL; };