From c8c410565927b54b2a1353ca00a1c4de38a74816 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 19 Sep 2018 13:54:43 -0400 Subject: nax: Avoid unnecessary calls to AsNCA() in IdentifyType() AsNCA() allocates an NCA instance every time it's called. In the current manner it's used, it's quite inefficient as it's making a redundant allocation. We can just amend the order of the conditionals to make it easier to just call it once. --- src/core/loader/nax.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/core/loader/nax.cpp') diff --git a/src/core/loader/nax.cpp b/src/core/loader/nax.cpp index b46d81c02..02a0d5ba7 100644 --- a/src/core/loader/nax.cpp +++ b/src/core/loader/nax.cpp @@ -21,12 +21,16 @@ AppLoader_NAX::~AppLoader_NAX() = default; FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) { FileSys::NAX nax(file); - if (nax.GetStatus() == ResultStatus::Success && nax.AsNCA() != nullptr && - nax.AsNCA()->GetStatus() == ResultStatus::Success) { - return FileType::NAX; + if (nax.GetStatus() != ResultStatus::Success) { + return FileType::Error; } - return FileType::Error; + const auto nca = nax.AsNCA(); + if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) { + return FileType::Error; + } + + return FileType::NAX; } ResultStatus AppLoader_NAX::Load(Kernel::SharedPtr& process) { -- cgit v1.2.3 From 45195a51a76b3000e028234f619a4d15bff443eb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 19 Sep 2018 14:13:00 -0400 Subject: nax: Avoid re-parsing NAX data with GetFileType() An instance of the NAX apploader already has an existing NAX instance in memory. Calling directly into IdentifyType() directly would re-parse the whole file again into yet another NAX instance, only to toss it away again. This gets rid of unnecessary/redundant file parsing and allocations. --- src/core/loader/nax.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'src/core/loader/nax.cpp') diff --git a/src/core/loader/nax.cpp b/src/core/loader/nax.cpp index 02a0d5ba7..5d4380684 100644 --- a/src/core/loader/nax.cpp +++ b/src/core/loader/nax.cpp @@ -11,16 +11,8 @@ #include "core/loader/nca.h" namespace Loader { - -AppLoader_NAX::AppLoader_NAX(FileSys::VirtualFile file) - : AppLoader(file), nax(std::make_unique(file)), - nca_loader(std::make_unique(nax->GetDecrypted())) {} - -AppLoader_NAX::~AppLoader_NAX() = default; - -FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) { - FileSys::NAX nax(file); - +namespace { +FileType IdentifyTypeImpl(const FileSys::NAX& nax) { if (nax.GetStatus() != ResultStatus::Success) { return FileType::Error; } @@ -32,6 +24,22 @@ FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) { return FileType::NAX; } +} // Anonymous namespace + +AppLoader_NAX::AppLoader_NAX(FileSys::VirtualFile file) + : AppLoader(file), nax(std::make_unique(file)), + nca_loader(std::make_unique(nax->GetDecrypted())) {} + +AppLoader_NAX::~AppLoader_NAX() = default; + +FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) { + const FileSys::NAX nax(file); + return IdentifyTypeImpl(nax); +} + +FileType AppLoader_NAX::GetFileType() { + return IdentifyTypeImpl(*nax); +} ResultStatus AppLoader_NAX::Load(Kernel::SharedPtr& process) { if (is_loaded) { -- cgit v1.2.3