JDK-6796087 : Speed-up sun.nio.cs.StandardCharsets
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.nio.charsets
  • Affected Version: 6
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-01-21
  • Updated: 2017-08-23
Description
A DESCRIPTION OF THE REQUEST :
Class sun.nio.cs.StandardCharsets is automatically generated from file standard-charsets, with lowercase preloaded hash maps via Hasher class.
1.) I propose to preload the hash maps with uppercase strings.
2.) I propose to save the classMap hash map.
3.) I propose, to set names (canonical + aliases) in file sun.io.cs.standard-charsets to uppercase letters, if even possible. Please note my comment on bug JDK-6795538
4.) Classes sun.nio.cs.AbstractCharsetProvider and sun.nio.cs.ext.ExtendedCharsets should be enhanced accordingly.

I need this change for proceeding my work at https://java-nio-charset-enhanced.dev.java.net/.

JUSTIFICATION :
1.) The majority of the canonical charset names are uppercase. So the lookup() method, derived from FastCharsetProvider, would profit in speed, because the transformation via a toUpper() method would not have so much work to do the transformation, than the actually given toLower() method. Additionally toUpper() would be potentially faster, as the 2nd term of { if (c >= 'a' && c <= 'z') } would only come to process in the rare case of character '_'.
See proposed code in source section.
2.) If Map<String,Charset> cache would be retyped to Map<String,Object> and preloaded with the current values of Map<String,String> classMap, classMap could be saved, if lookup() method, derived from FastCharsetProvider, would be refactored according my proposal from bug JDK-6790402. This would speed-up method lookup() as explained there.
3.) This would additionally speed-up lookup from the hash maps in FastCharsetProvider, as the need of transformation by the toUpper() method would become very rare.

For more details see: bug JDK-6790402


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
General usage of uppercase names as keys for the hash maps, used in FastCharsetProvider.
ACTUAL -
Lowercase names as keys for the hash maps in FastCharsetProvider are generally used.

---------- BEGIN SOURCE ----------
    static final String toUpper(final String s) {
        boolean allUpper = true;
        char[] ca = null;
        for (int i=0; i<s.length(); i++) {
            int c = s.charAt(i);
            if (c >= 'a' && c <= 'z') {
                if (allUpper) {
                    ca = s.toCharArray();
                    allUpper = false;
                }
                ca[i] -= '\u0020';
            }
        }
        return allUpper ? s : new String(ca);
    }

---------- END SOURCE ----------