<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div>
<div style="font-family: Calibri; text-align: left; color: rgb(0, 0, 0); margin-left: 5pt; font-size: 10pt;">
Confidential - Oracle Restricted \Including External Recipients</div>
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Ryan, and thank you for your observations!</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
We are currently working on improved support for lazily computed values, where <code>
LazyConstant</code> is just the first step. </div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
As you say, there is room for improvement in the <code>Class</code> class, but I feel it would be better to await said improvements rather than doing an intermediate change that would soon be replaced anyhow.</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
I will add the enum-related methods you mentioned to our internal list of improvement candidates to be made once (and if) the next steps of lazy become available.</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Thanks again, </div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Per Minborg</div>
<div id="appendonsend"></div>
<div><br>
<div style="font-family: Calibri; text-align: left; color: rgb(0, 0, 0); margin-left: 5pt; font-size: 10pt;">
Confidential - Oracle Restricted \Including External Recipients</div>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Ryan Hallock <ryan@hallock.us><br>
<b>Sent:</b> Friday, February 13, 2026 2:20 AM<br>
<b>To:</b> Chen Liang <chen.l.liang@oracle.com><br>
<b>Cc:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Re: Add @Stable to Class#enumConstants.</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">Hi Chen, just searching GitHub it seems there are 2600 results for Class#getEnumConstants()[0], from looking at them it's mostly used in a fallback scenario in most cases when they only have the Class. So I wouldn't be surprised if most of these
callsites would never be able to be resolved, this was meant to be an example that's easy to make an IR test & micro benchmark for.</div>
<div dir="ltr"><br>
</div>
<div>Class#enumConstants are also used in getting the universe of EnumMap, EnumSet, So call sites like EnumSet.noneOf(Class), and EnumMap(Class) would also benefit from such a change as it may be able to fold the volatile read. (untested)</div>
<div><br>
</div>
<div>In defense to using @Stable over LazyConstant is there would be a capturing lambda plus an LazyConstant allocated for every class even if it's not an Enum, then if you wanted to make LazyConstant itself lazy it would also require a @Stable to be able to
fold. This seems like an antipattern. Also this kind of goes against the idea behind caching Class#enumConstants here as it is currently null if it's never used. As for the implementation I have in mind would also likely place the Unsafe#compareAndExchangeReference
in the existing Class.Atomic as to still allow this being used early in the startup process similar to LazyConstant.</div>
<div><br>
</div>
<div>Please let me know if you have any additional questions.</div>
<div><br>
</div>
<div>Cheers,</div>
<div>Ryan Hallock</div>
</div>
<br>
<div class="x_gmail_quote x_gmail_quote_container">
<div dir="ltr" class="x_gmail_attr">On Thu, Feb 12, 2026 at 11:46 PM Chen Liang <<a href="mailto:chen.l.liang@oracle.com">chen.l.liang@oracle.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg6905785283247061210">
<div dir="ltr">
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hi Ryan, is there a use case where people call getEnumConstants() and access a fixed index of array element?</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
This is so far the only use case I can think of that would be negatively impacted by the lack of constant folding for this method.</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
In addition, I do not recommend adding more @Stable - we should consider better representations like LazyConstant.</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif; font-size:12pt; color:rgb(0,0,0)">
Regards</div>
<div id="x_m_6905785283247061210appendonsend"></div>
<hr style="display:inline-block; width:98%">
<div id="x_m_6905785283247061210divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> core-libs-dev <<a href="mailto:core-libs-dev-retn@openjdk.org" target="_blank">core-libs-dev-retn@openjdk.org</a>>
on behalf of Ryan Hallock <<a href="mailto:ryan@hallock.us" target="_blank">ryan@hallock.us</a>><br>
<b>Sent:</b> Thursday, February 12, 2026 3:45 PM<br>
<b>To:</b> <a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a> <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Subject:</b> Add @Stable to Class#enumConstants.</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hello, I am new to OpenJDK development and I want to propose changes to Class#enumConstants from Class#getEnumConstants()/Class#getSharedEnumConstants().<br>
<br>
Currently consider the following example:<br>
```java<br>
enum E {<br>
A, B, C<br>
}<br>
E[] values = E.values();<br>
E[] constants = E.class.getEnumConstants();<br>
assert Arrays.equals(values, constants);<br>
```<br>
These methods provide an equal array of values, but they are treated differently. This is because the _constants_ stored by Class#enumConstants are non final and declared volatile. So C2 can't trust this call. Consider if I only wanted to access the value from
an offset from these arrays.<br>
```java<br>
E value = values[0];<br>
E constant = constants[0];<br>
assert value == constant;<br>
```<br>
_constant_ would never be folded but _value_ would be. <br>
<br>
So I propose the addition of @Stable annotation added to Class#enumConstants so _constant_ can be folded by C2. This is not a trivial change as it requires changing the data race to abide by @Stable. Could I be sponsored for this change (JBS issue as well)?<br>
<br>
<div>A future proposal would be adding @Stable to Class#enumConstantDirectory, though that can be a seperate enhancement as that might require changing of the data structure to allow folding. Let me know if it should be combined as the Stable annotation itself
would be implemented in a similar way for both.</div>
<br>
Thanks,<br>
Ryan Hallock<br>
<br>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</body>
</html>