From 2f271fe46646971c66beb6f0ea906a2c771b6a1c Mon Sep 17 00:00:00 2001 From: Yurii Zubrytskyi Date: Thu, 19 Dec 2024 17:53:13 -0800 Subject: [res] Don't create extra asset provider when not needed MultiAssetsProvider is useful to combine a ResourcesLoader with a file-based provider, but most of the time it's used without any loaders, and ends up with an EmptyAssetsProvider object that just wastes cycles and RAM. This CL optimizes that out, only creating the extra layer when we have a real loader, and falling back to the only remaining provider otherwise Bug: 319137634 Test: build + boot + manual + atest libandroidfw_tests Flag: EXEMPT optimization Change-Id: Ibc5ac60adc5e008b70a62481a747758c89083ff9 --- libs/androidfw/AssetsProvider.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'libs/androidfw/AssetsProvider.cpp') diff --git a/libs/androidfw/AssetsProvider.cpp b/libs/androidfw/AssetsProvider.cpp index 59c6aba7b8b0..11b12eb030a6 100644 --- a/libs/androidfw/AssetsProvider.cpp +++ b/libs/androidfw/AssetsProvider.cpp @@ -24,9 +24,8 @@ #include namespace android { -namespace { -constexpr const char* kEmptyDebugString = ""; -} // namespace + +static constexpr std::string_view kEmptyDebugString = ""; std::unique_ptr AssetsProvider::Open(const std::string& path, Asset::AccessMode mode, bool* file_exists) const { @@ -356,8 +355,14 @@ MultiAssetsProvider::MultiAssetsProvider(std::unique_ptr&& prima std::unique_ptr MultiAssetsProvider::Create( std::unique_ptr&& primary, std::unique_ptr&& secondary) { - if (primary == nullptr || secondary == nullptr) { - return nullptr; + if (primary == nullptr && secondary == nullptr) { + return EmptyAssetsProvider::Create(); + } + if (!primary) { + return secondary; + } + if (!secondary) { + return primary; } return std::unique_ptr(new MultiAssetsProvider(std::move(primary), std::move(secondary))); @@ -425,7 +430,7 @@ const std::string& EmptyAssetsProvider::GetDebugName() const { if (path_.has_value()) { return *path_; } - const static std::string kEmpty = kEmptyDebugString; + constexpr static std::string kEmpty{kEmptyDebugString}; return kEmpty; } -- cgit v1.2.3-59-g8ed1b