The multiwindow model and Resources-per-activity model that came in N puts greater demands on AssetManagers. They are created whenever window dimensions change, which can be frequently. There is a need to be able to cheaply create a new AssetManager for each Activity, which shares a lot of underlying state. In order to make the creation of AssetManagers cheap, we need a new implementation of the native AssetManager and ResTable to support immutable representations of APKs. This new data structure/class is ApkAssets. ApkAssets have the same functionality of an AssetManager, except that they operate on a single APK, and they do not do any caching. Once loaded, they are immutable. ApkAssets will be exposed as a Java object, with its implementation in native code. The existing Java StringBlock will be owned by ApkAssets, which means that Strings can be shared across AssetManagers. ApkAssets can be cached by the ResourcesManager. Creating an AssetManager requires only a list of ApkAssets and a configuration. AssetManager2 (named with the suffix '2' for now while transitioning to the new implementation) caches bags that are accessed. Since ApkAssets are expected to be kept around longer, they do more validation of the resource table, which cause slower load times. Measured on an angler-userdebug, loading the framework assets takes 11ms with ApkAssets, and 2ms with the old AssetManager implementation. The tradeoff is that there does not need to be any security checks once an ApkAssets is loaded, and regular resource retrieval is faster. Measured on an angler-userdebug, accessing resource (android:string/ok) with many locales takes 18us with AssetManager2, and 19us with AssetManager (this is per resource, so these add up). Test: make libandroidfw_tests Change-Id: Id0e57ee828f17008891fe3741935a9be8830b01d
229 lines
7.1 KiB
C++
229 lines
7.1 KiB
C++
/*
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
#include "androidfw/ApkAssets.h"
|
|
#include "androidfw/AssetManager.h"
|
|
#include "androidfw/AssetManager2.h"
|
|
#include "androidfw/ResourceTypes.h"
|
|
|
|
#include "TestHelpers.h"
|
|
#include "data/basic/R.h"
|
|
#include "data/styles/R.h"
|
|
|
|
namespace basic = com::android::basic;
|
|
namespace app = com::android::app;
|
|
|
|
namespace android {
|
|
|
|
constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk";
|
|
|
|
static void BM_AssetManagerLoadAssets(benchmark::State& state) {
|
|
std::string path = GetTestDataPath() + "/basic/basic.apk";
|
|
while (state.KeepRunning()) {
|
|
std::unique_ptr<ApkAssets> apk = ApkAssets::Load(path);
|
|
AssetManager2 assets;
|
|
assets.SetApkAssets({apk.get()});
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerLoadAssets);
|
|
|
|
static void BM_AssetManagerLoadAssetsOld(benchmark::State& state) {
|
|
String8 path((GetTestDataPath() + "/basic/basic.apk").data());
|
|
while (state.KeepRunning()) {
|
|
AssetManager assets;
|
|
assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
|
|
false /* isSystemAsset */);
|
|
|
|
// Force creation.
|
|
assets.getResources(true);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerLoadAssetsOld);
|
|
|
|
static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) {
|
|
std::string path = kFrameworkPath;
|
|
while (state.KeepRunning()) {
|
|
std::unique_ptr<ApkAssets> apk = ApkAssets::Load(path);
|
|
AssetManager2 assets;
|
|
assets.SetApkAssets({apk.get()});
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerLoadFrameworkAssets);
|
|
|
|
static void BM_AssetManagerLoadFrameworkAssetsOld(benchmark::State& state) {
|
|
String8 path(kFrameworkPath);
|
|
while (state.KeepRunning()) {
|
|
AssetManager assets;
|
|
assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
|
|
false /* isSystemAsset */);
|
|
|
|
// Force creation.
|
|
assets.getResources(true);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerLoadFrameworkAssetsOld);
|
|
|
|
static void BM_AssetManagerGetResource(benchmark::State& state) {
|
|
std::unique_ptr<ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
|
|
if (apk == nullptr) {
|
|
state.SkipWithError("Failed to load assets");
|
|
return;
|
|
}
|
|
|
|
AssetManager2 assets;
|
|
assets.SetApkAssets({apk.get()});
|
|
|
|
Res_value value;
|
|
ResTable_config selected_config;
|
|
uint32_t flags;
|
|
|
|
while (state.KeepRunning()) {
|
|
assets.GetResource(basic::R::integer::number1, false /* may_be_bag */,
|
|
0u /* density_override */, &value, &selected_config, &flags);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerGetResource);
|
|
|
|
static void BM_AssetManagerGetResourceOld(benchmark::State& state) {
|
|
AssetManager assets;
|
|
if (!assets.addAssetPath(String8((GetTestDataPath() + "/basic/basic.apk").data()),
|
|
nullptr /* cookie */, false /* appAsLib */,
|
|
false /* isSystemAssets */)) {
|
|
state.SkipWithError("Failed to load assets");
|
|
return;
|
|
}
|
|
|
|
const ResTable& table = assets.getResources(true);
|
|
|
|
Res_value value;
|
|
ResTable_config selected_config;
|
|
uint32_t flags;
|
|
|
|
while (state.KeepRunning()) {
|
|
table.getResource(basic::R::integer::number1, &value, false /* may_be_bag */,
|
|
0u /* density_override */, &flags, &selected_config);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerGetResourceOld);
|
|
|
|
constexpr static const uint32_t kStringOkId = 0x0104000au;
|
|
|
|
static void BM_AssetManagerGetResourceFrameworkLocale(benchmark::State& state) {
|
|
std::unique_ptr<ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
|
|
if (apk == nullptr) {
|
|
state.SkipWithError("Failed to load assets");
|
|
return;
|
|
}
|
|
|
|
AssetManager2 assets;
|
|
assets.SetApkAssets({apk.get()});
|
|
|
|
ResTable_config config;
|
|
memset(&config, 0, sizeof(config));
|
|
memcpy(config.language, "fr", 2);
|
|
assets.SetConfiguration(config);
|
|
|
|
Res_value value;
|
|
ResTable_config selected_config;
|
|
uint32_t flags;
|
|
|
|
while (state.KeepRunning()) {
|
|
assets.GetResource(kStringOkId, false /* may_be_bag */, 0u /* density_override */, &value,
|
|
&selected_config, &flags);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerGetResourceFrameworkLocale);
|
|
|
|
static void BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State& state) {
|
|
AssetManager assets;
|
|
if (!assets.addAssetPath(String8((GetTestDataPath() + "/basic/basic.apk").data()),
|
|
nullptr /* cookie */, false /* appAsLib */,
|
|
false /* isSystemAssets */)) {
|
|
state.SkipWithError("Failed to load assets");
|
|
return;
|
|
}
|
|
|
|
ResTable_config config;
|
|
memset(&config, 0, sizeof(config));
|
|
memcpy(config.language, "fr", 2);
|
|
assets.setConfiguration(config, nullptr);
|
|
|
|
const ResTable& table = assets.getResources(true);
|
|
|
|
Res_value value;
|
|
ResTable_config selected_config;
|
|
uint32_t flags;
|
|
|
|
while (state.KeepRunning()) {
|
|
table.getResource(kStringOkId, &value, false /* may_be_bag */, 0u /* density_override */,
|
|
&flags, &selected_config);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerGetResourceFrameworkLocaleOld);
|
|
|
|
static void BM_AssetManagerGetBag(benchmark::State& state) {
|
|
std::unique_ptr<ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
|
|
if (apk == nullptr) {
|
|
state.SkipWithError("Failed to load assets");
|
|
return;
|
|
}
|
|
|
|
AssetManager2 assets;
|
|
assets.SetApkAssets({apk.get()});
|
|
|
|
while (state.KeepRunning()) {
|
|
const ResolvedBag* bag = assets.GetBag(app::R::style::StyleTwo);
|
|
const auto bag_end = end(bag);
|
|
for (auto iter = begin(bag); iter != bag_end; ++iter) {
|
|
uint32_t key = iter->key;
|
|
Res_value value = iter->value;
|
|
benchmark::DoNotOptimize(key);
|
|
benchmark::DoNotOptimize(value);
|
|
}
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerGetBag);
|
|
|
|
static void BM_AssetManagerGetBagOld(benchmark::State& state) {
|
|
AssetManager assets;
|
|
if (!assets.addAssetPath(String8((GetTestDataPath() + "/styles/styles.apk").data()),
|
|
nullptr /* cookie */, false /* appAsLib */,
|
|
false /* isSystemAssets */)) {
|
|
state.SkipWithError("Failed to load assets");
|
|
return;
|
|
}
|
|
|
|
const ResTable& table = assets.getResources(true);
|
|
|
|
while (state.KeepRunning()) {
|
|
const ResTable::bag_entry* bag_begin;
|
|
const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin);
|
|
const ResTable::bag_entry* const bag_end = bag_begin + N;
|
|
for (auto iter = bag_begin; iter != bag_end; ++iter) {
|
|
uint32_t key = iter->map.name.ident;
|
|
Res_value value = iter->map.value;
|
|
benchmark::DoNotOptimize(key);
|
|
benchmark::DoNotOptimize(value);
|
|
}
|
|
table.unlockBag(bag_begin);
|
|
}
|
|
}
|
|
BENCHMARK(BM_AssetManagerGetBagOld);
|
|
|
|
} // namespace android
|