8215194: Initial size of UnicodeBlock map is incorrect

Reviewed-by: rriggs, rgoel, igerasim
This commit is contained in:
Naoto Sato 2018-12-11 13:13:18 -08:00
parent 7cea6f6ac2
commit c2364ff9e0
2 changed files with 16 additions and 4 deletions

View file

@ -681,11 +681,12 @@ class Character implements java.io.Serializable, Comparable<Character> {
*/ */
public static final class UnicodeBlock extends Subset { public static final class UnicodeBlock extends Subset {
/** /**
* 649 - the expected number of entities * 667 - the expected number of entities
* 0.75 - the default load factor of HashMap * 0.75 - the default load factor of HashMap
*/ */
private static final int NUM_ENTITIES = 667;
private static Map<String, UnicodeBlock> map = private static Map<String, UnicodeBlock> map =
new HashMap<>((int)(649 / 0.75f + 1.0f)); new HashMap<>((int)(NUM_ENTITIES / 0.75f + 1.0f));
/** /**
* Creates a UnicodeBlock with the given identifier name. * Creates a UnicodeBlock with the given identifier name.

View file

@ -23,7 +23,7 @@
/** /**
* @test * @test
* @bug 8080535 8191410 * @bug 8080535 8191410 8215194
* @summary Expected size of Character.UnicodeBlock.map is not optimal * @summary Expected size of Character.UnicodeBlock.map is not optimal
* @library /test/lib * @library /test/lib
* @modules java.base/java.lang:open * @modules java.base/java.lang:open
@ -32,6 +32,7 @@
* @run main OptimalMapSize * @run main OptimalMapSize
*/ */
import java.lang.reflect.Field;
import jdk.test.lib.util.OptimalCapacity; import jdk.test.lib.util.OptimalCapacity;
// What will be the number of the Unicode blocks in the future. // What will be the number of the Unicode blocks in the future.
@ -44,14 +45,24 @@ import jdk.test.lib.util.OptimalCapacity;
// After implementing support of Unicode 9 and 10 in Java, there will // After implementing support of Unicode 9 and 10 in Java, there will
// be 638 entries in Character.UnicodeBlock.map. // be 638 entries in Character.UnicodeBlock.map.
// //
// As of Unicode 11, 667 entries are expected.
//
// Initialization of the map and this test will have to be adjusted // Initialization of the map and this test will have to be adjusted
// accordingly then. // accordingly then.
//
// Note that HashMap's implementation aligns the initial capacity to
// a power of two size, so it will end up 1024 (and thus succeed) in
// cases, such as 638 and 667.
public class OptimalMapSize { public class OptimalMapSize {
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
// The initial size of Character.UnicodeBlock.map. // The initial size of Character.UnicodeBlock.map.
// See src/java.base/share/classes/java/lang/Character.java // See src/java.base/share/classes/java/lang/Character.java
int initialCapacity = (int)(638 / 0.75f + 1.0f); Field f = Character.UnicodeBlock.class.getDeclaredField("NUM_ENTITIES");
f.setAccessible(true);
int num_entities = f.getInt(null);
assert num_entities == 667;
int initialCapacity = (int)(num_entities / 0.75f + 1.0f);
OptimalCapacity.ofHashMap(Character.UnicodeBlock.class, OptimalCapacity.ofHashMap(Character.UnicodeBlock.class,
"map", initialCapacity); "map", initialCapacity);