AssetManager: speed up XML inflation using HashMap caching

This makes XML inflation around 10 times faster. Inflating a layout XML
with one TextView and one ImageView now takes 4 ms instead of 40 ms
This commit is contained in:
Julian Winkler
2023-10-29 08:00:02 +01:00
parent 3bdffe7ce9
commit 9025142bdd

View File

@@ -147,13 +147,18 @@ public final class AssetManager {
}
}
private Map<Integer, EntryGroup> tableBlockCache = new HashMap<>();
private EntryGroup tableBlockSearch(int resId) {
if (tableBlockCache.containsKey(resId)) {
return tableBlockCache.get(resId);
}
EntryGroup ret = null;
for (TableBlock tableBlock : tableBlocks) {
ret = tableBlock.search(resId);
if (ret != null)
break;
}
tableBlockCache.put(resId, ret);
return ret;
}
@@ -913,6 +918,12 @@ public final class AssetManager {
ResXmlPullParser parser = (ResXmlPullParser)set;
outIndices[0] = 0;
Map<Integer,Integer> xmlCache = new HashMap<>();
if (set != null) {
for (int j=0; j<set.getAttributeCount(); j++) {
xmlCache.put(set.getAttributeNameResource(j), j);
}
}
for (int i = 0; i < inAttrs.length; i++) {
int d = i*AssetManager.STYLE_NUM_ENTRIES;
// reset values in case of cached array
@@ -928,13 +939,8 @@ public final class AssetManager {
ValueItem valueItem = null;
while ("attr".equals(entry.getTypeName())) {
valueItem = null;
if (set != null) {
for (int j=0; j<set.getAttributeCount(); j++) {
if (set.getAttributeNameResource(j) == resId) {
valueItem = parser.getResXmlAttributeAt(j);
break;
}
}
if (xmlCache.containsKey(resId)) {
valueItem = parser.getResXmlAttributeAt(xmlCache.get(resId));
}
if (valueItem == null) {
valueItem = defStyle.get(resId);