8162955: Activate anonymous class loading for small sources
authorhannesw
Mon, 08 Aug 2016 12:51:44 +0200
changeset 40219 b381e616c242
parent 40218 3b33c714f2ac
child 40220 756a2e306a01
8162955: Activate anonymous class loading for small sources Reviewed-by: sundar
nashorn/docs/DEVELOPER_README
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java
--- a/nashorn/docs/DEVELOPER_README	Thu Aug 04 17:49:29 2016 +0000
+++ b/nashorn/docs/DEVELOPER_README	Mon Aug 08 12:51:44 2016 +0200
@@ -302,6 +302,16 @@
 See -Dnashorn.debug for methods to access the event queue.
 The default value is 1024.
 
+SYSTEM PROPERTY: -Dnashorn.anonymous.classes.threshold=<value>
+
+Nashorn can use anonymous classes for loading compiled scripts, depending
+on the --anonymous-classes=[auto|true|false] option. Anonymous classes load
+faster, but the loaded classes get less optimization applied to them and
+therefore usually run slower. In the default "auto" setting, scripts are
+loaded as anonymous classes if the script size does not exceed 512 bytes.
+The above system property allows to set this threshold to a user defined
+value.
+
 ===============
 2. The loggers.
 ===============
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Thu Aug 04 17:49:29 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Mon Aug 08 12:51:44 2016 +0200
@@ -1502,7 +1502,7 @@
         final URL          url    = source.getURL();
         final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
         final CodeInstaller installer;
-        if (!env.useAnonymousClasses(isEval) || env._persistent_cache || !env._lazy_compilation) {
+        if (!env.useAnonymousClasses(source.getLength()) || env._persistent_cache || !env._lazy_compilation) {
             // Persistent code cache and eager compilation preclude use of VM anonymous classes
             final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
             installer = new NamedContextCodeInstaller(this, cs, loader);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java	Thu Aug 04 17:49:29 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java	Mon Aug 08 12:51:44 2016 +0200
@@ -222,7 +222,7 @@
     /** Timing */
     public final Timing _timing;
 
-    /** Whether to use anonymous classes. See {@link #useAnonymousClasses(boolean)}. */
+    /** Whether to use anonymous classes. See {@link #useAnonymousClasses(int)}. */
     private final AnonymousClasses _anonymousClasses;
     private enum AnonymousClasses {
         AUTO,
@@ -230,6 +230,12 @@
         ON
     }
 
+    /** Size threshold up to which we use anonymous classes in {@link AnonymousClasses#AUTO} setting */
+    private final int _anonymous_classes_threshold;
+
+    /** Default value for anonymous class threshold */
+    private final static int DEFAULT_ANON_CLASS_THRESHOLD = 512;
+
     /**
      * Constructor
      *
@@ -327,6 +333,8 @@
             throw new RuntimeException("Unsupported value for anonymous classes: " + anonClasses);
         }
 
+        this._anonymous_classes_threshold = Options.getIntProperty(
+                "nashorn.anonymous.classes.threshold", DEFAULT_ANON_CLASS_THRESHOLD);
 
         final String language = options.getString("language");
         if (language == null || language.equals("es5")) {
@@ -462,11 +470,12 @@
 
     /**
      * Returns true if compilation should use anonymous classes.
-     * @param isEval true if compilation is an eval call.
+     * @param sourceLength length of source being compiled.
      * @return true if anonymous classes should be used
      */
-    public boolean useAnonymousClasses(final boolean isEval) {
-        return _anonymousClasses == AnonymousClasses.ON || (_anonymousClasses == AnonymousClasses.AUTO && isEval);
+    public boolean useAnonymousClasses(final int sourceLength) {
+        return _anonymousClasses == AnonymousClasses.ON
+                || (_anonymousClasses == AnonymousClasses.AUTO && sourceLength <= _anonymous_classes_threshold);
     }
 
 }