Move IO from under the ResourcesManager lock

ApkAssets.isUpToDate() performs IO for many types of assets,
and doing it under a highly contended lock may cause visible
jank.

Given that we don't have to remove the key right at the time of
the lookup, it's fine to move the up-to-date check out of the
lock - if it's not, the function would replace the outdated
asset anyway

Bug: 259941466
Test: build + boot
Change-Id: I8828deadc82180e93bbd9fc34f80a7bd9411d4ad
This commit is contained in:
Yurii Zubrytskyi
2022-12-01 00:34:09 -08:00
parent 3d0a6c17f4
commit 4b60d29375

View File

@@ -305,9 +305,13 @@ public class ResourcesManager {
for (int i = mCachedApkAssets.size() - 1; i >= 0; i--) {
final ApkKey key = mCachedApkAssets.keyAt(i);
if (key.path.equals(path)) {
WeakReference<ApkAssets> apkAssetsRef = mCachedApkAssets.removeAt(i);
if (apkAssetsRef != null && apkAssetsRef.get() != null) {
apkAssetsRef.get().close();
final WeakReference<ApkAssets> apkAssetsRef = mCachedApkAssets.removeAt(i);
if (apkAssetsRef == null) {
continue;
}
final ApkAssets apkAssets = apkAssetsRef.get();
if (apkAssets != null) {
apkAssets.close();
}
}
}
@@ -446,16 +450,14 @@ public class ResourcesManager {
ApkAssets apkAssets;
// Optimistically check if this ApkAssets exists somewhere else.
final WeakReference<ApkAssets> apkAssetsRef;
synchronized (mLock) {
final WeakReference<ApkAssets> apkAssetsRef = mCachedApkAssets.get(key);
if (apkAssetsRef != null) {
apkAssets = apkAssetsRef.get();
if (apkAssets != null && apkAssets.isUpToDate()) {
return apkAssets;
} else {
// Clean up the reference.
mCachedApkAssets.remove(key);
}
apkAssetsRef = mCachedApkAssets.get(key);
}
if (apkAssetsRef != null) {
apkAssets = apkAssetsRef.get();
if (apkAssets != null && apkAssets.isUpToDate()) {
return apkAssets;
}
}