From 055be9b058d5bdf806106725567e7190bbf78900 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 19 Jun 2024 07:11:18 +0200 Subject: [PATCH] Refactor to be more input/output, remove globals from parsing --- src/transcript_browser/main.cpp | 97 ++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/src/transcript_browser/main.cpp b/src/transcript_browser/main.cpp index 72fe745..7dc576d 100644 --- a/src/transcript_browser/main.cpp +++ b/src/transcript_browser/main.cpp @@ -8,14 +8,6 @@ #include #include -struct XToTimeString { - String string; // String inside transcript arena - uint16_t hour; - uint16_t minute; - uint16_t second; - String filepath; -}; - struct TimeString { uint16_t hour; uint16_t minute; @@ -23,15 +15,6 @@ struct TimeString { String string; }; -struct TimeFile { - Array time_strings; - String file; -}; - -bool AppInitializedWithFolder; -Arena XArena; -Arena Perm; - Array ParseSrtFile(Arena *arena, String filename) { String content = ReadFile(*arena, filename); Array lines = Split(*arena, content, "\n"); @@ -75,31 +58,46 @@ Array ParseSrtFile(Arena *arena, String filename) { return time_strings; } -Array ArenasToFree; -Array TimeFiles; -std::mutex ParseThreadMutex; -void ParseThreadEntry(Array files) { - Arena *arena = AllocArena(); - ParseThreadMutex.lock(); - ArenasToFree.add(arena); - ParseThreadMutex.unlock(); - For(files) { - Array time_strings = ParseSrtFile(arena, it); - ParseThreadMutex.lock(); - TimeFiles.add({time_strings, it}); - ParseThreadMutex.unlock(); +struct TimeFile { + Array time_strings; + String file; +}; + +struct ParseThreadIO { + Array input_files; + + // output + Arena *arena; + Array time_files; +}; + +void ParseThreadEntry(ParseThreadIO *io) { + io->arena = AllocArena(); + io->time_files.allocator = *io->arena; + For(io->input_files) { + Array time_strings = ParseSrtFile(io->arena, it); + io->time_files.add({time_strings, it}); } } -Array XToTimeStringArray = {Perm}; -Array InitForFolder(String folder) { - Scratch scratch; - ArenasToFree.allocator = scratch; - TimeFiles.allocator = scratch; +struct XToTimeString { + String string; // String inside transcript arena + uint16_t hour; + uint16_t minute; + uint16_t second; + String filepath; +}; - int thread_count = 16; - Array filenames = {Perm}; - Array srt_files = {scratch}; +Arena Perm; +Array XToTimeStringArray = {Perm}; +bool AppInitializedWithFolder; +Arena XArena; + +Array InitForFolder(String folder) { + Scratch scratch; + + Array filenames = {Perm}; + Array srt_files = {scratch}; for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) { filenames.add(Copy(Perm, iter.absolute_path)); if (EndsWith(iter.filename, ".srt")) { @@ -107,17 +105,24 @@ Array InitForFolder(String folder) { } } + int thread_count = 16; Array threads = {scratch}; int files_per_thread = srt_files.len / thread_count; int remainder = srt_files.len % thread_count; int fi = 0; + + Array io = {scratch}; + io.reserve(thread_count); for (int ti = 0; ti < thread_count; ti += 1) { Array files = {scratch}; for (int i = 0; fi < srt_files.len && i < files_per_thread + remainder; fi += 1, i += 1) { files.add(srt_files[fi]); } if (remainder) remainder = 0; - threads.add(new std::thread(ParseThreadEntry, files)); + + ParseThreadIO *i = io.alloc(); + i->input_files = files; + threads.add(new std::thread(ParseThreadEntry, i)); } For(threads) { @@ -125,14 +130,16 @@ Array InitForFolder(String folder) { delete it; } - ForItem(file, TimeFiles) { - For(file.time_strings) { - String s = Copy(XArena, it.string); - s.data[s.len] = ' '; - XToTimeStringArray.add({s, it.hour, it.minute, it.second, file.file}); + ForItem(it_io, io) { + ForItem(it_time_file, it_io.time_files) { + For(it_time_file.time_strings) { + String s = Copy(XArena, it.string); + s.data[s.len] = ' '; + XToTimeStringArray.add({s, it.hour, it.minute, it.second, it_time_file.file}); + } } + Release(it_io.arena); } - For(ArenasToFree) Release(it); AppInitializedWithFolder = true; return filenames;