about Enum.values() memory allocation
Ron Shapiro
ronshapiro at google.com
Wed Jul 25 16:32:34 UTC 2018
+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/bdbbf70e/attachment.html>
More information about the compiler-dev
mailing list