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

Modify generated valueOf method for enum (performance enhancement)

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • tools
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Especially for larger-size enums (more than ten elements), the valueOf method implementation is slow, iterating over all elements of the $VALUES array until an element with matching name is found.

      The request is to change the code generated for this method to a hashmap-based lookup, at least for "large" enums.

      JUSTIFICATION :
      Performance. Example: for an enumeration of 192 countries, the average access times on my machine are as follows:

      Generated valueOf method 2,510 nanoseconds
      Hashmap-based getInstance method 92 nanoseconds

      For a 64-element enum, the corresponding times are 781 and 87 nanoseconds.
      For a 10-element enum, the times are 157 and 76 nanoseconds.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Cache name-element pairs in unmodifiable HashMap.
      ACTUAL -
      See above.

      ---------- BEGIN SOURCE ----------
      For enum E {...}:

      public static E valueOf(String elmName) {
         E elm = _cachedRefs.get(elmName);
         if (elm == null) throw new IllegalArgumentException(...);
         return elm;
      }

      private static final Map<String, E> _cachedRefs;
      static {
         // populate map
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Write getInstance method for each enum.

            ahe Peter Ahe
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: