<div dir="auto">If the CloseableCollection doesn't escape (likely), then JIT will get rid of the object allocation.  The internal Closeable[] (?) may still stick around... unless JIT can determine it doesn't escape too.  Can JIT escape analysis get rid of small arrays?  If so, the CloseableCollection's footprint disappears.  The only thing remaining is the logic.  That's exactly what an enhanced try-with-resources would do.<div dir="auto"><br></div><div dir="auto">So, instead of putting effort into a risky Java language change, I world suggest we put the effort into JIT to get rid of the allocations.  This will benefit not only CloseableCollection but a whole lot more.<br><div dir="auto"><br></div><div dir="auto">Switching topics... Please do a code analysis of where multiple try with resources are used.  Figure out the typical number of resources that are closed.  Then, set the default size of the internal array (?) to that size.  The default size may be too large and many elements in the array will be null.  By setting the default size smaller, there will be less unused space allocated and hence GC won't happen as often.</div><div dir="auto"><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 27, 2022, 8:25 AM Brian Goetz <<a href="mailto:brian.goetz@oracle.com">brian.goetz@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div style="word-wrap:break-word;line-break:after-white-space">
I think Mark’s solution is quite practical.  I think you’re greatly overestimating the benefits (“enormous ergonomic advantage”) of your idea (a common trap to fall into) and enormously^2 underestimating where the bar is for “add features to everyone’s Java.”
  
<div><br>
</div>
<div><br>
</div>
<div>
<div>
<div>
<div>
<div><br>
<blockquote type="cite">
<div>On Dec 27, 2022, at 9:16 AM, Red IO <<a href="mailto:redio.development@gmail.com" target="_blank" rel="noreferrer">redio.development@gmail.com</a>> wrote:</div>
<br>
<div>
<div dir="auto">This is a nice workaround but allocates additional resources and adds clutter in form of (still) a level of nesting and an unnecessary collection. Considering that try resource is only a syntactic sugar. It shouldn't be difficult to
 add this simple addition of syntax to provide this enormous ergonomic advantage. Also I think it's unlikely that try (... ); would find another useful use. </div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Tue, Dec 27, 2022, 15:07 Mark Raynsford <<a href="mailto:org.openjdk@io7m.com" target="_blank" rel="noreferrer">org.openjdk@io7m.com</a>> wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 2022-12-27T14:49:13 +0100<br>
Red IO <<a href="mailto:redio.development@gmail.com" rel="noreferrer noreferrer" target="_blank">redio.development@gmail.com</a>> wrote:<br>
><br>
> Sometimes we need to initialize something between the resources and we get<br>
> multiple levels of nesting.<br>
> And I was wondering if we could not just lose the block and auto close at<br>
> the end of a method like this :<br>
<br>
I work with Vulkan a lot, and that involves a metric ton of closeable<br>
resources that also have to be closed in stack order. I have a<br>
library-level solution for this:<br>
<br>
  <a href="https://www.io7m.com/software/jmulticlose/" rel="noreferrer noreferrer noreferrer" target="_blank">
https://www.io7m.com/software/jmulticlose/</a><br>
<br>
-- <br>
Mark Raynsford | <a href="https://www.io7m.com/" rel="noreferrer noreferrer noreferrer" target="_blank">
https://www.io7m.com</a><br>
<br>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>

</blockquote></div>