From b07595d1941a25b5f82c62f9b7099c3c77d06056 Mon Sep 17 00:00:00 2001 From: Sergio Giro Date: Fri, 13 May 2016 16:34:46 +0100 Subject: [PATCH] ZygoteInit: warm up JCA providers during preload This makes the time spent in the first call of an app to SSLSocketFactory.getDefault() drop from ~240 ms to ~50 ms. In M it was around ~6ms. This is due to the fact that, while instantiating the default factory, all providers are initialized. In order to obtain the timings above, I created an app calling SSLSocketFactory.getDefault in onCreate and timed it with System.currentTimeMillis() . (cherry picked from commit 6cb7b1c4765e9bc5175056826523dbd88426e9aa) Bug: 28545496 Change-Id: Ic5aab3ece609d9fef06fee4ccb83d8371af075b2 --- .../com/android/internal/os/ZygoteInit.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java index d84cb8065f95e..1440e5fb559de 100644 --- a/core/java/com/android/internal/os/ZygoteInit.java +++ b/core/java/com/android/internal/os/ZygoteInit.java @@ -52,6 +52,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.security.Security; +import java.security.Provider; import java.util.ArrayList; /** @@ -193,6 +195,7 @@ public class ZygoteInit { // Ask the WebViewFactory to do any initialization that must run in the zygote process, // for memory sharing purposes. WebViewFactory.prepareWebViewInZygote(); + warmUpJcaProviders(); Log.d(TAG, "end preload"); } @@ -213,6 +216,24 @@ public class ZygoteInit { Hyphenator.init(); } + /** + * Warm up the providers that are already registered. + * + * By doing it here we avoid that each app does it when requesting a service from the + * provider for the first time. + */ + private static void warmUpJcaProviders() { + long startTime = SystemClock.uptimeMillis(); + Trace.traceBegin( + Trace.TRACE_TAG_DALVIK, "Starting warm up of JCA providers"); + for (Provider p : Security.getProviders()) { + p.warmUpServiceProvision(); + } + Log.i(TAG, "Warmed up JCA providers in " + + (SystemClock.uptimeMillis()-startTime) + "ms."); + Trace.traceEnd(Trace.TRACE_TAG_DALVIK); + } + /** * Performs Zygote process initialization. Loads and initializes * commonly used classes.