about Enum.values() memory allocation

Jason Parachoniak jparachoniak at google.com
Wed Jul 25 17:45:34 UTC 2018


Hi Ron,

Thanks for the heads up our team (michaelquigley@ speficially) is hoping to
expand enum unboxing to the collection types (and similar) this quarter.
We are finishing up the work on adding all of the information about
signatures to our type system to make this reasonable.

Making the iterator work seems reasonable.  (I think we would translate
that into a

for (int e = 1; e < maxOrdinal+1; e++) { // 0 is for null :)
  // Loop code.
}

You should be able to follow the bug for more details.

On Wed, Jul 25, 2018 at 9:32 AM Ron Shapiro <ronshapiro at google.com> wrote:

> +appreduce-team <appreduce-team at google.com> maybe this is something good
> that AppReduce can do?
>
> On Wed, Jul 25, 2018 at 12:30 PM nezih yigitbasi <nezihyigitbasi at gmail.com>
> wrote:
>
>> Thanks Ron. Yes, working around this is pretty easy. But, it is a common
>> pattern to foreach over Enum.values(), so it would be nice if the
>> runtime/compiler handles that in a more efficient way.
>>
>> Nezih
>>
>> Ron Shapiro <ronshapiro at google.com>, 25 Tem 2018 Çar, 04:18 tarihinde
>> şunu yazdı:
>>
>>> It's a bit of indirection, but can for (YourEnum e :
>>> EnumSet.allOf(YourEnum.class)) {} do the trick? It will still allocate, but
>>> likely only one instance instead of a full array.
>>>
>>> On Wed, Jul 25, 2018, 5:23 AM Michael Rasmussen <
>>> Michael.Rasmussen at roguewave.com> wrote:
>>>
>>>> With condy being added in Java 11, this could potentially be a use-case
>>>> for that ?
>>>>
>>>>
>>>> for (EnumType e: const(EnumType.values())) { ... }
>>>>
>>>>
>>>> Or perhaps it's time to add a List<E> valuesList(); to enum types, that
>>>> returns an immutable List of the values?
>>>>
>>>>
>>>> /Michael
>>>> ------------------------------
>>>> *From:* compiler-dev <compiler-dev-bounces at openjdk.java.net> on behalf
>>>> of nezih yigitbasi <nezihyigitbasi at gmail.com>
>>>> *Sent:* 25 July 2018 08:23:57
>>>> *To:* compiler-dev at openjdk.java.net
>>>> *Subject:* about Enum.values() memory allocation
>>>>
>>>> Hi,
>>>> I recently noticed in our app that Enum.values() allocates a
>>>> significant amount of memory when called in a tight loop as it clones the
>>>> constant values array (which is probably for immutability, and I can
>>>> understand that). I found that the same issue has been discussed back in
>>>> 2012:
>>>> http://mail.openjdk.java.net/pipermail/compiler-dev/2012-March/004210.html
>>>>
>>>> Are there any plans to address this issue going forward?
>>>>
>>>> Thanks!
>>>>
>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180725/fcc7d4f7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4855 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180725/fcc7d4f7/smime-0001.p7s>


More information about the compiler-dev mailing list