Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cats.kernel.Hash port for Scala CHAMP HashMap #4193

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a1e22f1
Initial port of Scala CHAMP HashMap
DavidGregory084 Apr 19, 2022
1881a9b
Update access modifiers for HashMap internals
DavidGregory084 Apr 21, 2022
5f93902
Fix regression in Foldable#iterateRight by ensuring Iterator is alway…
DavidGregory084 Apr 21, 2022
2f44919
Appease the formatter
DavidGregory084 Apr 21, 2022
ff0e86c
Remove sizeHint - it serves no purpose now that size is cached in bit…
DavidGregory084 Apr 21, 2022
c655fdd
Revert sizeHint removal as I was wrong - it prevents erroneous subnod…
DavidGregory084 Apr 21, 2022
ef63d19
Narrow conditions for subnode escalation
DavidGregory084 Apr 21, 2022
7dabe64
Use byteswap hashing in HashMap to improve input hash
DavidGregory084 Apr 21, 2022
5864984
Add copyright notice from Scala standard library to meet license obli…
DavidGregory084 Apr 25, 2022
3fdf9d5
Add benchmarks for HashMap
DavidGregory084 May 3, 2022
d5e7596
Override concrete UnorderedFoldable operations for HashMap
DavidGregory084 May 3, 2022
186264f
Override even more concrete UnorderedFoldable operations for HashMap
DavidGregory084 May 3, 2022
151e52d
Addressing review comments:
DavidGregory084 May 16, 2022
5c15c53
Add a test for collectFirst to NonEmptyCollectionSuite
DavidGregory084 May 16, 2022
0169ce3
Fix key-value bitmap calculation when subnodes are propagated as the …
DavidGregory084 May 17, 2022
b292134
Add tests for consistency between String representation and equality
DavidGregory084 May 17, 2022
27a69d3
Reduce duplication in 2.12 concat, remove an unnecessary branch
DavidGregory084 May 17, 2022
6203e1a
Weaken show / equality laws due to collisions
DavidGregory084 May 17, 2022
0f66333
Add a toMap operation that returns a Map wrapper for HashMap
DavidGregory084 May 17, 2022
a4732f2
Add copyright headers for WrappedHashMap
DavidGregory084 May 17, 2022
ea5c7de
Avoid allocating tuple in HashMapBench
DavidGregory084 May 17, 2022
7cc3ade
Address review comments
DavidGregory084 May 17, 2022
d25015f
Address further review comments:
DavidGregory084 May 23, 2022
d572156
Change formatting of HashMapMonoid#combine
DavidGregory084 May 23, 2022
ea31a5d
Fix inconsistency in benchmark names
DavidGregory084 May 23, 2022
e7343e8
Implement a concat operation on BitMapNode to merge the CHAMP structures
DavidGregory084 May 24, 2022
a64bcf7
Revert "Implement a concat operation on BitMapNode to merge the CHAMP…
DavidGregory084 Jun 21, 2022
0a2ff88
Merge branch 'main' of github.com:typelevel/cats into eq-based-map
DavidGregory084 Jun 21, 2022
9cc8be6
Update formatting for new scalafmt version
DavidGregory084 Jun 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions bench/src/main/scala-2.12/cats/bench/HashMapBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.bench

import cats.data.HashMap
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import scala.collection.immutable.{HashMap => SHashMap}

@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class HashMapBench {
@Param(Array("0", "1", "2", "3", "4", "7", "8", "15", "16", "17", "39", "282", "4096", "131070", "7312102"))
var size: Int = _

var hashMap: HashMap[Long, Int] = _
var otherHashMap: HashMap[Long, Int] = _
var scalaMap: SHashMap[Long, Int] = _
var otherScalaMap: SHashMap[Long, Int] = _

def hashMapOfSize(n: Int) = HashMap.fromSeq((1L to (n.toLong)).zipWithIndex)
def scalaMapOfSize(n: Int) = SHashMap((1L to (n.toLong)).zipWithIndex: _*)

@Setup(Level.Trial)
def init(): Unit = {
hashMap = hashMapOfSize(size)
otherHashMap = hashMapOfSize(size)
scalaMap = scalaMapOfSize(size)
otherScalaMap = scalaMapOfSize(size)
}

@Benchmark
def hashMapFromSeq(bh: Blackhole): Unit =
bh.consume(hashMapOfSize(size))

@Benchmark
def scalaMapFromSeq(bh: Blackhole): Unit =
bh.consume(scalaMapOfSize(size))

@Benchmark
@OperationsPerInvocation(1000)
def hashMapUpdated(bh: Blackhole): Unit = {
var hs = hashMap
var i = 0
while (i < 1000) {
hs = hs.updated(-i.toLong, i)
i += 1
}
bh.consume(hs)
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapUpdated(bh: Blackhole): Unit = {
var ss = scalaMap
var i = 0
while (i < 1000) {
ss = ss.updated(-i.toLong, i)
i += 1
}
bh.consume(ss)
}

@Benchmark
@OperationsPerInvocation(1000)
def hashMapRemoved(bh: Blackhole): Unit = {
var hs = hashMap
var i = 0L
while (i < 1000L) {
hs = hs.removed(i)
i += 1L
}
bh.consume(hs)
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapRemoved(bh: Blackhole): Unit = {
var ss = scalaMap
var i = 0L
while (i < 1000L) {
ss -= i
i += 1L
}
bh.consume(ss)
}

@Benchmark
@OperationsPerInvocation(1000)
def hashMapContains(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(hashMap.contains(i))
i += 1L
}
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapContains(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(scalaMap.contains(i))
i += 1L
}
}

@Benchmark
@OperationsPerInvocation(1000)
def hashMapGet(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(hashMap.get(i))
i += 1L
}
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapGet(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(scalaMap.get(i))
i += 1L
}
}

@Benchmark
def hashMapForeach(bh: Blackhole): Unit =
hashMap.foreach((k, v) => bh.consume((k, v)))

@Benchmark
def scalaMapForeach(bh: Blackhole): Unit =
scalaMap.foreach(bh.consume(_))

@Benchmark
def hashMapIterator(bh: Blackhole): Unit = {
val it = hashMap.iterator
while (it.hasNext) {
bh.consume(it.next())
}
}

@Benchmark
def scalaMapIterator(bh: Blackhole): Unit = {
val it = scalaMap.iterator
while (it.hasNext) {
bh.consume(it.next())
}
}

@Benchmark
def hashMapConcat(bh: Blackhole): Unit =
bh.consume(hashMap.concat(otherHashMap))

@Benchmark
def scalaMapConcat(bh: Blackhole): Unit =
bh.consume(scalaMap ++ otherScalaMap)

@Benchmark
def hashMapUniversalEquals(bh: Blackhole): Unit =
bh.consume(hashMap == otherHashMap)

@Benchmark
def hashMapEqEquals(bh: Blackhole): Unit =
bh.consume(hashMap === otherHashMap)

@Benchmark
def scalaMapUniversalEquals(bh: Blackhole): Unit =
bh.consume(scalaMap == otherScalaMap)
}
192 changes: 192 additions & 0 deletions bench/src/main/scala-2.13+/cats/bench/HashMapBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.bench

import cats.data.HashMap
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import scala.collection.immutable.{HashMap => SHashMap}

@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class HashMapBench {
@Param(Array("0", "1", "2", "3", "4", "7", "8", "15", "16", "17", "39", "282", "4096", "131070", "7312102"))
var size: Int = _

var hashMap: HashMap[Long, Int] = _
var otherHashMap: HashMap[Long, Int] = _
var scalaMap: SHashMap[Long, Int] = _
var otherScalaMap: SHashMap[Long, Int] = _

def hashMapOfSize(n: Int) = HashMap.fromSeq((1L to (n.toLong)).zipWithIndex)
def scalaMapOfSize(n: Int) = SHashMap.from((1L to (n.toLong)).zipWithIndex)

@Setup(Level.Trial)
def init(): Unit = {
hashMap = hashMapOfSize(size)
otherHashMap = hashMapOfSize(size)
scalaMap = scalaMapOfSize(size)
otherScalaMap = scalaMapOfSize(size)
}

@Benchmark
def hashMapFromSeq(bh: Blackhole): Unit =
bh.consume(hashMapOfSize(size))

@Benchmark
def scalaMapFromSeq(bh: Blackhole): Unit =
bh.consume(scalaMapOfSize(size))

@Benchmark
@OperationsPerInvocation(1000)
def hashMapUpdated(bh: Blackhole): Unit = {
var hs = hashMap
var i = 0
while (i < 1000) {
hs = hs.updated(-i.toLong, i)
i += 1
}
bh.consume(hs)
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapUpdated(bh: Blackhole): Unit = {
var ss = scalaMap
var i = 0
while (i < 1000) {
ss = ss.updated(-i.toLong, i)
i += 1
}
bh.consume(ss)
}

@Benchmark
@OperationsPerInvocation(1000)
def hashMapRemoved(bh: Blackhole): Unit = {
var hs = hashMap
var i = 0L
while (i < 1000L) {
hs = hs.removed(i)
i += 1L
}
bh.consume(hs)
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapRemoved(bh: Blackhole): Unit = {
var ss = scalaMap
var i = 0L
while (i < 1000L) {
ss -= i
i += 1L
}
bh.consume(ss)
}

@Benchmark
@OperationsPerInvocation(1000)
def hashMapContains(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(hashMap.contains(i))
i += 1L
}
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapContains(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(scalaMap.contains(i))
i += 1L
}
}

@Benchmark
@OperationsPerInvocation(1000)
def hashMapGet(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(hashMap.get(i))
i += 1L
}
}

@Benchmark
@OperationsPerInvocation(1000)
def scalaMapGet(bh: Blackhole): Unit = {
var i = 0L
while (i < 1000L) {
bh.consume(scalaMap.get(i))
i += 1L
}
}

@Benchmark
def hashMapForeach(bh: Blackhole): Unit =
hashMap.foreach((k, v) => bh.consume((k, v)))

@Benchmark
def scalaMapForeach(bh: Blackhole): Unit =
scalaMap.foreach(bh.consume(_))

@Benchmark
def hashMapIterator(bh: Blackhole): Unit = {
val it = hashMap.iterator
while (it.hasNext) {
bh.consume(it.next())
}
}

@Benchmark
def scalaMapIterator(bh: Blackhole): Unit = {
val it = scalaMap.iterator
while (it.hasNext) {
bh.consume(it.next())
}
}

@Benchmark
def hashMapConcat(bh: Blackhole): Unit =
bh.consume(hashMap.concat(otherHashMap))

@Benchmark
def scalaMapConcat(bh: Blackhole): Unit =
bh.consume(scalaMap ++ otherScalaMap)

@Benchmark
def hashMapUniversalEquals(bh: Blackhole): Unit =
bh.consume(hashMap == otherHashMap)

@Benchmark
def hashMapEqEquals(bh: Blackhole): Unit =
bh.consume(hashMap === otherHashMap)

@Benchmark
def scalaMapUniversalEquals(bh: Blackhole): Unit =
bh.consume(scalaMap == otherScalaMap)
}
Loading