From 286dcae5e087db817f3350cf442145107b25bc9c Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 9 Sep 2023 21:41:36 -0700 Subject: Constrain log file, UI text field, and transcript sizes Log file is constrained to 1 MB and UI to 100-200 lines. 1k lines is too high to keep the UI from lagging. Transcript is constrained to 4k characters. Also put a 5 ms sleep in the transcription hot path. --- GUI/GUI/GUI/Logging.cpp | 28 ++++++++++++++++++++++------ Scripts/transcribe_v2.py | 9 +++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/GUI/GUI/GUI/Logging.cpp b/GUI/GUI/GUI/Logging.cpp index 6b42bd6..5d0e23e 100644 --- a/GUI/GUI/GUI/Logging.cpp +++ b/GUI/GUI/GUI/Logging.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -34,7 +35,8 @@ void Logging::ThreadLogger::Append(wxTextCtrl* frame, const std::string&& messag void Logging::ThreadLogger::Drain() { std::scoped_lock l(mu_); - std::ofstream log_ofs("Resources/log.txt", std::ios_base::app); + const std::filesystem::path log_path("Resources/log.txt"); + std::ofstream log_ofs(log_path, std::ios_base::app); for (const auto& [frame, messages] : messages_) { for (const auto& message : messages) { if (frame) { @@ -46,14 +48,15 @@ void Logging::ThreadLogger::Drain() log_ofs << message; } - // Constrain wxTextCtrl's to 1000 lines to keep memory in check. + // Constrain wxTextCtrl's to 100-200 lines to keep memory usage / + // general snappiness in check. if (frame) { wxString allText = frame->GetValue(); wxArrayString lines = wxStringTokenize(allText, "\n"); size_t count = lines.GetCount(); - if (count > 2000) { - // Keep only the last 1000 lines. - size_t linesToRemove = count - 1000; + if (count > 200) { + // Keep only the last 100 lines. + size_t linesToRemove = count - 100; // Remove lines from the beginning lines.RemoveAt(0, linesToRemove); @@ -67,9 +70,22 @@ void Logging::ThreadLogger::Drain() } } } - log_ofs.close(); messages_.clear(); + + // Drop first 50% of lines in file if larger than 1 MB. + if (std::filesystem::file_size(log_path) > 1024 * 1024) { + std::vector lines; + std::ifstream log_ifs(log_path); + std::string line; + while (std::getline(log_ifs, line)) { + lines.push_back(std::move(line)); + } + log_ofs = std::ofstream(log_path); + for (int i = lines.size() / 2; i < lines.size(); i++) { + log_ofs << lines[i]; + } + } } std::string Logging::HidePII(const std::string&& str, diff --git a/Scripts/transcribe_v2.py b/Scripts/transcribe_v2.py index eae3830..81a4bf2 100644 --- a/Scripts/transcribe_v2.py +++ b/Scripts/transcribe_v2.py @@ -566,6 +566,8 @@ def evaluate(cfg, last_commit_ts = None while True: + time.sleep(.005) + commit = committer.getDelta() if last_commit_ts != None and collector.now() - last_commit_ts > 30: @@ -577,6 +579,8 @@ def evaluate(cfg, last_commit_ts = collector.now() transcript += commit.delta + # Hard-cap transcript length at 4k. + transcript = transcript[-4096:] preview = commit.preview if False and len(commit.delta): @@ -657,6 +661,8 @@ def optimize(cfg, def transcriptionThread(ctrl: ThreadControl): while ctrl.run_app: + time.sleep(.005) + op = None commit = ctrl.committer.getDelta() @@ -673,6 +679,9 @@ def transcriptionThread(ctrl: ThreadControl): print("Finalized: 1") ctrl.transcript += commit.delta + # Hard-cap transcript length at 4k characters to prevent runaway memory + # use. + ctrl.transcript = ctrl.transcript[-4096:] ctrl.preview = ctrl.transcript + commit.preview def vrInputThread(ctrl: ThreadControl): -- cgit v1.2.3