From 8014c67faa11e6d2daa402724679170a4a7f32f6 Mon Sep 17 00:00:00 2001 From: Mat M Date: Sun, 19 Apr 2020 03:24:37 -0400 Subject: [PATCH] service: Resolve trivially avoidable copies (#5237) * am/am: Avoid redundant copy in GetProgramInfoFromCia() We can just use a reference to the title metadata. Avoids copying several data entries and std::vector instances that don't need to be copied. * hle/service: Avoid redundant copying of std::string GetUserPath() returns the path as a reference, so we can make use of said reference to avoid making copies. --- src/core/hle/service/am/am.cpp | 2 +- src/core/hle/service/cecd/cecd.cpp | 2 +- src/core/hle/service/cfg/cfg.cpp | 2 +- src/core/hle/service/fs/archive.cpp | 24 ++++++++++++++---------- src/core/hle/service/ptm/ptm.cpp | 10 +++++----- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 240762d25..dce84e94b 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1222,7 +1222,7 @@ void Module::Interface::GetProgramInfoFromCia(Kernel::HLERequestContext& ctx) { return; } - FileSys::TitleMetadata tmd = container.GetTitleMetadata(); + const FileSys::TitleMetadata& tmd = container.GetTitleMetadata(); TitleInfo title_info = {}; container.Print(); diff --git a/src/core/hle/service/cecd/cecd.cpp b/src/core/hle/service/cecd/cecd.cpp index 977a47842..9c3cfa0ab 100644 --- a/src/core/hle/service/cecd/cecd.cpp +++ b/src/core/hle/service/cecd/cecd.cpp @@ -1381,7 +1381,7 @@ Module::Module(Core::System& system) : system(system) { change_state_event = system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "CECD::change_state_event"); - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); FileSys::ArchiveFactory_SystemSaveData systemsavedata_factory(nand_directory); // Open the SystemSaveData archive 0x00010026 diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index fc285f662..982dc03f4 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -555,7 +555,7 @@ ResultCode Module::FormatConfig() { } // namespace Service::CFG ResultCode Module::LoadConfigNANDSaveFile() { - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); FileSys::ArchiveFactory_SystemSaveData systemsavedata_factory(nand_directory); // Open the SystemSaveData archive 0x00010017 diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index c4ed0d42c..8da2bf52e 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp @@ -267,25 +267,29 @@ ResultCode ArchiveManager::DeleteExtSaveData(MediaType media_type, u32 high, u32 ResultCode ArchiveManager::DeleteSystemSaveData(u32 high, u32 low) { // Construct the binary path to the archive first - FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low); + const FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low); - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); - std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory); - std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path); - if (!FileUtil::DeleteDirRecursively(systemsavedata_path)) + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory); + const std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path); + if (!FileUtil::DeleteDirRecursively(systemsavedata_path)) { return ResultCode(-1); // TODO(Subv): Find the right error code + } + return RESULT_SUCCESS; } ResultCode ArchiveManager::CreateSystemSaveData(u32 high, u32 low) { // Construct the binary path to the archive first - FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low); + const FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low); - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); - std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory); - std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path); - if (!FileUtil::CreateFullPath(systemsavedata_path)) + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory); + const std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path); + if (!FileUtil::CreateFullPath(systemsavedata_path)) { return ResultCode(-1); // TODO(Subv): Find the right error code + } + return RESULT_SUCCESS; } diff --git a/src/core/hle/service/ptm/ptm.cpp b/src/core/hle/service/ptm/ptm.cpp index 430d48cdd..9c4c09fad 100644 --- a/src/core/hle/service/ptm/ptm.cpp +++ b/src/core/hle/service/ptm/ptm.cpp @@ -134,7 +134,7 @@ void Module::Interface::CheckNew3DS(Kernel::HLERequestContext& ctx) { } static void WriteGameCoinData(GameCoin gamecoin_data) { - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); FileSys::ArchiveFactory_ExtSaveData extdata_archive_factory(nand_directory, true); FileSys::Path archive_path(ptm_shared_extdata_id); @@ -167,7 +167,7 @@ static void WriteGameCoinData(GameCoin gamecoin_data) { } static GameCoin ReadGameCoinData() { - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); FileSys::ArchiveFactory_ExtSaveData extdata_archive_factory(nand_directory, true); FileSys::Path archive_path(ptm_shared_extdata_id); @@ -197,10 +197,10 @@ static GameCoin ReadGameCoinData() { Module::Module() { // Open the SharedExtSaveData archive 0xF000000B and create the gamecoin.dat file if it doesn't // exist - std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); + const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); FileSys::ArchiveFactory_ExtSaveData extdata_archive_factory(nand_directory, true); - FileSys::Path archive_path(ptm_shared_extdata_id); - auto archive_result = extdata_archive_factory.Open(archive_path, 0); + const FileSys::Path archive_path(ptm_shared_extdata_id); + const auto archive_result = extdata_archive_factory.Open(archive_path, 0); // If the archive didn't exist, write the default game coin file if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) { WriteGameCoinData(default_game_coin);