Proposal: ArrayList constructor perforrmance improvement
Jason Mehrens
jason_mehrens at hotmail.com
Tue Dec 18 17:58:16 UTC 2018
Hi Steve,
>ArrayList has a constructor which takes an arbitrary Collection as a
>parameter. This constructor will create an iterator over the collection
>;and it will add each entry returned to the ArrayList.
>We have found that quite a lot of the time the object passed as a
>parameter is in fact an instance of ArrayList.
>In the case of an ArrayList it is possible to significantly increase the
>performance of the method since there is no need for an iterator - the
>backing array can be directly copied.
Maybe I'm looking at a different version of the ArrayList source code but it seems that the ArrayList constructor calls c.toArray(). If the given Collection is an ArrayList then that will call the overridden ArrayList.toArray which directly copies the backing array once and doesn't create an iterator. I would assume that most collections provide an optimized toArray().
Jason
More information about the core-libs-dev
mailing list