Minor optimisations to InstallSource.

Memoize the common case used for most system apps.
Don't intern() a string when we know it already has been.

Bug: 134746019
Test: atest CtsPackageInstallTestCases PackageManagerTests
Change-Id: I191b3b994c84cfa948f2cdc9f23d18b1b9b8f834
This commit is contained in:
Alan Stokes
2019-11-07 13:47:57 +00:00
parent 5ed8537b66
commit 5bcd3fa6cc

View File

@@ -31,6 +31,9 @@ final class InstallSource {
*/
static final InstallSource EMPTY = new InstallSource(null, null, null, false);
/** We also memoize this case because it is common - all un-updated system apps. */
private static final InstallSource EMPTY_ORPHANED = new InstallSource(null, null, null, true);
/** The package that requested the installation, if known. */
@Nullable
final String initiatingPackageName;
@@ -57,18 +60,24 @@ final class InstallSource {
static InstallSource create(@Nullable String initiatingPackageName,
@Nullable String originatingPackageName, @Nullable String installerPackageName,
boolean isOrphaned) {
if (initiatingPackageName == null && originatingPackageName == null
&& installerPackageName == null && !isOrphaned) {
return EMPTY;
}
return new InstallSource(
initiatingPackageName == null ? null : initiatingPackageName.intern(),
originatingPackageName == null ? null : originatingPackageName.intern(),
installerPackageName == null ? null : installerPackageName.intern(),
return createInternal(
intern(initiatingPackageName),
intern(originatingPackageName),
intern(installerPackageName),
isOrphaned);
}
private static InstallSource createInternal(@Nullable String initiatingPackageName,
@Nullable String originatingPackageName, @Nullable String installerPackageName,
boolean isOrphaned) {
if (initiatingPackageName == null && originatingPackageName == null
&& installerPackageName == null) {
return isOrphaned ? EMPTY_ORPHANED : EMPTY;
}
return new InstallSource(initiatingPackageName, originatingPackageName,
installerPackageName, isOrphaned);
}
private InstallSource(@Nullable String initiatingPackageName,
@Nullable String originatingPackageName, @Nullable String installerPackageName,
boolean isOrphaned) {
@@ -85,8 +94,8 @@ final class InstallSource {
if (Objects.equals(installerPackageName, this.installerPackageName)) {
return this;
}
return create(initiatingPackageName, originatingPackageName, installerPackageName,
isOrphaned);
return createInternal(initiatingPackageName, originatingPackageName,
intern(installerPackageName), isOrphaned);
}
/**
@@ -96,7 +105,7 @@ final class InstallSource {
if (isOrphaned == this.isOrphaned) {
return this;
}
return create(initiatingPackageName, originatingPackageName, installerPackageName,
return createInternal(initiatingPackageName, originatingPackageName, installerPackageName,
isOrphaned);
}
@@ -132,7 +141,12 @@ final class InstallSource {
if (!modified) {
return this;
}
return create(initiatingPackageName, originatingPackageName, installerPackageName,
return createInternal(initiatingPackageName, originatingPackageName, installerPackageName,
isOrphaned);
}
@Nullable
private static String intern(@Nullable String packageName) {
return packageName == null ? null : packageName.intern();
}
}