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

Enums should be fully optimized



      Enums are the recommended way to express abstract named singleton values. But they do not optimize well, because the JIT does not trust their ordinal and name fields.

      (Another problem is that the current translation strategy for switch-on-enum uses a mutable array (for locally indexing enums) when a constant or stable one will work better. Fixing this will require a change to the translation strategy for switches, which is a separate bug. But the present fix is valuable standing alone, because a determined library writer can switch or if/else on the raw ordinal of an enum, getting full performance. The cost of this hack is a tight coupling between the enum structure and the library, but this is sometimes acceptable, such as in Java SE platform code.)

      Suggested fix for constant folding ordinal and name fields:

      diff --git a/src/share/vm/ci/ciField.cpp b/src/share/vm/ci/ciField.cpp
      --- a/src/share/vm/ci/ciField.cpp
      +++ b/src/share/vm/ci/ciField.cpp
      @@ -219,6 +219,9 @@
         // Trust final fields in String
         if (holder->name() == ciSymbol::java_lang_String())
           return true;
      + // Trust standard enum fields, such as Enum.ordinal and Enum.name.
      + if (holder->name() == ciSymbol::java_lang_Enum())
      + return true;
         // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
         // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
         if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||


          Issue Links



              • Assignee:
                jrose John Rose
              • Votes:
                0 Vote for this issue
                3 Start watching this issue


                • Created: