<commit_before>package benchmark

import org.openjdk.jmh.annotations._
import offheap.collection._
import org.openjdk.jmh.infra.Blackhole

import scala.util.Random
import scala.collection.mutable.{OpenHashMap => StdlibMap}

@State(Scope.Thread)
class MapBenchmark {

  val mapSize = 100000
  val random = Random

  val keys: Array[String] = {
    val keys = new Array[String](mapSize)
    0 until mapSize foreach (keys(_) = random.nextString(10))
    keys
  }

  val map: HashMap[String, Int] = {
    val map = new HashMap[String, Int]()
    0 until mapSize foreach (i => map.put(keys(i), i * i))
    map
  }

  val stdMap: StdlibMap[String, Int] = {
    val map = StdlibMap[String, Int]()
    0 until mapSize foreach (i => map.put(keys(i), i * i))
    map
  }

  var randKey: String = _
  var nonexistingKey: String = _

  @Setup(Level.Invocation)
  def setup = {
    randKey = keys(random.nextInt(mapSize))
    nonexistingKey = random.nextString(11)
  }

  @Benchmark
  def get = map(randKey)

  @Benchmark
  def getStdlib = stdMap.get(randKey)

  @Benchmark
  def getNonExisting = map(nonexistingKey)

  @Benchmark
  def getNonExistingStdlib = stdMap.get(nonexistingKey)

  @Benchmark
  def put = {
    val m = new HashMap[String, Int]
    var i = 0
    while (i < mapSize) {
      m.put(keys(i), i)
      i += 1
    }
  }

  @Benchmark
  def putStdlib = {
    val m = StdlibMap[String, Int]()
    var i = 0
    while (i < mapSize) {
      m.put(keys(i), i)
      i += 1
    }
  }

  @Benchmark
  def foreach(blackhole: Blackhole) = map foreach (blackhole.consume(_))

  @Benchmark
  def foreachStdlib(blackhole: Blackhole) =
    stdMap foreach (blackhole.consume(_))

  @Benchmark
  def putRemoveRead(blackhole: Blackhole) = {
    val map = new HashMap[String, Int]()
    var i = 0
    while (i < 10000) { map.put(keys(i), i); i += 1 }
    i = 0
    while (i < 1000) { map.remove(keys(i * 10)); i += 1 }
    i = 0
    while (i < 10000) { blackhole.consume(map(keys(i))); i += 1 }
  }

  @Benchmark
  def putRemoveReadStdlib(blackhole: Blackhole) = {
    val map = new StdlibMap[String, Int]()
    var i = 0
    while (i < 10000) { map.put(keys(i), i); i += 1 }
    i = 0
    while (i < 1000) { map.remove(keys(i * 10)); i += 1 }
    i = 0
    while (i < 10000) { blackhole.consume(map.get(keys(i))); i += 1 }
  }
}

@State(Scope.Thread)
class MapRemoveBenchmark {

  var map: HashMap[String, Int] = _
  val keys: Array[String] = {
    val keys = new Array[String](1000)
    0 until 1000 foreach (i => keys(i) = i toString)
    keys
  }

  @Setup(Level.Invocation)
  def setup = {
    map = new HashMap[String, Int]
    0 until 1000 foreach (i => map.put(keys(i), i * i))
  }

  @Benchmark
  def benchmark = {
    var i = 0
    while (i < 1000) { map.remove(keys(i)); i += 1 }
  }
}

@State(Scope.Thread)
class MapRemoveStdlibBenchmark {

  var map: StdlibMap[String, Int] = _
  val keys: Array[String] = {
    val keys = new Array[String](1000)
    0 until 1000 foreach (i => keys(i) = i toString)
    keys
  }

  @Setup(Level.Invocation)
  def setup = {
    map = StdlibMap[String, Int]()
    0 until 1000 foreach (i => map.put(keys(i), i * i))
  }

  @Benchmark
  def benchmark = {
    var i = 0
    while (i < 1000) { map.remove(keys(i)); i += 1 }
  }
}<commit_msg>Custom key class instead of string in benchmark<commit_after>package benchmark

import org.openjdk.jmh.annotations._
import offheap.collection._
import org.openjdk.jmh.infra.Blackhole

import scala.util.Random
import scala.collection.mutable.{OpenHashMap => StdlibMap}

object MapBenchmark {

  val mapSize = 1000000
  val random = Random

  class Key(val x: Int, val y: Int, val z: Int) {
    override def equals(that: Any) = {
      val t = that.asInstanceOf[Key]
      x == t.x && y == t.y && z == t.z
    }
    override def hashCode = (x * 31 + y) * 31 + z
  }

  object Key {
    @inline
    def generate = new Key(random.nextInt(), random.nextInt(), random.nextInt())
  }

  val keys: Array[Key] = {
    val keys = new Array[Key](mapSize)
    0 until mapSize foreach (keys(_) = Key.generate)
    keys
  }

  val initialMapSize = {
    var size = 1
    while (size < mapSize) size *= 2
    size
  }
}

@State(Scope.Thread)
class MapBenchmark {

  import MapBenchmark._

  val map: HashMap[Key, Int] = {
    val map = new HashMap[Key, Int](initialSize = initialMapSize)
    0 until mapSize foreach (i => map.put(keys(i), i * i))
    map
  }

  val stdMap: StdlibMap[Key, Int] = {
    val map = new StdlibMap[Key, Int](initialSize = initialMapSize)
    0 until mapSize foreach (i => map.put(keys(i), i * i))
    map
  }

  var randKey: Key = _
  var nonExistingKey: Key = _

  @Setup(Level.Invocation)
  def setup = {
    randKey = keys(random.nextInt(mapSize))
    nonExistingKey = Key.generate
  }

  @Benchmark
  def get = map(randKey)

  @Benchmark
  def getStdlib = stdMap.get(randKey)

  @Benchmark
  def getNonExisting = map(nonExistingKey)

  @Benchmark
  def getNonExistingStdlib = stdMap.get(nonExistingKey)

  @Benchmark
  def put = {
    val m = new HashMap[Key, Int](initialSize = initialMapSize)
    var i = 0
    while (i < mapSize) {
      m.put(keys(i), i)
      i += 1
    }
  }

  @Benchmark
  def putStdlib = {
    val m = new StdlibMap[Key, Int](initialSize = initialMapSize)
    var i = 0
    while (i < mapSize) {
      m.put(keys(i), i)
      i += 1
    }
  }

  @Benchmark
  def foreach(blackhole: Blackhole) = map foreach (blackhole.consume(_))

  @Benchmark
  def foreachStdlib(blackhole: Blackhole) =
    stdMap foreach (blackhole.consume(_))

  @Benchmark
  def putRemoveRead(blackhole: Blackhole) = {
    val map = new HashMap[Key, Int](initialSize = initialMapSize)
    var i = 0
    while (i < mapSize) { map.put(keys(i), i); i += 1 }
    i = 0
    while (i < mapSize / 10) { map.remove(keys(i * 10)); i += 1 }
    i = 0
    while (i < mapSize) { blackhole.consume(map(keys(i))); i += 1 }
  }

  @Benchmark
  def putRemoveReadStdlib(blackhole: Blackhole) = {
    val map = new StdlibMap[Key, Int](initialSize = initialMapSize)
    var i = 0
    while (i < mapSize) { map.put(keys(i), i); i += 1 }
    i = 0
    while (i < mapSize / 10) { map.remove(keys(i * 10)); i += 1 }
    i = 0
    while (i < mapSize) { blackhole.consume(map.get(keys(i))); i += 1 }
  }
}

@State(Scope.Thread)
class MapRemoveBenchmark {

  import MapBenchmark._

  var map: HashMap[Key, Int] = _

  @Setup(Level.Invocation)
  def setup = {
    map = new HashMap[Key, Int]
    0 until mapSize foreach (i => map.put(keys(i), i * i))
  }

  @Benchmark
  def benchmark = {
    var i = 0
    while (i < mapSize / 10) { map.remove(keys(i * 10)); i += 1 }
  }
}

@State(Scope.Thread)
class MapRemoveStdlibBenchmark {

  import MapBenchmark._

  var map: StdlibMap[Key, Int] = _

  @Setup(Level.Invocation)
  def setup = {
    map = StdlibMap[Key, Int]()
    0 until mapSize foreach (i => map.put(keys(i), i * i))
  }

  @Benchmark
  def benchmark = {
    var i = 0
    while (i < mapSize / 10) { map.remove(keys(i * 10)); i += 1 }
  }
}
{'fraction_non_alphanumeric': 0.13782279925809673, 'fraction_numerical': 0.022114424311599372, 'mean_word_length': 2.8035811177428105, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1550132', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2595, 'n_words': 688}