Fwd: [infinispan-dev] Avoid Collections.emptySet() / emptyMap() / emptyList

11 views
Skip to first unread message

Adrian Cole

unread,
Oct 19, 2012, 10:54:56 AM10/19/12
to jclou...@googlegroups.com

Learned something new!

---------- Forwarded message ----------
From: "Galder Zamarreño" <gal...@redhat.com>
Date: Oct 19, 2012 5:32 AM
Subject: [infinispan-dev] Avoid Collections.emptySet() / emptyMap() / emptyList
To: "infinispan -Dev List" <infinis...@lists.jboss.org>

Hi all,

Re: https://github.com/galderz/infinispan/commit/0609207d13216de81d77ff51dc20652ce270c635

Please avoid using these JDK methods where possible. I've created alternative versions in InfinispanCollections util class that provide a better implementation.

The problem with the JDK versions is that even if these collections are immutable, if you wanna iterate them (i.e. for(X : emptySet) ), they'll create a brand new Iterator every time they're looped, and that generates useless garbage.

The InfinispanCollections versions of emptyX will return a constant singleton iterator which avoids this problem, and avoids the need for client code to check if the collection is empty before looping.

Cheers,
--
Galder Zamarreño
gal...@redhat.com
twitter.com/galderz

Project Lead, Escalante
http://escalante.io

Engineer, Infinispan
http://infinispan.org


_______________________________________________
infinispan-dev mailing list
infinis...@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/infinispan-dev

Tim Peierls

unread,
Oct 19, 2012, 2:16:58 PM10/19/12
to jclou...@googlegroups.com
I can't find any sign that Guava is doing the same, but perhaps they've measured and found that the extra garbage is so readily reclaimed that it isn't worth the extra code complexity.

--tim

--
You received this message because you are subscribed to the Google Groups "jclouds-dev" group.
To post to this group, send email to jclou...@googlegroups.com.
To unsubscribe from this group, send email to jclouds-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jclouds-dev?hl=en.

Andrew Gaul

unread,
Oct 20, 2012, 5:29:39 PM10/20/12
to jclou...@googlegroups.com
I tested against Guava 13.0.1 and it does not create an unneeded object.  I will submit a pull request to clean up jclouds.
ImmutableIterator.java

Tim Peierls

unread,
Oct 20, 2012, 5:46:34 PM10/20/12
to jclou...@googlegroups.com
What clean-up is needed if Guava doesn't create any unneeded objects?

To view this discussion on the web visit https://groups.google.com/d/msg/jclouds-dev/-/mP6Ow_RYxIgJ.

Adrian Cole

unread,
Oct 20, 2012, 5:58:47 PM10/20/12
to jclou...@googlegroups.com

I'm guessing there are places we use Collections vs ImmutableX.of()

-A

Reply all
Reply to author
Forward
0 new messages