From 632139dce26f1c1efbda7bba207a0f08f2279acb Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Wed, 23 Jan 2019 13:59:34 -0500 Subject: [PATCH] perf improvement --- GUI/extenwindow.cpp | 5 ++--- GUI/host/textthread.cpp | 2 +- extensions/extensionimpl.cpp | 5 +++-- extensions/regexfilter.cpp | 4 ++-- include/common.h | 1 + 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index eee5402..7960aeb 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -4,7 +4,6 @@ #include "text.h" #include "types.h" #include "misc.h" -#include #include #include #include @@ -21,7 +20,7 @@ namespace ~InfoForExtension() { if (next) delete next; }; }; - QHash extensions; + QHash extensions; QStringList extenNames; std::shared_mutex extenMutex; @@ -32,7 +31,7 @@ namespace if (FARPROC callback = GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence")) { 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); } } diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index 118ac99..7a71b31 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -46,7 +46,7 @@ void TextThread::Flush() { std::vector sentences; queuedSentences->swap(sentences); - for (auto sentence : sentences) + for (auto& sentence : sentences) if (Output(this, sentence)) storage->append(sentence); std::scoped_lock lock(bufferMutex); diff --git a/extensions/extensionimpl.cpp b/extensions/extensionimpl.cpp index 794d91b..f84ee35 100644 --- a/extensions/extensionimpl.cpp +++ b/extensions/extensionimpl.cpp @@ -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. * 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 { std::wstring sentenceStr(sentence); + int origLength = sentenceStr.size(); if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo })) { // 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()); return newSentence; } diff --git a/extensions/regexfilter.cpp b/extensions/regexfilter.cpp index f4d2734..57eda59 100644 --- a/extensions/regexfilter.cpp +++ b/extensions/regexfilter.cpp @@ -7,7 +7,7 @@ #include std::wregex regex; -std::mutex m; +std::shared_mutex m; 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) { - std::lock_guard l(m); + std::shared_lock l(m); if (window == nullptr || sentenceInfo["hook address"] == -1) return false; sentence = std::regex_replace(sentence, regex, L""); return true; diff --git a/include/common.h b/include/common.h index f648c4a..3f8e5f1 100644 --- a/include/common.h +++ b/include/common.h @@ -13,5 +13,6 @@ #include #include #include +#include #include #include