When we inflate a layout does it get cached? If I recall correctly I have read that the inflate caches XML for performance. Is that correct? What exactly is being cached?
Asked
Active
Viewed 407 times
1
-
I don't think so – M D Jul 17 '17 at 07:21
-
@MD: So it re-reads the XML file from the FS each time we inflate? – Jim Jul 17 '17 at 07:24
-
if any caching is done, it is on system level (file system cache), not app level – pskink Jul 17 '17 at 07:28
-
You can cache all the views inside Holder. Not an XML – M D Jul 17 '17 at 07:28
-
@MD: I am not talking about lists and the holder pattern. I thought that there was some optimization for inflate – Jim Jul 17 '17 at 07:42
-
The optimization applied is some sort of compression and binarization of the xmls for faster parsing. But i don't think there is caching by default. – WindRider Sep 18 '17 at 18:47
1 Answers
1
From ResourcesImpl
class under android.content.res
, I got some clues that there is an internal caching mechanism while inflating a view from XML.In inflater.inflate()
there is an XmlResourceParser
object which is responsible for parsing XML. And this object is got from a method named loadXmlResourceParser
in the ResourcesImpl
class. The method is -
/**
* Loads an XML parser for the specified file.
*
* @param file the path for the XML file to parse
* @param id the resource identifier for the file
* @param assetCookie the asset cookie for the file
* @param type the type of resource (used for logging)
* @return a parser for the specified XML file
* @throws NotFoundException if the file could not be loaded
*/
@NonNull
XmlResourceParser loadXmlResourceParser(@NonNull String file, @AnyRes int id, int assetCookie,
@NonNull String type)
throws NotFoundException {
if (id != 0) {
try {
synchronized (mCachedXmlBlocks) {
final int[] cachedXmlBlockCookies = mCachedXmlBlockCookies;
final String[] cachedXmlBlockFiles = mCachedXmlBlockFiles;
final XmlBlock[] cachedXmlBlocks = mCachedXmlBlocks;
// First see if this block is in our cache.
final int num = cachedXmlBlockFiles.length;
for (int i = 0; i < num; i++) {
if (cachedXmlBlockCookies[i] == assetCookie && cachedXmlBlockFiles[i] != null
&& cachedXmlBlockFiles[i].equals(file)) {
return cachedXmlBlocks[i].newParser();
}
}
// Not in the cache, create a new block and put it at
// the next slot in the cache. (EVIDENCE FOR CACHING)
final XmlBlock block = mAssets.openXmlBlockAsset(assetCookie, file);
if (block != null) {
final int pos = (mLastCachedXmlBlockIndex + 1) % num;
mLastCachedXmlBlockIndex = pos;
final XmlBlock oldBlock = cachedXmlBlocks[pos];
if (oldBlock != null) {
oldBlock.close();
}
cachedXmlBlockCookies[pos] = assetCookie;
cachedXmlBlockFiles[pos] = file;
cachedXmlBlocks[pos] = block;
return block.newParser();
}
}
} catch (Exception e) {
final NotFoundException rnf = new NotFoundException("File " + file
+ " from xml type " + type + " resource ID #0x" + Integer.toHexString(id));
rnf.initCause(e);
throw rnf;
}
}
throw new NotFoundException("File " + file + " from xml type " + type + " resource ID #0x"
+ Integer.toHexString(id));
}

Gk Mohammad Emon
- 6,084
- 3
- 42
- 42