Refactor to be more input/output, remove globals from parsing
This commit is contained in:
@@ -8,14 +8,6 @@
|
||||
#include <semaphore>
|
||||
#include <mutex>
|
||||
|
||||
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<TimeString> time_strings;
|
||||
String file;
|
||||
};
|
||||
|
||||
bool AppInitializedWithFolder;
|
||||
Arena XArena;
|
||||
Arena Perm;
|
||||
|
||||
Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
|
||||
String content = ReadFile(*arena, filename);
|
||||
Array<String> lines = Split(*arena, content, "\n");
|
||||
@@ -75,31 +58,46 @@ Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
|
||||
return time_strings;
|
||||
}
|
||||
|
||||
Array<Arena *> ArenasToFree;
|
||||
Array<TimeFile> TimeFiles;
|
||||
std::mutex ParseThreadMutex;
|
||||
void ParseThreadEntry(Array<String> files) {
|
||||
Arena *arena = AllocArena();
|
||||
ParseThreadMutex.lock();
|
||||
ArenasToFree.add(arena);
|
||||
ParseThreadMutex.unlock();
|
||||
For(files) {
|
||||
Array<TimeString> time_strings = ParseSrtFile(arena, it);
|
||||
ParseThreadMutex.lock();
|
||||
TimeFiles.add({time_strings, it});
|
||||
ParseThreadMutex.unlock();
|
||||
struct TimeFile {
|
||||
Array<TimeString> time_strings;
|
||||
String file;
|
||||
};
|
||||
|
||||
struct ParseThreadIO {
|
||||
Array<String> input_files;
|
||||
|
||||
// output
|
||||
Arena *arena;
|
||||
Array<TimeFile> time_files;
|
||||
};
|
||||
|
||||
void ParseThreadEntry(ParseThreadIO *io) {
|
||||
io->arena = AllocArena();
|
||||
io->time_files.allocator = *io->arena;
|
||||
For(io->input_files) {
|
||||
Array<TimeString> time_strings = ParseSrtFile(io->arena, it);
|
||||
io->time_files.add({time_strings, it});
|
||||
}
|
||||
}
|
||||
|
||||
Array<XToTimeString> XToTimeStringArray = {Perm};
|
||||
Array<String> 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<String> filenames = {Perm};
|
||||
Array<String> srt_files = {scratch};
|
||||
Arena Perm;
|
||||
Array<XToTimeString> XToTimeStringArray = {Perm};
|
||||
bool AppInitializedWithFolder;
|
||||
Arena XArena;
|
||||
|
||||
Array<String> InitForFolder(String folder) {
|
||||
Scratch scratch;
|
||||
|
||||
Array<String> filenames = {Perm};
|
||||
Array<String> 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<String> InitForFolder(String folder) {
|
||||
}
|
||||
}
|
||||
|
||||
int thread_count = 16;
|
||||
Array<std::thread *> threads = {scratch};
|
||||
int files_per_thread = srt_files.len / thread_count;
|
||||
int remainder = srt_files.len % thread_count;
|
||||
int fi = 0;
|
||||
|
||||
Array<ParseThreadIO> io = {scratch};
|
||||
io.reserve(thread_count);
|
||||
for (int ti = 0; ti < thread_count; ti += 1) {
|
||||
Array<String> 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<String> 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;
|
||||
|
||||
Reference in New Issue
Block a user