changeset 16699:a9986a4dff27

8174831: Reduce number of Charset classes loaded on bootstrap Reviewed-by: alanb
author redestad
date Mon, 13 Feb 2017 20:32:06 +0100
parents a3b3c7b6464d
children 735dfe90bf33
files src/java.base/share/classes/java/lang/StringCoding.java src/java.base/share/classes/java/nio/charset/Charset.java src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template src/java.base/share/classes/sun/nio/cs/US_ASCII.java
diffstat 4 files changed, 29 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/StringCoding.java	Mon Feb 13 10:47:15 2017 -0700
+++ b/src/java.base/share/classes/java/lang/StringCoding.java	Mon Feb 13 20:32:06 2017 +0100
@@ -46,9 +46,6 @@
 import static java.lang.String.LATIN1;
 import static java.lang.String.UTF16;
 import static java.lang.String.COMPACT_STRINGS;
-import static java.nio.charset.StandardCharsets.ISO_8859_1;
-import static java.nio.charset.StandardCharsets.US_ASCII;
-import static java.nio.charset.StandardCharsets.UTF_8;
 
 /**
  * Utility class for string encoding and decoding.
@@ -64,6 +61,10 @@
     private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
         new ThreadLocal<>();
 
+    private static final Charset ISO_8859_1 = Charset.forName("iso-8859-1");
+    private static final Charset US_ASCII = Charset.forName("us-ascii");
+    private static final Charset UTF_8 = Charset.forName("utf-8");
+
     private static boolean warnUnsupportedCharset = true;
 
     private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
--- a/src/java.base/share/classes/java/nio/charset/Charset.java	Mon Feb 13 10:47:15 2017 -0700
+++ b/src/java.base/share/classes/java/nio/charset/Charset.java	Mon Feb 13 20:32:06 2017 +0100
@@ -300,9 +300,8 @@
      */
     private static void checkName(String s) {
         int n = s.length();
-        if (!atBugLevel("1.4")) {
-            if (n == 0)
-                throw new IllegalCharsetNameException(s);
+        if (n == 0 && !atBugLevel("1.4")) {
+            throw new IllegalCharsetNameException(s);
         }
         for (int i = 0; i < n; i++) {
             char c = s.charAt(i);
@@ -319,7 +318,9 @@
     }
 
     /* The standard set of charsets */
-    private static CharsetProvider standardProvider = new StandardCharsets();
+    private static final CharsetProvider standardProvider = new StandardCharsets();
+
+    private static final String[] zeroAliases = new String[0];
 
     // Cache of the most-recently-returned charsets,
     // along with the names that were used to find them
@@ -626,7 +627,6 @@
 
     private final String name;          // tickles a bug in oldjavac
     private final String[] aliases;     // tickles a bug in oldjavac
-    private final String[] zeroAliases = new String[0];
     private Set<String> aliasSet = null;
 
     /**
--- a/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template	Mon Feb 13 10:47:15 2017 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template	Mon Feb 13 20:32:06 2017 +0100
@@ -32,7 +32,6 @@
 import java.nio.charset.Charset;
 import java.nio.charset.spi.CharsetProvider;
 import java.util.Iterator;
-import java.util.Locale;
 import java.util.Map;
 import sun.security.action.GetPropertyAction;
 
@@ -44,13 +43,13 @@
     _INCLUDE_CACHE_MAP_
 
     // Maps canonical names to class names
-    private Map<String,String> classMap;
+    private final Map<String,String> classMap;
     // Maps alias names to canonical names
-    private Map<String,String> aliasMap;
+    private final Map<String,String> aliasMap;
     // Maps canonical names to cached instances
-    private Map<String,Charset> cache;
+    private final Map<String,Charset> cache;
 
-    private String packagePrefix = "sun.nio.cs";
+    private static final String packagePrefix = "sun.nio.cs";
 
     public StandardCharsets() {
         this.aliasMap = new Aliases();
@@ -102,10 +101,16 @@
         if (cln == null)
             return null;
 
-        if (cln.equals("US_ASCII")) {
-            cs = new US_ASCII();
-            cache.put(csn, cs);
-            return cs;
+        // As all charset class names added to classMap are string literals we
+        // can check identity here as an optimization
+        if (cln == "US_ASCII") {
+            return cache(csn, new US_ASCII());
+        }
+        if (cln == "ISO_8859_1") {
+            return cache(csn, new ISO_8859_1());
+        }
+        if (cln == "UTF_8") {
+            return cache(csn, new UTF_8());
         }
 
         // Instantiate the charset and cache it
@@ -114,9 +119,7 @@
             Object o = Class.forName(packagePrefix + "." + cln,
                                      true,
                                      this.getClass().getClassLoader()).newInstance();
-            cs = (Charset)o;
-            cache.put(csn, cs);
-            return cs;
+            return cache(csn, (Charset)o);
         } catch (ClassNotFoundException |
                  IllegalAccessException |
                  InstantiationException x) {
@@ -124,6 +127,11 @@
         }
     }
 
+    private Charset cache(String csn, Charset cs) {
+        cache.put(csn, cs);
+        return cs;
+    }
+
     public final Charset charsetForName(String charsetName) {
         synchronized (this) {
             return lookup(canonicalize(charsetName));
--- a/src/java.base/share/classes/sun/nio/cs/US_ASCII.java	Mon Feb 13 10:47:15 2017 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/US_ASCII.java	Mon Feb 13 20:32:06 2017 +0100
@@ -31,7 +31,6 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
-import java.util.Arrays;
 
 public class US_ASCII
     extends Charset