From 370d77f13a2af4ddee48c0d74418ea732799f7d5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Dec 2017 14:12:04 -0500 Subject: [PATCH 1/2] file_sys: std::move std::shared_ptr instances in constructors where applicable By default, a regular copy requires an atomic increment and decrement. A move avoids this from occurring, which makes sense when the constructor is taking the shared_ptr by value. --- src/core/file_sys/archive_ncch.cpp | 2 +- src/core/file_sys/archive_other_savedata.cpp | 5 +++-- src/core/file_sys/archive_savedata.cpp | 3 ++- src/core/file_sys/ivfc_archive.cpp | 7 +++++++ src/core/file_sys/ivfc_archive.h | 7 ++----- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp index 07d7967d9..24b0f504b 100644 --- a/src/core/file_sys/archive_ncch.cpp +++ b/src/core/file_sys/archive_ncch.cpp @@ -76,7 +76,7 @@ ResultVal> NCCHArchive::OpenFile(const Path& path, u64 romfs_size = 0; result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size); - file = std::make_unique(romfs_file, romfs_offset, romfs_size); + file = std::make_unique(std::move(romfs_file), romfs_offset, romfs_size); } else if (filepath_type == NCCHFilePathType::Code || filepath_type == NCCHFilePathType::ExeFS) { std::vector buffer; diff --git a/src/core/file_sys/archive_other_savedata.cpp b/src/core/file_sys/archive_other_savedata.cpp index d3cf080da..1977585b5 100644 --- a/src/core/file_sys/archive_other_savedata.cpp +++ b/src/core/file_sys/archive_other_savedata.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include "core/file_sys/archive_other_savedata.h" #include "core/file_sys/errors.h" #include "core/hle/kernel/process.h" @@ -60,7 +61,7 @@ ResultVal> ParsePathGeneral(const Path& path) { ArchiveFactory_OtherSaveDataPermitted::ArchiveFactory_OtherSaveDataPermitted( std::shared_ptr sd_savedata) - : sd_savedata_source(sd_savedata) {} + : sd_savedata_source(std::move(sd_savedata)) {} ResultVal> ArchiveFactory_OtherSaveDataPermitted::Open( const Path& path) { @@ -98,7 +99,7 @@ ResultVal ArchiveFactory_OtherSaveDataPermitted::GetFormatInf ArchiveFactory_OtherSaveDataGeneral::ArchiveFactory_OtherSaveDataGeneral( std::shared_ptr sd_savedata) - : sd_savedata_source(sd_savedata) {} + : sd_savedata_source(std::move(sd_savedata)) {} ResultVal> ArchiveFactory_OtherSaveDataGeneral::Open( const Path& path) { diff --git a/src/core/file_sys/archive_savedata.cpp b/src/core/file_sys/archive_savedata.cpp index 61f7654f7..d551f590f 100644 --- a/src/core/file_sys/archive_savedata.cpp +++ b/src/core/file_sys/archive_savedata.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "core/file_sys/archive_savedata.h" #include "core/hle/kernel/process.h" @@ -12,7 +13,7 @@ namespace FileSys { ArchiveFactory_SaveData::ArchiveFactory_SaveData( std::shared_ptr sd_savedata) - : sd_savedata_source(sd_savedata) {} + : sd_savedata_source(std::move(sd_savedata)) {} ResultVal> ArchiveFactory_SaveData::Open(const Path& path) { return sd_savedata_source->Open(Kernel::g_current_process->codeset->program_id); diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp index 07dcf010d..7cd09ed07 100644 --- a/src/core/file_sys/ivfc_archive.cpp +++ b/src/core/file_sys/ivfc_archive.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "common/common_types.h" #include "common/logging/log.h" #include "core/file_sys/ivfc_archive.h" @@ -13,6 +14,9 @@ namespace FileSys { +IVFCArchive::IVFCArchive(std::shared_ptr file, u64 offset, u64 size) + : romfs_file(std::move(file)), data_offset(offset), data_size(size) {} + std::string IVFCArchive::GetName() const { return "IVFC"; } @@ -85,6 +89,9 @@ u64 IVFCArchive::GetFreeBytes() const { //////////////////////////////////////////////////////////////////////////////////////////////////// +IVFCFile::IVFCFile(std::shared_ptr file, u64 offset, u64 size) + : romfs_file(std::move(file)), data_offset(offset), data_size(size) {} + ResultVal IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const { LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length); romfs_file->Seek(data_offset + offset, SEEK_SET); diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h index 53a2637a6..768084036 100644 --- a/src/core/file_sys/ivfc_archive.h +++ b/src/core/file_sys/ivfc_archive.h @@ -7,7 +7,6 @@ #include #include #include -#include #include "common/common_types.h" #include "common/file_util.h" #include "core/file_sys/archive_backend.h" @@ -27,8 +26,7 @@ namespace FileSys { */ class IVFCArchive : public ArchiveBackend { public: - IVFCArchive(std::shared_ptr file, u64 offset, u64 size) - : romfs_file(file), data_offset(offset), data_size(size) {} + IVFCArchive(std::shared_ptr file, u64 offset, u64 size); std::string GetName() const override; @@ -52,8 +50,7 @@ protected: class IVFCFile : public FileBackend { public: - IVFCFile(std::shared_ptr file, u64 offset, u64 size) - : romfs_file(file), data_offset(offset), data_size(size) {} + IVFCFile(std::shared_ptr file, u64 offset, u64 size); ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) override; From 11705857cdafbf8c870de9dec1a391691f1df271 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Dec 2017 14:18:31 -0500 Subject: [PATCH 2/2] file_sys: std::move the vector in NCCHFile's constructor Avoids making unnecessary copies of the source vector's data. --- src/core/file_sys/archive_ncch.cpp | 5 ++++- src/core/file_sys/archive_ncch.h | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp index 24b0f504b..2c4ed2d45 100644 --- a/src/core/file_sys/archive_ncch.cpp +++ b/src/core/file_sys/archive_ncch.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "common/common_types.h" #include "common/file_util.h" @@ -83,7 +84,7 @@ ResultVal> NCCHArchive::OpenFile(const Path& path, // Load NCCH .code or icon/banner/logo result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer); - file = std::make_unique(buffer); + file = std::make_unique(std::move(buffer)); } else { LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type); result = Loader::ResultStatus::Error; @@ -193,6 +194,8 @@ u64 NCCHArchive::GetFreeBytes() const { //////////////////////////////////////////////////////////////////////////////////////////////////// +NCCHFile::NCCHFile(std::vector buffer) : file_buffer(std::move(buffer)) {} + ResultVal NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const { LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length); size_t length_left = static_cast(data_size - offset); diff --git a/src/core/file_sys/archive_ncch.h b/src/core/file_sys/archive_ncch.h index 0dfbd4241..f2ba888d8 100644 --- a/src/core/file_sys/archive_ncch.h +++ b/src/core/file_sys/archive_ncch.h @@ -51,7 +51,7 @@ protected: // File backend for NCCH files class NCCHFile : public FileBackend { public: - NCCHFile(std::vector buffer) : file_buffer(buffer) {} + explicit NCCHFile(std::vector buffer); ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) override;