Cache resolved theme values
When calling Resources#obtainStyledAttributes, if a value for an
attribute is supplied from the theme, and the value in the theme
is a reference to a resource, the reference will be resolved using
AssetManager2::ResolveReference each time the value from the theme
is selected. This causes Resources#obtainStyledAttributes to do
repeated work every time the same attribute is supplied from the
theme in multiple invocations.
Caching the result of ResolveReference reduces the cost of this
repeated work and reduces the amount of time needed to inflate views.
Before:
com.android.resources.perf (3 Tests)
[1/3] com.android.resources.perf.PerfTest#youtube: PASSED (11.748s)
youtube_ns_median: 95490747
youtube_ns_standardDeviation: 7282249
youtube_ns_mean: 98442515
[2/3] com.android.resources.perf.PerfTest#maps: PASSED (10.862s)
maps_ns_standardDeviation: 4484213
maps_ns_mean: 87912988
maps_ns_median: 86325549
[3/3] com.android.resources.perf.PerfTest#gmail: PASSED (24.034s)
gmail_ns_median: 282175838
gmail_ns_standardDeviation: 6560876
gmail_ns_mean: 282869146
After:
com.android.resources.perf (3 Tests)
[1/3] com.android.resources.perf.PerfTest#youtube: PASSED (11.245s)
youtube_ns_median: 92292347
youtube_ns_standardDeviation: 5899906
youtube_ns_mean: 93045239
[2/3] com.android.resources.perf.PerfTest#maps: PASSED (10.583s)
maps_ns_standardDeviation: 7567929
maps_ns_mean: 81895979
maps_ns_median: 78647883
[3/3] com.android.resources.perf.PerfTest#gmail: PASSED (21.439s)
gmail_ns_median: 229185043
gmail_ns_standardDeviation: 8770133
gmail_ns_mean: 232561234
These tests were done on a Pixel 3 and with cpu settings configured by
libs/hwui/tests/scripts/prep_generic.sh:
Locked CPUs 4,5,6,7 to 1459200 / 2803200 KHz
Disabled CPUs 0,1,2,3
Bug: 170232288
Test: atest ResourcesPerfWorkloads
Change-Id: I1e9e9acaa40fa60475a0e55230e11243f5b69b39
diff --git a/libs/androidfw/AttributeResolution.cpp b/libs/androidfw/AttributeResolution.cpp
index 71919fd..347b4ec 100644
--- a/libs/androidfw/AttributeResolution.cpp
+++ b/libs/androidfw/AttributeResolution.cpp
@@ -39,8 +39,7 @@
: public BackTrackingAttributeFinder<XmlAttributeFinder, size_t> {
public:
explicit XmlAttributeFinder(const ResXMLParser* parser)
- : BackTrackingAttributeFinder(
- 0, parser != nullptr ? parser->getAttributeCount() : 0),
+ : BackTrackingAttributeFinder(0, parser != nullptr ? parser->getAttributeCount() : 0),
parser_(parser) {}
inline uint32_t GetAttribute(size_t index) const {
@@ -178,7 +177,7 @@
value = *attr_value;
DEBUG_LOG("-> From theme: type=0x%x, data=0x%08x", value.type, value.data);
- const auto result = assetmanager->ResolveReference(value);
+ const auto result = assetmanager->ResolveReference(value, true /* cache_value */);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}
@@ -310,7 +309,7 @@
value = *attr_value;
DEBUG_LOG("-> From theme: type=0x%x, data=0x%08x", value.type, value.data);
- auto result = assetmanager->ResolveReference(value);
+ auto result = assetmanager->ResolveReference(value, true /* cache_value */);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}