From 1a7ba99687f648729073492e3af503d57742fbcf Mon Sep 17 00:00:00 2001 From: Chris Li Date: Mon, 13 Feb 2023 15:56:21 +0800 Subject: [PATCH] Preload WindowManager Extensions to improve startup performance WindowManager Extensions is an optional shared library that is required for WindowManager Jetpack to fully function. Add it to the class loaders so that it can improve the apps startup performance. Bug: 269966257 Bug: 266268883 Test: Build and measure the system tracing Change-Id: I496bb04a06fcd78d4bf246c13528e508c7e0025a --- core/java/android/app/ApplicationLoaders.java | 8 ++-- .../com/android/internal/os/ZygoteInit.java | 45 +++++++++++++------ .../android/app/ApplicationLoadersTest.java | 30 +++++++------ 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/core/java/android/app/ApplicationLoaders.java b/core/java/android/app/ApplicationLoaders.java index 53b16d3a81706..9cd99dca1ab1f 100644 --- a/core/java/android/app/ApplicationLoaders.java +++ b/core/java/android/app/ApplicationLoaders.java @@ -157,15 +157,15 @@ public class ApplicationLoaders { * All libraries in the closure of libraries to be loaded must be in libs. A library can * only depend on libraries that come before it in the list. */ - public void createAndCacheNonBootclasspathSystemClassLoaders(SharedLibraryInfo[] libs) { + public void createAndCacheNonBootclasspathSystemClassLoaders(List libs) { if (mSystemLibsCacheMap != null) { throw new IllegalStateException("Already cached."); } - mSystemLibsCacheMap = new HashMap(); + mSystemLibsCacheMap = new HashMap<>(); - for (SharedLibraryInfo lib : libs) { - createAndCacheNonBootclasspathSystemClassLoader(lib); + for (int i = 0; i < libs.size(); i++) { + createAndCacheNonBootclasspathSystemClassLoader(libs.get(i)); } } diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java index 1505ccce97a12..6e8d98c83b40e 100644 --- a/core/java/com/android/internal/os/ZygoteInit.java +++ b/core/java/com/android/internal/os/ZygoteInit.java @@ -51,6 +51,7 @@ import android.util.EventLog; import android.util.Log; import android.util.Slog; import android.util.TimingsTraceLog; +import android.view.WindowManager; import android.webkit.WebViewFactory; import android.widget.TextView; @@ -72,6 +73,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.security.Provider; import java.security.Security; +import java.util.ArrayList; +import java.util.List; /** * Startup class for the zygote process. @@ -384,33 +387,49 @@ public class ZygoteInit { * classpath. */ private static void cacheNonBootClasspathClassLoaders() { + // Ordered dependencies first + final List libs = new ArrayList<>(); // These libraries used to be part of the bootclasspath, but had to be removed. // Old system applications still get them for backwards compatibility reasons, // so they are cached here in order to preserve performance characteristics. - SharedLibraryInfo hidlBase = new SharedLibraryInfo( + libs.add(new SharedLibraryInfo( "/system/framework/android.hidl.base-V1.0-java.jar", null /*packageName*/, null /*codePaths*/, null /*name*/, 0 /*version*/, SharedLibraryInfo.TYPE_BUILTIN, null /*declaringPackage*/, null /*dependentPackages*/, null /*dependencies*/, - false /*isNative*/); - SharedLibraryInfo hidlManager = new SharedLibraryInfo( + false /*isNative*/)); + libs.add(new SharedLibraryInfo( "/system/framework/android.hidl.manager-V1.0-java.jar", null /*packageName*/, null /*codePaths*/, null /*name*/, 0 /*version*/, SharedLibraryInfo.TYPE_BUILTIN, null /*declaringPackage*/, null /*dependentPackages*/, null /*dependencies*/, - false /*isNative*/); + false /*isNative*/)); - SharedLibraryInfo androidTestBase = new SharedLibraryInfo( + libs.add(new SharedLibraryInfo( "/system/framework/android.test.base.jar", null /*packageName*/, null /*codePaths*/, null /*name*/, 0 /*version*/, SharedLibraryInfo.TYPE_BUILTIN, null /*declaringPackage*/, null /*dependentPackages*/, null /*dependencies*/, - false /*isNative*/); + false /*isNative*/)); - ApplicationLoaders.getDefault().createAndCacheNonBootclasspathSystemClassLoaders( - new SharedLibraryInfo[]{ - // ordered dependencies first - hidlBase, - hidlManager, - androidTestBase, - }); + // WindowManager Extensions is an optional shared library that is required for WindowManager + // Jetpack to fully function. Since it is a widely used library, preload it to improve apps + // startup performance. + if (WindowManager.hasWindowExtensionsEnabled()) { + final String systemExtFrameworkPath = + new File(Environment.getSystemExtDirectory(), "framework").getPath(); + libs.add(new SharedLibraryInfo( + systemExtFrameworkPath + "/androidx.window.extensions.jar", + "androidx.window.extensions", null /*codePaths*/, + "androidx.window.extensions", SharedLibraryInfo.VERSION_UNDEFINED, + SharedLibraryInfo.TYPE_BUILTIN, null /*declaringPackage*/, + null /*dependentPackages*/, null /*dependencies*/, false /*isNative*/)); + libs.add(new SharedLibraryInfo( + systemExtFrameworkPath + "/androidx.window.sidecar.jar", + "androidx.window.sidecar", null /*codePaths*/, + "androidx.window.sidecar", SharedLibraryInfo.VERSION_UNDEFINED, + SharedLibraryInfo.TYPE_BUILTIN, null /*declaringPackage*/, + null /*dependentPackages*/, null /*dependencies*/, false /*isNative*/)); + } + + ApplicationLoaders.getDefault().createAndCacheNonBootclasspathSystemClassLoaders(libs); } /** diff --git a/core/tests/coretests/src/android/app/ApplicationLoadersTest.java b/core/tests/coretests/src/android/app/ApplicationLoadersTest.java index 19e7f80dfa5b0..3cb62b97647bf 100644 --- a/core/tests/coretests/src/android/app/ApplicationLoadersTest.java +++ b/core/tests/coretests/src/android/app/ApplicationLoadersTest.java @@ -16,14 +16,17 @@ package android.app; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import android.content.pm.SharedLibraryInfo; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; +import com.google.android.collect.Lists; + import org.junit.Test; import org.junit.runner.RunWith; @@ -48,7 +51,7 @@ public class ApplicationLoadersTest { @Test public void testGetNonExistantLib() { ApplicationLoaders loaders = new ApplicationLoaders(); - assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( + assertNull(loaders.getCachedNonBootclasspathSystemLib( "/system/framework/nonexistantlib.jar", null, null, null)); } @@ -57,9 +60,9 @@ public class ApplicationLoadersTest { ApplicationLoaders loaders = new ApplicationLoaders(); SharedLibraryInfo libA = createLib(LIB_A); - loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); + loaders.createAndCacheNonBootclasspathSystemClassLoaders(Lists.newArrayList(libA)); - assertNotEquals(null, loaders.getCachedNonBootclasspathSystemLib( + assertNotNull(loaders.getCachedNonBootclasspathSystemLib( LIB_A, null, null, null)); } @@ -71,9 +74,9 @@ public class ApplicationLoadersTest { ClassLoader parent = ClassLoader.getSystemClassLoader(); assertNotEquals(null, parent); - loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); + loaders.createAndCacheNonBootclasspathSystemClassLoaders(Lists.newArrayList(libA)); - assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( + assertNull(loaders.getCachedNonBootclasspathSystemLib( LIB_A, parent, null, null)); } @@ -82,9 +85,9 @@ public class ApplicationLoadersTest { ApplicationLoaders loaders = new ApplicationLoaders(); SharedLibraryInfo libA = createLib(LIB_A); - loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); + loaders.createAndCacheNonBootclasspathSystemClassLoaders(Lists.newArrayList(libA)); - assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( + assertNull(loaders.getCachedNonBootclasspathSystemLib( LIB_A, null, "other classloader", null)); } @@ -98,9 +101,9 @@ public class ApplicationLoadersTest { ArrayList sharedLibraries = new ArrayList<>(); sharedLibraries.add(dep); - loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); + loaders.createAndCacheNonBootclasspathSystemClassLoaders(Lists.newArrayList(libA)); - assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( + assertNull(loaders.getCachedNonBootclasspathSystemLib( LIB_A, null, null, sharedLibraries)); } @@ -112,7 +115,7 @@ public class ApplicationLoadersTest { libB.addDependency(libA); loaders.createAndCacheNonBootclasspathSystemClassLoaders( - new SharedLibraryInfo[]{libA, libB}); + Lists.newArrayList(libA, libB)); ClassLoader loadA = loaders.getCachedNonBootclasspathSystemLib( LIB_A, null, null, null); @@ -121,7 +124,7 @@ public class ApplicationLoadersTest { ArrayList sharedLibraries = new ArrayList<>(); sharedLibraries.add(loadA); - assertNotEquals(null, loaders.getCachedNonBootclasspathSystemLib( + assertNotNull(loaders.getCachedNonBootclasspathSystemLib( LIB_DEP_A, null, null, sharedLibraries)); } @@ -132,7 +135,6 @@ public class ApplicationLoadersTest { SharedLibraryInfo libB = createLib(LIB_DEP_A); libB.addDependency(libA); - loaders.createAndCacheNonBootclasspathSystemClassLoaders( - new SharedLibraryInfo[]{libB, libA}); + loaders.createAndCacheNonBootclasspathSystemClassLoaders(Lists.newArrayList(libB, libA)); } }