From 9b2a02e92bc6e30bd2c9f01854558ac10c8c8e88 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 12 Sep 2015 19:58:50 -0600 Subject: [PATCH] avoid calling unrelated JNI methods during class initialization The main goal here is to avoid making JNI calls from code that really shouldn't need JNI (e.g. before this patch, ArrayList.add called Math.max, which called Math., which called Random., which called System.currentTimeMillis). Besides following the "pay for only what you need" principle, this change ensures we can call LambdaMetaFactory methods during AOT compilation with a minimal VM (i.e. without compiling in JNI methods we don't need). --- classpath/java/lang/Math.java | 7 +++++-- classpath/java/lang/System.java | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/classpath/java/lang/Math.java b/classpath/java/lang/Math.java index eb79c3cc1b..c1d1c0bdaf 100644 --- a/classpath/java/lang/Math.java +++ b/classpath/java/lang/Math.java @@ -15,7 +15,10 @@ import java.util.Random; public final class Math { public static final double E = 2.718281828459045; public static final double PI = 3.141592653589793; - private static final Random random = new Random(); + + private static class Static { + public static final Random random = new Random(); + } private Math() { } @@ -84,7 +87,7 @@ public final class Math { } public static double random() { - return random.nextDouble(); + return Static.random.nextDouble(); } public static native double floor(double v); diff --git a/classpath/java/lang/System.java b/classpath/java/lang/System.java index 10886dbb03..f6ceabad6b 100644 --- a/classpath/java/lang/System.java +++ b/classpath/java/lang/System.java @@ -22,7 +22,9 @@ import java.util.Hashtable; import java.util.Properties; public abstract class System { - private static final long NanoTimeBaseInMillis = currentTimeMillis(); + private static class NanoTime { + public static final long BaseInMillis = currentTimeMillis(); + } private static class Static { public static Properties properties = makeProperties(); @@ -94,7 +96,7 @@ public abstract class System { public static native int identityHashCode(Object o); public static long nanoTime() { - return (currentTimeMillis() - NanoTimeBaseInMillis) * 1000000; + return (currentTimeMillis() - NanoTime.BaseInMillis) * 1000000; } public static String mapLibraryName(String name) {