changeset 51534:edcf0d527658

8209837: Avoid initializing ExpiringCache during bootstrap Reviewed-by: sundar, forax
author redestad
date Fri, 24 Aug 2018 14:04:34 +0200
parents 7c3891b9f1e0
children 2e98c7737d8f
files src/java.base/share/classes/java/io/FileSystem.java src/java.base/unix/classes/java/io/UnixFileSystem.java src/java.base/windows/classes/java/io/WinNTFileSystem.java
diffstat 3 files changed, 39 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/io/FileSystem.java	Fri Jul 06 16:04:19 2018 +0200
+++ b/src/java.base/share/classes/java/io/FileSystem.java	Fri Aug 24 14:04:34 2018 +0200
@@ -231,18 +231,16 @@
 
     // Flags for enabling/disabling performance optimizations for file
     // name canonicalization
-    static boolean useCanonCaches;
-    static boolean useCanonPrefixCache;
+    static final boolean useCanonCaches;
+    static final boolean useCanonPrefixCache;
 
     private static boolean getBooleanProperty(String prop, boolean defaultVal) {
-        return Boolean.parseBoolean(System.getProperty(prop,
-                String.valueOf(defaultVal)));
+        String value = System.getProperty(prop);
+        return (value != null) ? Boolean.parseBoolean(value) : defaultVal;
     }
 
     static {
-        useCanonCaches      = getBooleanProperty("sun.io.useCanonCaches",
-                                                 useCanonCaches);
-        useCanonPrefixCache = getBooleanProperty("sun.io.useCanonPrefixCache",
-                                                 useCanonPrefixCache);
+        useCanonCaches      = getBooleanProperty("sun.io.useCanonCaches", false);
+        useCanonPrefixCache = useCanonCaches && getBooleanProperty("sun.io.useCanonPrefixCache", false);
     }
 }
--- a/src/java.base/unix/classes/java/io/UnixFileSystem.java	Fri Jul 06 16:04:19 2018 +0200
+++ b/src/java.base/unix/classes/java/io/UnixFileSystem.java	Fri Aug 24 14:04:34 2018 +0200
@@ -44,6 +44,8 @@
         colon = props.getProperty("path.separator").charAt(0);
         javaHome = StaticProperty.javaHome();
         userDir = StaticProperty.userDir();
+        cache = useCanonCaches ? new ExpiringCache() : null;
+        javaHomePrefixCache = useCanonPrefixCache ? new ExpiringCache() : null;
     }
 
 
@@ -145,11 +147,11 @@
     // same directory, and must not create results differing from the true
     // canonicalization algorithm in canonicalize_md.c. For this reason the
     // prefix cache is conservative and is not used for complex path names.
-    private ExpiringCache cache = new ExpiringCache();
+    private final ExpiringCache cache;
     // On Unix symlinks can jump anywhere in the file system, so we only
     // treat prefixes in java.home as trusted and cacheable in the
     // canonicalization algorithm
-    private ExpiringCache javaHomePrefixCache = new ExpiringCache();
+    private final ExpiringCache javaHomePrefixCache;
 
     public String canonicalize(String path) throws IOException {
         if (!useCanonCaches) {
@@ -158,7 +160,7 @@
             String res = cache.get(path);
             if (res == null) {
                 String dir = null;
-                String resDir = null;
+                String resDir;
                 if (useCanonPrefixCache) {
                     // Note that this can cause symlinks that should
                     // be resolved to a destination directory to be
@@ -266,8 +268,12 @@
         // (i.e., only remove/update affected entries) but probably
         // not worth it since these entries expire after 30 seconds
         // anyway.
-        cache.clear();
-        javaHomePrefixCache.clear();
+        if (useCanonCaches) {
+            cache.clear();
+        }
+        if (useCanonPrefixCache) {
+            javaHomePrefixCache.clear();
+        }
         return delete0(f);
     }
     private native boolean delete0(File f);
@@ -279,8 +285,12 @@
         // (i.e., only remove/update affected entries) but probably
         // not worth it since these entries expire after 30 seconds
         // anyway.
-        cache.clear();
-        javaHomePrefixCache.clear();
+        if (useCanonCaches) {
+            cache.clear();
+        }
+        if (useCanonPrefixCache) {
+            javaHomePrefixCache.clear();
+        }
         return rename0(f1, f2);
     }
     private native boolean rename0(File f1, File f2);
--- a/src/java.base/windows/classes/java/io/WinNTFileSystem.java	Fri Jul 06 16:04:19 2018 +0200
+++ b/src/java.base/windows/classes/java/io/WinNTFileSystem.java	Fri Aug 24 14:04:34 2018 +0200
@@ -51,6 +51,8 @@
         semicolon = props.getProperty("path.separator").charAt(0);
         altSlash = (this.slash == '\\') ? '/' : '\\';
         userDir = normalize(props.getProperty("user.dir"));
+        cache = useCanonCaches ? new ExpiringCache() : null;
+        prefixCache = useCanonPrefixCache ? new ExpiringCache() : null;
     }
 
     private boolean isSlash(char c) {
@@ -387,8 +389,8 @@
     // same directory, and must not create results differing from the true
     // canonicalization algorithm in canonicalize_md.c. For this reason the
     // prefix cache is conservative and is not used for complex path names.
-    private ExpiringCache cache       = new ExpiringCache();
-    private ExpiringCache prefixCache = new ExpiringCache();
+    private final ExpiringCache cache;
+    private final ExpiringCache prefixCache;
 
     @Override
     public String canonicalize(String path) throws IOException {
@@ -568,8 +570,12 @@
         // (i.e., only remove/update affected entries) but probably
         // not worth it since these entries expire after 30 seconds
         // anyway.
-        cache.clear();
-        prefixCache.clear();
+        if (useCanonCaches) {
+            cache.clear();
+        }
+        if (useCanonPrefixCache) {
+            prefixCache.clear();
+        }
         return delete0(f);
     }
 
@@ -582,8 +588,12 @@
         // (i.e., only remove/update affected entries) but probably
         // not worth it since these entries expire after 30 seconds
         // anyway.
-        cache.clear();
-        prefixCache.clear();
+        if (useCanonCaches) {
+            cache.clear();
+        }
+        if (useCanonPrefixCache) {
+            prefixCache.clear();
+        }
         return rename0(f1, f2);
     }