Details
Description
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 inJDK-8140483.
if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
(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
if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
Attachments
Issue Links
- relates to
-
JDK-8161250 javac should use indy to compute switch indexes for strings and enums
-
- Open
-
-
JDK-8233873 final field values should be trusted as constant
-
- Open
-
-
JDK-8286190 Add test to verify constant folding for Enum fields
-
- Resolved
-
-
JDK-8149813 Move trusted final field handling from C2 LoadNode::Value to shared code
-
- Resolved
-
-
JDK-8261007 Frozen Arrays (Preview)
-
- Draft
-
-
JDK-8261099 Internal Frozen Arrays
-
- Draft
-
(1 relates to)