EnumData space optimization in j.l.Class (JEP-146)

Remi Forax forax at univ-mlv.fr
Tue Dec 18 18:50:16 UTC 2012

On 12/18/2012 10:18 AM, Peter Levart wrote:
> On 12/17/2012 11:39 PM, Remi Forax wrote:
>> On 12/17/2012 11:14 PM, Peter Levart wrote:
>>> On 12/17/2012 10:26 PM, Mandy Chung wrote:
>>>> On 12/17/12 7:36 AM, Peter Levart wrote:
>>>>> Hi David and others,
>>>>> Here's a patch that eliminates one of two fields in 
>>>>> java.lang.Class, related to caching enum constants:
>>>>> http://dl.dropbox.com/u/101777488/jdk8-tl/JEP-149.enum/webrev.01/index.html 
>>>>> It does it by moving one field to a subclass of HashMap, which is 
>>>>> referenced by a remaining field that serves two different 
>>>>> purposes/stages of caching.
>>>> Your observation of merging the enumConstants and 
>>>> enumConstantDirectory is a good one.   I see that caching of 
>>>> enumConstantDirectory is important as it's used by EnumMap and 
>>>> EnumSet whose performance is critical (specified with constant time 
>>>> operations).  I'm unsure about Class.getEnumConstants whether it's 
>>>> performance critical and worths the complexity of your proposed fix 
>>>> (the enumData field of two types).  If a class has cached an 
>>>> enumConstantDirectory, Class.getEnumConstants can return a clone of 
>>>> its values().
>>>> Anyone knows how Class.getEnumConstants is commonly used and needs 
>>>> to be performant?  I suspect it's more typical to obtain the list 
>>>> of enum constants statistically (calling Enum.values()) than 
>>>> reflectively.
>>> Hi Mandy,
>>> public Class.getEnumConstants() is a reflection mirror of 
>>> SomeEnum.values(). It returns a defensive copy of the constants 
>>> array. The primary place for Enum constants is in a private static 
>>> final $VALUES field, generated by compiler in each Enum subclass. 
>>> But that I think is not part of specification, so for internal usage 
>>> (as far as I have managed to find out only in the constructors of 
>>> EnumSet and EnumMap), the package-private 
>>> Class.getEnumConstantsShared() is used which obtains a copy of the 
>>> array by calling SomeEnum.values() and than caches is.
>>> The Class.enumConstantDirectory() on the other hand is an internal 
>>> package-private method that returns a shared/cached Map<String, T>, 
>>> which is used internally to implement SomeEnum.valueOf(String) and 
>>> Enum.valueOf(Class, String) static methods.
>>> Both package-private methods must be fast.
>>> Regards, Peter
>> for what it worth, I'm the guy behind the patch of bug 6276988 (it 
>> was before OpenJDK was setup BTW),
>>   http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6276988
>> and for the little story, I need that patch because I was developing 
>> an Eclipse plugin that uses EnumSet to represent the possible 
>> completion values.
>> So to answer to Mandy, this application needs really fast EnumSet 
>> creation thus really fast getEnumConstantShared() because the 
>> EnumSets was created as user types code.
> Hi Rémi,
> is 600M EnumSets / sec good enough for a fast typist?

The eclipse plugin uses is a LR parser (a GLR exactly) which has the 
stupid property that it can change the whole parse tree if you just add 
one character.

Also i'm maybe wrong but, if you test with several different 'enum 
classes', you should see a slowdown.


More information about the core-libs-dev mailing list