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

ResourceBundle.debug wastes time and space

XMLWordPrintable

    • 1.1.6
    • generic, x86, sparc
    • generic, solaris_2.5, windows_95
    • Not verified

            In java.util.ResourceBundle there is the exact example of a style that
        at JavaOne this year I told people to avoid. Sigh. There's the method

            /**
             * For printf debugging.
             */
            private static boolean debugFlag = false;
            private static void debug(String str) {
                if( debugFlag ) {
                    System.out.println("ResourceBundle: " + str);
                }
            }

        and then sprinkled throughout the code are calls of the form

        debug("Searching for parent " + baseName + localeName);
        debug("Found " + searchName + " in cache as NOTFOUND");
        debug("Found " + searchName + " in cache");
        debug("Searching for " + searchName );
        debug("Searching for " + searchName );
        debug("Adding " + cacheCandidates.elementAt(i) + " to cache"
        debug("Adding " + cacheCandidates.elementAt(i)

        The problem with this style is that the argument String gets constructed,
        even though when it arrives in the debug method it gets dropped on the
        floor. That's StringBuffer allocations, char[] allocations, synchronized
        StringBuffer.append() calls, arraycopy's for the chars, and if the argument
        gets longer than the default StringBuffer (16 chars), reallocating the
        char[] inside the StringBuffer and arraycopy'ing the chars again, then
        allocating and constructing a String from the StringBuffer. All dropped on
        the floor.

            The solution is to hoist the if test out of the debug method and inline
        it at all the call sites. It's not as pretty as macros in C, but it
        doesn't generate any code and doesn't waste any time or space.

        peter.kessler@Eng 1997-10-30

              rgillamsunw Richard Gillam (Inactive)
              pbk Peter Kessler
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: