8174831: Reduce number of Charset classes loaded on bootstrap

Reviewed-by: alanb
This commit is contained in:
Claes Redestad 2017-02-13 20:32:06 +01:00
parent bf8ad52b22
commit e17bdd6c77
4 changed files with 29 additions and 21 deletions
jdk/src/java.base/share/classes

@ -46,9 +46,6 @@ import sun.nio.cs.ArrayEncoder;
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 @@ class StringCoding {
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) {

@ -300,9 +300,8 @@ public abstract class Charset
*/
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 @@ public abstract class Charset
}
/* 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 @@ public abstract class Charset
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;
/**

@ -32,7 +32,6 @@ package sun.nio.cs;
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 @@ public class StandardCharsets extends CharsetProvider {
_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 @@ public class StandardCharsets extends CharsetProvider {
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 @@ public class StandardCharsets extends CharsetProvider {
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 @@ public class StandardCharsets extends CharsetProvider {
}
}
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));

@ -31,7 +31,6 @@ import java.nio.charset.Charset;
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