From faf9162dbd403a92f38d663145eb57f5d9998f5e Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Thu, 1 Oct 2020 00:32:40 +0800 Subject: [PATCH] layered_fs: Make LoadFile and LoadDirectory less recursive The deep recursion has caused issues in certain games with large numbers of files, especially with MSVC builds. Previously the recursion depth is about equal to the number of files present. With this the depth should be about equal to the maximum depth of the directory structure of the RomFS. --- src/core/file_sys/layered_fs.cpp | 26 ++++++++++---------------- src/core/file_sys/layered_fs.h | 10 ++++++---- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/core/file_sys/layered_fs.cpp b/src/core/file_sys/layered_fs.cpp index c4ab89ceb..31d85ee6b 100644 --- a/src/core/file_sys/layered_fs.cpp +++ b/src/core/file_sys/layered_fs.cpp @@ -82,7 +82,7 @@ void LayeredFS::Load() { LayeredFS::~LayeredFS() = default; -void LayeredFS::LoadDirectory(Directory& current, u32 offset) { +u32 LayeredFS::LoadDirectory(Directory& current, u32 offset) { DirectoryMetadata metadata; romfs->ReadFile(header.directory_metadata_table.offset + offset, sizeof(metadata), reinterpret_cast(&metadata)); @@ -92,28 +92,24 @@ void LayeredFS::LoadDirectory(Directory& current, u32 offset) { current.path = current.parent->path + current.name + DIR_SEP; directory_path_map.emplace(current.path, ¤t); - if (metadata.first_file_offset != 0xFFFFFFFF) { - LoadFile(current, metadata.first_file_offset); + u32 file_offset = metadata.first_file_offset; + while (file_offset != 0xFFFFFFFF) { + file_offset = LoadFile(current, file_offset); } - if (metadata.first_child_directory_offset != 0xFFFFFFFF) { + u32 child_directory_offset = metadata.first_child_directory_offset; + while (child_directory_offset != 0xFFFFFFFF) { auto child = std::make_unique(); auto& directory = *child; directory.parent = ¤t; current.directories.emplace_back(std::move(child)); - LoadDirectory(directory, metadata.first_child_directory_offset); + child_directory_offset = LoadDirectory(directory, child_directory_offset); } - if (metadata.next_sibling_offset != 0xFFFFFFFF) { - auto sibling = std::make_unique(); - auto& directory = *sibling; - directory.parent = current.parent; - current.parent->directories.emplace_back(std::move(sibling)); - LoadDirectory(directory, metadata.next_sibling_offset); - } + return metadata.next_sibling_offset; } -void LayeredFS::LoadFile(Directory& parent, u32 offset) { +u32 LayeredFS::LoadFile(Directory& parent, u32 offset) { FileMetadata metadata; romfs->ReadFile(header.file_metadata_table.offset + offset, sizeof(metadata), reinterpret_cast(&metadata)); @@ -129,9 +125,7 @@ void LayeredFS::LoadFile(Directory& parent, u32 offset) { file_path_map.emplace(file->path, file.get()); parent.files.emplace_back(std::move(file)); - if (metadata.next_sibling_offset != 0xFFFFFFFF) { - LoadFile(parent, metadata.next_sibling_offset); - } + return metadata.next_sibling_offset; } std::string LayeredFS::ReadName(u32 offset, u32 name_length) { diff --git a/src/core/file_sys/layered_fs.h b/src/core/file_sys/layered_fs.h index 2a494bd8e..3643be99d 100644 --- a/src/core/file_sys/layered_fs.h +++ b/src/core/file_sys/layered_fs.h @@ -65,11 +65,13 @@ private: std::string ReadName(u32 offset, u32 name_length); - // Loads the current directory, then its siblings, and then its children. - void LoadDirectory(Directory& current, u32 offset); + // Loads the current directory, then its children. + // Returns offset of the next sibling directory to load (0xFFFFFFFF if the last directory) + u32 LoadDirectory(Directory& current, u32 offset); - // Load the file at offset, and then its siblings. - void LoadFile(Directory& parent, u32 offset); + // Load the file at offset. + // Returns offset of the next sibling file to load (0xFFFFFFFF if the last file) + u32 LoadFile(Directory& parent, u32 offset); // Load replace/create relocations void LoadRelocations();