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

Optimize Formatter.formatMessage

XMLWordPrintable

      We noticed that java.util.logging.Formatter.formatMessage shows up in application profiles and wondered whether its performance can be improved. One low-hanging fruit is to replace 3 calls to record.getMessage with a single one. A tougher question is whether the "synchronized" keyword can be removed from this method. There is no obvious use of synchronization here, but the concurrency properties of java.util.logging are historically troublesome and hard to understand.

      Here's a possible patch that passes logging jtreg tests:

      --- a/src/java.logging/share/classes/java/util/logging/Formatter.java
      +++ b/src/java.logging/share/classes/java/util/logging/Formatter.java
      @@ -109,15 +109,14 @@
            * @param record the log record containing the raw message
            * @return a localized and formatted message
            */
      - public synchronized String formatMessage(LogRecord record) {
      + public String formatMessage(LogRecord record) {
               String format = record.getMessage();
               java.util.ResourceBundle catalog = record.getResourceBundle();
               if (catalog != null) {
                   try {
      - format = catalog.getString(record.getMessage());
      + format = catalog.getString(format);
                   } catch (java.util.MissingResourceException ex) {
                       // Drop through. Use record message as format
      - format = record.getMessage();
                   }
               }
               // Do the formatting.

            dfuchs Daniel Fuchs
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: