summaryrefslogtreecommitdiff
path: root/libs/androidfw
diff options
context:
space:
mode:
author Yurii Zubrytskyi <zyy@google.com> 2024-12-19 17:53:13 -0800
committer Yurii Zubrytskyi <zyy@google.com> 2024-12-19 21:27:18 -0800
commit2f271fe46646971c66beb6f0ea906a2c771b6a1c (patch)
tree1468731ad67991c6a2915e96c59752a6f555264c /libs/androidfw
parent84f0758b9521255a5e55cb7dc1f98b5a009d1c57 (diff)
[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
Diffstat (limited to 'libs/androidfw')
-rw-r--r--libs/androidfw/AssetsProvider.cpp17
-rw-r--r--libs/androidfw/include/androidfw/AssetsProvider.h2
2 files changed, 12 insertions, 7 deletions
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 <ziparchive/zip_archive.h>
namespace android {
-namespace {
-constexpr const char* kEmptyDebugString = "<empty>";
-} // namespace
+
+static constexpr std::string_view kEmptyDebugString = "<empty>";
std::unique_ptr<Asset> AssetsProvider::Open(const std::string& path, Asset::AccessMode mode,
bool* file_exists) const {
@@ -356,8 +355,14 @@ MultiAssetsProvider::MultiAssetsProvider(std::unique_ptr<AssetsProvider>&& prima
std::unique_ptr<AssetsProvider> MultiAssetsProvider::Create(
std::unique_ptr<AssetsProvider>&& primary, std::unique_ptr<AssetsProvider>&& 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<MultiAssetsProvider>(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;
}
diff --git a/libs/androidfw/include/androidfw/AssetsProvider.h b/libs/androidfw/include/androidfw/AssetsProvider.h
index 46d7a57ab763..e3b3ae41f7f4 100644
--- a/libs/androidfw/include/androidfw/AssetsProvider.h
+++ b/libs/androidfw/include/androidfw/AssetsProvider.h
@@ -164,7 +164,7 @@ struct DirectoryAssetsProvider : public AssetsProvider {
// `secondary` asset provider if the asset cannot be found in the `primary`.
struct MultiAssetsProvider : public AssetsProvider {
static std::unique_ptr<AssetsProvider> Create(std::unique_ptr<AssetsProvider>&& primary,
- std::unique_ptr<AssetsProvider>&& secondary);
+ std::unique_ptr<AssetsProvider>&& secondary = {});
bool ForEachFile(const std::string& root_path,
base::function_ref<void(StringPiece, FileType)> f) const override;