DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Our RandomAccess AbstractList implementations could avoid an Iterator allocation during toArray() #1556

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
And they'd be faster, too, per the definition of RandomAccess: 
http://docs.oracle.com/javase/7/docs/api/java/util/RandomAccess.html

The performance difference is unlikely to matter, but the subject came up in a 
StackOverflow question, so I figured I'd file this issue. 
http://stackoverflow.com/q/19383323/28465#comment28769722_19383714

Original issue reported on code.google.com by cpov...@google.com on 16 Oct 2013 at 4:42

GoogleCodeExporter commented 9 years ago
Given that the randomaccess interface explicitly states that indexed iteration 
should be faster there should be no downside in this change, instead it follows 
closely the recommendations given there.

I know that there are implementations like  persistent vectors which have a 
constant time indexed access but might be faster to iterate via an iterator. 
But these should not implement this interface IMHO. Also none of these 
datastructures is common in java, given the complete absence of persistent 
collections in the core library.

Original comment by Eric6i...@googlemail.com on 1 Nov 2013 at 8:22

GoogleCodeExporter commented 9 years ago
I'm not quite clear on what this is referring to, but I don't see any sense in 
which this issue could be unresolved.  ImmutableList.toArray() delegates to 
ImmutableList.copyIntoArray, which does an indexed iteration rather than using 
an Iterator.  (And RegularImmutableList does a System.arraycopy, which is even 
better.)

Original comment by lowas...@google.com on 24 Sep 2014 at 6:30

GoogleCodeExporter commented 9 years ago
Not sure if it's worth it, but I may know what's it all about:

ImmutableList.copyOf(Lists.transform(input, function))

works perfectly while (for the same input)

FluentIterable.from(input).transform(function).toList()

calls

ImmutableCollection.ensureCapacity(5)
ImmutableCollection.ensureCapacity(8)
ImmutableCollection.ensureCapacity(12)
ImmutableCollection.ensureCapacity(18)

though it could know better.

Original comment by Maaarti...@gmail.com on 25 Sep 2014 at 5:42

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08