perf improvement
This commit is contained in:
parent
ba83760157
commit
632139dce2
@ -4,7 +4,6 @@
|
||||
#include "text.h"
|
||||
#include "types.h"
|
||||
#include "misc.h"
|
||||
#include <shared_mutex>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QDropEvent>
|
||||
#include <QMimeData>
|
||||
@ -21,7 +20,7 @@ namespace
|
||||
~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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ void TextThread::Flush()
|
||||
{
|
||||
std::vector<std::wstring> sentences;
|
||||
queuedSentences->swap(sentences);
|
||||
for (auto sentence : sentences)
|
||||
for (auto& sentence : sentences)
|
||||
if (Output(this, sentence)) storage->append(sentence);
|
||||
|
||||
std::scoped_lock lock(bufferMutex);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <QTimer>
|
||||
|
||||
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;
|
||||
|
@ -13,5 +13,6 @@
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
Loading…
Reference in New Issue
Block a user