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

Galder Zamarreño galder at redhat.com
Mon Oct 22 02:22:52 EDT 2012


On Oct 19, 2012, at 4:50 PM, Vladimir Blagojevic <vblagoje at redhat.com> wrote:

> Cool! Did not know about this! Is this original idea or others are already doing this?

As spotted via Lord Profiler:
https://issues.jboss.org/secure/attachment/12357778/Screen%20Shot%202012-10-17%20at%201.21.48%20PM.png

> On 12-10-19 8:31 AM, Galder Zamarreño wrote:
>> 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
>> galder at redhat.com
>> twitter.com/galderz
>> 
>> Project Lead, Escalante
>> http://escalante.io
>> 
>> Engineer, Infinispan
>> http://infinispan.org
>> 
>> 
>> _______________________________________________
>> infinispan-dev mailing list
>> infinispan-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/infinispan-dev
> 


--
Galder Zamarreño
galder at redhat.com
twitter.com/galderz

Project Lead, Escalante
http://escalante.io

Engineer, Infinispan
http://infinispan.org




More information about the infinispan-dev mailing list