diff options
author | Andy McFadden <fadden@android.com> | 2009-07-10 16:15:23 -0700 |
---|---|---|
committer | Andy McFadden <fadden@android.com> | 2009-07-10 16:15:23 -0700 |
commit | 0fb831a410412728d6ee79ff98c465233b0803d0 (patch) | |
tree | fc28a878a32ae655b0e48e1fbc67de271d713672 /dalvik/src | |
parent | 12037fdf53f4ac46f3e12067b3ae8b96f8967974 (diff) | |
download | libcore-0fb831a410412728d6ee79ff98c465233b0803d0.zip libcore-0fb831a410412728d6ee79ff98c465233b0803d0.tar.gz libcore-0fb831a410412728d6ee79ff98c465233b0803d0.tar.bz2 |
Minor class loading optimization.
In internal bug 1836311, the String.replace() in PathLoader.findClass
was identifed by traceview as 6.6% of startup time for an app. This
adds a (hidden) alternative that takes a "binary name" like the
functions in ClassLoader, and we do the slash-to-dot conversion inside
the VM as we convert it to a descriptor. (This is really how it should
have been done in the first place, but now it's part of the visible API
and engraved in stone.)
The original function now does a slash-to-dot conversion and calls the
new one.
(We may want to un-hide the method for the benefit of people writing
custom class loaders. If so, we can do that in a separate API-update
commit.)
Diffstat (limited to 'dalvik/src')
-rw-r--r-- | dalvik/src/main/java/dalvik/system/DexFile.java | 38 | ||||
-rw-r--r-- | dalvik/src/main/java/dalvik/system/PathClassLoader.java | 8 |
2 files changed, 31 insertions, 15 deletions
diff --git a/dalvik/src/main/java/dalvik/system/DexFile.java b/dalvik/src/main/java/dalvik/system/DexFile.java index da51e45..00de314 100644 --- a/dalvik/src/main/java/dalvik/system/DexFile.java +++ b/dalvik/src/main/java/dalvik/system/DexFile.java @@ -38,11 +38,13 @@ public final class DexFile { /** * Opens a DEX file from a given File object. This will usually be a ZIP/JAR - * file with a "classes.dex" inside. The method should not be used for files - * inside the Dalvik cache. - * - * @cts What will happen if we refer to the Dalvik cache? Should be either - * specified or throw an exception... + * file with a "classes.dex" inside. + * + * The VM will generate the name of the coresponding file in + * /data/dalvik-cache and open it, possibly creating or updating + * it first if system permissions allow. Don't pass in the name of + * a file in /data/dalvik-cache, as the named file is expected to be + * in its original (pre-dexopt) state. * * @param file * the File object referencing the actual DEX file @@ -57,11 +59,13 @@ public final class DexFile { /** * Opens a DEX file from a given filename. This will usually be a ZIP/JAR - * file with a "classes.dex" inside. The method should not be used for files - * inside the Dalvik cache. - * - * @cts What will happen if we refer to the Dalvik cache? Should be either - * specified or throw an exception... + * file with a "classes.dex" inside. + * + * The VM will generate the name of the coresponding file in + * /data/dalvik-cache and open it, possibly creating or updating + * it first if system permissions allow. Don't pass in the name of + * a file in /data/dalvik-cache, as the named file is expected to be + * in its original (pre-dexopt) state. * * @param fileName * the filename of the DEX file @@ -190,11 +194,23 @@ public final class DexFile { * @cts Exception comment is a bit cryptic. What exception will be thrown? */ public Class loadClass(String name, ClassLoader loader) { + String slashName = name.replace('.', '/'); + return loadClassBinaryName(slashName, loader); + } + + /** + * See {@link #loadClass(String, ClassLoader)}. + * + * This takes a "binary" class name to better match ClassLoader semantics. + * + * {@hide} + */ + public Class loadClassBinaryName(String name, ClassLoader loader) { return defineClass(name, loader, mCookie, null); //new ProtectionDomain(name) /*DEBUG ONLY*/); } - + native private static Class defineClass(String name, ClassLoader loader, int cookie, ProtectionDomain pd); diff --git a/dalvik/src/main/java/dalvik/system/PathClassLoader.java b/dalvik/src/main/java/dalvik/system/PathClassLoader.java index 55e61a9..597eb5b 100644 --- a/dalvik/src/main/java/dalvik/system/PathClassLoader.java +++ b/dalvik/src/main/java/dalvik/system/PathClassLoader.java @@ -179,8 +179,9 @@ public class PathClassLoader extends ClassLoader { * parent ClassLoader has failed to find a loaded class of the same name. * * @param name - * The name of the class to search for, in a human-readable form - * like "java.lang.String" or "java.net.URLClassLoader$3$1". + * The "binary name" of the class to search for, in a + * human-readable form like "java.lang.String" or + * "java.net.URLClassLoader$3$1". * @return the {@link Class} object representing the class * @throws ClassNotFoundException * if the class cannot be found @@ -199,8 +200,7 @@ public class PathClassLoader extends ClassLoader { //System.out.println("My path is: " + mPaths[i]); if (mDexs[i] != null) { - String slashName = name.replace('.', '/'); - Class clazz = mDexs[i].loadClass(slashName, this); + Class clazz = mDexs[i].loadClassBinaryName(name, this); if (clazz != null) return clazz; } else if (mZips[i] != null) { |