SearchHighlight compiles the search pattern once per frame and yields each line's match ranges; the terminal, ImGui and Qt renderers had three copies of this, and Qt compiled a std::regex for every visible line on every frame. Qt now uses kte::Regex like the others. kte::Regex::RunGuarded replaces Command.cc's run_regex_work, which chose the large stack by comparing engine names. ForEachRegexMatch had no callers left and is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
53 lines
972 B
C++
53 lines
972 B
C++
#include "RegexGuard.h"
|
|
|
|
#include <exception>
|
|
#include <pthread.h>
|
|
|
|
namespace kte {
|
|
namespace {
|
|
// 1 GiB supports matches spanning several million characters.
|
|
constexpr std::size_t kLargeStackBytes = std::size_t{1} << 30;
|
|
|
|
struct Job {
|
|
const std::function<void()> *fn;
|
|
std::exception_ptr error;
|
|
};
|
|
|
|
|
|
void *
|
|
run_job(void *arg)
|
|
{
|
|
auto *job = static_cast<Job *>(arg);
|
|
try {
|
|
(*job->fn)();
|
|
} catch (...) {
|
|
job->error = std::current_exception();
|
|
}
|
|
return nullptr;
|
|
}
|
|
} // namespace
|
|
|
|
|
|
void
|
|
RunWithLargeStack(const std::function<void()> &fn)
|
|
{
|
|
Job job{&fn, nullptr};
|
|
pthread_attr_t attr;
|
|
bool started = false;
|
|
pthread_t tid{};
|
|
if (pthread_attr_init(&attr) == 0) {
|
|
if (pthread_attr_setstacksize(&attr, kLargeStackBytes) == 0)
|
|
started = pthread_create(&tid, &attr, run_job, &job) == 0;
|
|
pthread_attr_destroy(&attr);
|
|
}
|
|
if (!started) {
|
|
fn();
|
|
return;
|
|
}
|
|
pthread_join(tid, nullptr);
|
|
if (job.error)
|
|
std::rethrow_exception(job.error);
|
|
}
|
|
|
|
} // namespace kte
|