perf improvement

This commit is contained in:
Akash Mozumdar 2019-01-23 13:59:34 -05:00
parent ba83760157
commit 632139dce2
5 changed files with 9 additions and 8 deletions

View File

@ -4,7 +4,6 @@
#include "text.h" #include "text.h"
#include "types.h" #include "types.h"
#include "misc.h" #include "misc.h"
#include <shared_mutex>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QDropEvent> #include <QDropEvent>
#include <QMimeData> #include <QMimeData>
@ -21,7 +20,7 @@ namespace
~InfoForExtension() { if (next) delete next; }; ~InfoForExtension() { if (next) delete next; };
}; };
QHash<QString, wchar_t*(*)(const wchar_t*, const InfoForExtension*)> extensions; QHash<QString, wchar_t*(*)(wchar_t*, const InfoForExtension*)> extensions;
QStringList extenNames; QStringList extenNames;
std::shared_mutex extenMutex; std::shared_mutex extenMutex;
@ -32,7 +31,7 @@ namespace
if (FARPROC callback = GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence")) if (FARPROC callback = GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence"))
{ {
std::scoped_lock writeLock(extenMutex); std::scoped_lock writeLock(extenMutex);
extensions[extenName] = (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback; extensions[extenName] = (wchar_t*(*)(wchar_t*, const InfoForExtension*))callback;
extenNames.push_back(extenName); extenNames.push_back(extenName);
} }
} }

View File

@ -46,7 +46,7 @@ void TextThread::Flush()
{ {
std::vector<std::wstring> sentences; std::vector<std::wstring> sentences;
queuedSentences->swap(sentences); queuedSentences->swap(sentences);
for (auto sentence : sentences) for (auto& sentence : sentences)
if (Output(this, sentence)) storage->append(sentence); if (Output(this, sentence)) storage->append(sentence);
std::scoped_lock lock(bufferMutex); std::scoped_lock lock(bufferMutex);

View File

@ -13,15 +13,16 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
* Textractor will display the sentence after all extensions have had a chance to process and/or modify it. * Textractor will display the sentence after all extensions have had a chance to process and/or modify it.
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE! * THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
*/ */
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo) extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const InfoForExtension* miscInfo)
{ {
try try
{ {
std::wstring sentenceStr(sentence); std::wstring sentenceStr(sentence);
int origLength = sentenceStr.size();
if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo })) if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo }))
{ {
// No need to worry about freeing this: Textractor does it for you. // No need to worry about freeing this: Textractor does it for you.
wchar_t* newSentence = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentenceStr.size() + 1) * sizeof(wchar_t)); wchar_t* newSentence = sentenceStr.size() > origLength ? (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentenceStr.size() + 1) * sizeof(wchar_t)) : sentence;
wcscpy_s(newSentence, sentenceStr.size() + 1, sentenceStr.c_str()); wcscpy_s(newSentence, sentenceStr.size() + 1, sentenceStr.c_str());
return newSentence; return newSentence;
} }

View File

@ -7,7 +7,7 @@
#include <QTimer> #include <QTimer>
std::wregex regex; std::wregex regex;
std::mutex m; std::shared_mutex m;
struct : QMainWindow struct : QMainWindow
{ {
@ -63,7 +63,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
std::lock_guard l(m); std::shared_lock l(m);
if (window == nullptr || sentenceInfo["hook address"] == -1) return false; if (window == nullptr || sentenceInfo["hook address"] == -1) return false;
sentence = std::regex_replace(sentence, regex, L""); sentence = std::regex_replace(sentence, regex, L"");
return true; return true;

View File

@ -13,5 +13,6 @@
#include <optional> #include <optional>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <shared_mutex>
#include <cstdint> #include <cstdint>
#include <cassert> #include <cassert>