diff options
author | 2024-12-19 17:53:13 -0800 | |
---|---|---|
committer | 2024-12-19 21:27:18 -0800 | |
commit | 2f271fe46646971c66beb6f0ea906a2c771b6a1c (patch) | |
tree | 1468731ad67991c6a2915e96c59752a6f555264c /libs/androidfw/AssetsProvider.cpp | |
parent | 84f0758b9521255a5e55cb7dc1f98b5a009d1c57 (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/AssetsProvider.cpp')
-rw-r--r-- | libs/androidfw/AssetsProvider.cpp | 17 |
1 files changed, 11 insertions, 6 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; } |