【问题标题】:Does inflate cache xml layouts?是否膨胀缓存 xml 布局?
【发布时间】:2017-07-17 07:19:15
【问题描述】:

当我们膨胀一个布局时,它会被缓存吗?如果我没记错的话,我已经读过 inflate 缓存 XML 以提高性能。那是对的吗?到底缓存了什么?

【问题讨论】:

  • 我不这么认为
  • @MD:所以每次我们膨胀时它都会从 FS 重新读取 XML 文件?
  • 如果任何缓存完成,它是在系统级别(文件系统缓存),而不是应用级别
  • 可以缓存Holder中的所有视图。不是 XML
  • @MD:我不是在谈论列表和持有者模式。我认为inflate有一些优化

标签: android performance android-xml android-inflate


【解决方案1】:

android.content.res 下的ResourcesImpl 类中,我得到了一些线索,即在从XML 膨胀视图时存在内部缓存机制。在inflater.inflate() 中有一个XmlResourceParser 对象负责解析XML。这个对象是从ResourcesImpl 类中一个名为loadXmlResourceParser 的方法获得的。方法是-

 /**
 * 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));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多