Patched version of the Trove library - changes the Collections semantics to match proper java.util.Map semantics
Java
Clone or download
schlosna Improve toArray performance for empty primitive hash sets and maps. (#2)
While analyzing some code for performance bottlenecks, I noticed that
TLongHashSet.toArray() for empty sets was spinning through a lot of CPU
iterating through all of the states, even though the colleciton was empty. This
change short circuits toArray to return the empty array for empty collections,
giving a roughly 50% increase in performance for empty collections, while
maintaining performance for non-empty collections.

JMH benchmarks for before/after comparison of TIntHashSet.toArray() for varying
sizes of sets of 0, 1, 10, and 100 elements.

Before changes (baseline):
Benchmark                               Mode Thr    Cnt  Sec         Mean   Mean error    Units
g.t.b.ToArrayBenchmark.toArray_0000    thrpt   1      5    2        0.049        0.005   ops/ns
g.t.b.ToArrayBenchmark.toArray_0001    thrpt   1      5    2        0.045        0.004   ops/ns
g.t.b.ToArrayBenchmark.toArray_0010    thrpt   1      5    2        0.018        0.001   ops/ns
g.t.b.ToArrayBenchmark.toArray_0100    thrpt   1      5    2        0.002        0.000   ops/ns
g.t.b.ToArrayBenchmark.toArray_0000     avgt   1      5    2       20.115        0.517    ns/op
g.t.b.ToArrayBenchmark.toArray_0001     avgt   1      5    2       21.412        1.070    ns/op
g.t.b.ToArrayBenchmark.toArray_0010     avgt   1      5    2       53.599        1.597    ns/op
g.t.b.ToArrayBenchmark.toArray_0100     avgt   1      5    2      453.046       41.071    ns/op

After changes:
Benchmark                               Mode Thr    Cnt  Sec         Mean   Mean error    Units
g.t.b.ToArrayBenchmark.toArray_0000    thrpt   1      5    2        0.074        0.002   ops/ns
g.t.b.ToArrayBenchmark.toArray_0001    thrpt   1      5    2        0.047        0.001   ops/ns
g.t.b.ToArrayBenchmark.toArray_0010    thrpt   1      5    2        0.018        0.001   ops/ns
g.t.b.ToArrayBenchmark.toArray_0100    thrpt   1      5    2        0.002        0.000   ops/ns
g.t.b.ToArrayBenchmark.toArray_0000     avgt   1      5    2       13.415        0.353    ns/op
g.t.b.ToArrayBenchmark.toArray_0001     avgt   1      5    2       21.666        0.856    ns/op
g.t.b.ToArrayBenchmark.toArray_0010     avgt   1      5    2       54.996        1.836    ns/op
g.t.b.ToArrayBenchmark.toArray_0100     avgt   1      5    2      443.527       13.193    ns/op

cherry-pick of upstream commit commit 3e91f4414c88143d24bd2ff0d750e631e2ef653c,
see https://bitbucket.org/trove4j/trove/pull-requests/2
Latest commit 0d87573 Aug 2, 2016

README.md

trove-3.0.3

Patched version of the Trove library - changes the Collections semantics to match proper java.util.Map semantics

This library has been patched by Palantir Technologies to make the following changes:

Branch palantir-gotham-3.12.x - used by Palantir Gotham 3.12.x:

  • Trove implements of series of decorators that wrap their custom collections and implement the standard Java Collections interfaces. These decorators depart from the behavior specified in the Map interface by returning 0 when put() or remove() is called with a key not in the map. This version returns null as specified by Map.
  • Trove collections use a magic primitive value to mean null when wrapped by a decorator. If this magic primitive value is actually added to the collection, decorators will treat it as null, the same way they would treat a missing value. This version distinguishes between these two cases and disallows adding null to decorators.

Branch palantir-gotham-4.x - used by Palantir Gotham 4.x:

  • This version adds implementations of arrays, lists, sets, and maps that utilize offheap memory allocated by the sun.misc.Unsafe class.

Original source code for this library available at:

http://sourceforge.net/projects/trove4j/files/trove/3.0.3/trove-3.0.3.tar.gz/download

This release made available under the LGPL version 2.1 - see LICENSE