<div dir="ltr">Also see <a href="https://github.com/openjdk/jdk/pull/15614">https://github.com/openjdk/jdk/pull/15614</a>, which removes caching in `AbstractMap` etc, meaning once Valhalla is out, the anonymous set will be a value object with no overhead. In addition, hotspot already has an escape analysis mechanism which can optimize iterators to index for loops if it determines an iterator does not get shared.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 2, 2024 at 2:54 PM Jason Mehrens <<a href="mailto:jason_mehrens@hotmail.com">jason_mehrens@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div>
<div></div>
See: <a href="https://bugs.openjdk.org/browse/JDK-8193128" target="_blank">https://bugs.openjdk.org/browse/JDK-8193128</a><br>
<div></div>
<div style="font-family:inherit;font-size:inherit;color:inherit;background-color:transparent">
<br>
</div>
<div style="font-family:inherit;font-size:inherit;color:inherit;background-color:transparent">
Jason</div>
<hr style="display:inline-block;width:98%">
<div id="m_-6745568978198888556divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><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 Ernst <<a href="mailto:ryan@iernst.net" target="_blank">ryan@iernst.net</a>><br>
<b>Sent:</b> Friday, February 2, 2024 1:42 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> Object creation from iterating Map.of()/Set.of()/List.of()</font>
<div> </div>
</div>
<div><font size="2"><span style="font-size:11pt">
<div>The newer “of” methods in collections are really nice, they make creating these collections much easier and often result in better performance.
<br>
However, the empty collection cases with Map.of()/Set.of()/List.of() has one small downside. The implementations within ImmutableCollections use non-specialized implementations for zero sized collections. For example, ImmutableCollections.EMPTY_MAP is a MapN.
 If you iterate over that Map, even if it is empty as in the case for Map.of(), a new anonymous AbstractSet is created. In comparison, Collections.emptyMap().entrySet() returns emptySet().<br>
<br>
I don’t know what the reasoning was for rebuilding the empty based variants in ImmutableCollections. But regardless, it seems like the empty collections defined in ImmutableCollections should likewise never construct any objects.<br>
<br>
I’m happy to raise a PR to either mimic or reuse the empty collection implementations from Collections, but I wanted to check there isn’t some reasoning the of() methods work this way.</div>
</span></font></div>
</div>

</blockquote></div>