RFR: 8215995: Add specialized toArray methods to immutable collections

Claes Redestad claes.redestad at oracle.com
Wed Jan 2 18:17:43 UTC 2019


Hi,

the Set and List implementations in java.util.ImmutableCollections
inherits toArray from AbstractCollection, whose implementations always
fall back to an iterator-based approach. Additionally there's logic to
deal with not getting the expected number of elements from the iterator,
which can't happen for these immutable collections.

This patch add specialized implementations, along with a microbenchmark
to back up the claim that these are actually faster (a few tests I
created when working on JDK-8193128 are also included).

Webrev: http://cr.openjdk.java.net/~redestad/8215995/jdk.00/
Bug:    https://bugs.openjdk.java.net/browse/JDK-8215995

Test running:
java -jar build/linux-x64/images/test/micro/benchmarks.jar Immutable.*to 
-f 5 -w 4 -r 4:

Baseline:
Benchmark                             Mode  Cnt  Score   Error   Units
ImmutableColls.toArrayFromList       thrpt   25  2.949 ± 0.031  ops/us
ImmutableColls.toArrayFromSet        thrpt   25  3.822 ± 0.075  ops/us
ImmutableColls.toTypedArrayFromList  thrpt   25  2.474 ± 0.023  ops/us
ImmutableColls.toTypedArrayFromSet   thrpt   25  3.237 ± 0.085  ops/us

Patch:
Benchmark                             Mode  Cnt  Score   Error   Units
ImmutableColls.toArrayFromList       thrpt   25  7.939 ± 0.101  ops/us
ImmutableColls.toArrayFromSet        thrpt   25  5.399 ± 0.099  ops/us
ImmutableColls.toTypedArrayFromList  thrpt   25  3.941 ± 0.003  ops/us
ImmutableColls.toTypedArrayFromSet   thrpt   25  4.111 ± 0.101  ops/us

These micros use a mix of Set12/SetN and List12/ListN/SubList -
larger speedups could be provoked if only testing 1 and 2-elements lists
and sets, but I don't want to be too synthetic.

Thanks!

/Claes


More information about the core-libs-dev mailing list