From 08076a7b14ed39301933dde51587a2cd79109d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Walln=C3=B6fer?= Date: Mon, 8 Aug 2016 12:51:44 +0200 Subject: [PATCH] 8162955: Activate anonymous class loading for small sources Reviewed-by: sundar --- nashorn/docs/DEVELOPER_README | 10 ++++++++++ .../jdk/nashorn/internal/runtime/Context.java | 2 +- .../internal/runtime/ScriptEnvironment.java | 17 +++++++++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/nashorn/docs/DEVELOPER_README b/nashorn/docs/DEVELOPER_README index deb525d5ff4..2c116bfac02 100644 --- a/nashorn/docs/DEVELOPER_README +++ b/nashorn/docs/DEVELOPER_README @@ -302,6 +302,16 @@ Nashorn provides a fixed sized runtime event queue for debugging purposes. See -Dnashorn.debug for methods to access the event queue. The default value is 1024. +SYSTEM PROPERTY: -Dnashorn.anonymous.classes.threshold= + +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. =============== diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java index 21155f9c05e..17ff028b383 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java @@ -1502,7 +1502,7 @@ public final class Context { 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); diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java index 3e358cc51dd..5b62cf901d6 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java @@ -222,7 +222,7 @@ public final class ScriptEnvironment { /** 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 @@ public final class ScriptEnvironment { 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 @@ public final class ScriptEnvironment { 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 @@ public final class ScriptEnvironment { /** * 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); } }