Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6636323

Optimize handling of builtin charsets

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • core-libs
    • b53
    • generic, x86
    • generic, linux

        Builtin charsets can be trusted more than user-provided charsets,
        and this provides optimization opportunities.


        /**
         * A {@link Charset} that is provided as part of the JDK.
         * Such a charset guarantees that the following pathological
         * conditions never occur:
         *
         * <ul>
         * <li>no collisions among builtin charset names or aliases
         * <li>the charset encoders and decoders never retain references to
         * the source or destination arrays after a coding operation
         * <li>the {@code Charset} constructors are always called with valid
         * arguments
         * </ul>
         *
         * <p>The default security manager enforces that user-provided
         * charsets do not subclass this class.
         */
        public abstract class BuiltinCharset extends Charset {
            protected BuiltinCharset(String canonicalName, String[] aliases) {
                super(canonicalName, aliases);
            }
        }

        In particular, do the CharsetEncoder and CharsetDecoder constructors really need to
        validate their input every single time, given that their parameters are build-time
        constants in practice?

            {#if[encoder]?protected:private}
            Charset$Coder$(Charset cs,
        float average$ItypesPerOtype$,
        float max$ItypesPerOtype$,
        $replType$ replacement)
            {
        this.charset = cs;
        this.average$ItypesPerOtype$ = average$ItypesPerOtype$;
        this.max$ItypesPerOtype$ = max$ItypesPerOtype$;
        this.replacement = replacement;

                if (cs instanceof sun.nio.cs.BuiltinCharset)
                    assertConstructorPreconditions(cs,
                                                   average$ItypesPerOtype$,
                                                   max$ItypesPerOtype$,
                                                   replacement);
                else
                    checkConstructorPreconditions(cs,
                                                  average$ItypesPerOtype$,
                                                  max$ItypesPerOtype$,
                                                  replacement);
            }

        Also, the StringCoding class could do this to check for whether defensive copies
        are necessary:

            private static boolean charsetIsTrusted(Charset cs) {
                return System.getSecurityManager() == null ||
                    cs instanceof sun.nio.cs.BuiltinCharset ||
                    cs.getClass().getClassLoader0() == null;
            }

              sherman Xueming Shen
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: