title: For Scala 2.13, what is the fastest method for updating a LongMap, HashMap, or TrieMap with millions of updates?
body: <p><strong>Goal</strong></p>
  
  <p>I have a mutable Map[Long, Long] with millions of entries. I need to make many iterations of updates with millions of updates. I would like to do this as fast as possible.</p>
  
  <p><strong>Background</strong></p>
  
  <p>Currently, the fastest method is to use a single threaded mutable.LongMap[Long]. This type is optimized for Long types as the key.</p>
  
  <p>Other map types appear to be slower -- but I may have implemented them incorrectly as I was trying to do the updates concurrently and/or in parallel without success. It is possible that updating a map in parallel is not actually occurring or is not possible in Scala.</p>
  
  <p>In order of fastest to slowest:</p>
  
  <ol>
  <li>LongMap[Long] (from above)</li>
  <li>TrieMap[Long, Long]</li>
  <li>ParTrieMap[Long, Long]</li>
  <li>HashMap[Long, Long]</li>
  <li>ParHashMap[Long, Long]</li>
  <li>ParMap[Long, Long]</li>
  </ol>
  
  <p>It is OK if a faster method is not mutable, but I do not think this will be the case. A mutable map is probably best for this use case.</p>
  
  <p><strong>Code to generate test data and time the test</strong></p>
  
  <pre><code>import java.util.Calendar
  import scala.collection.mutable
  
  object DictSpeedTest2 {
  
    //helper constants
    val million: Long = 1000000
    val billion: Long = million * 1000
  
    //config
    val garbageCollectionWait = 3
    val numEntries: Long = million * 10 //may need to increase JVM memory with something like: -Xmx32g
    val maxValue: Long = billion * million // max Long = 9223372036854775807L
                                           // this is       1000000000000000L
  
    def main(args: Array[String]): Unit = {
      //generate random data; initial entries in a; updates in b
      val a = genData(numEntries, maxValue, seed = 1000)
      val b = genData(numEntries, maxValue, seed = 9999)
  
      //initialization
      val dict = new mutable.LongMap[Long]()
      a.foreach(x =&gt; dict += (x._1 -&gt; x._2))
  
      //run and time test
      println("start test: " + Calendar.getInstance().getTime)
      val start = System.currentTimeMillis
      b.foreach(x =&gt; dict += (x._1 -&gt; x._2)) //updates
      val end = System.currentTimeMillis
  
      //print runtime
      val durationInSeconds = (end - start).toFloat / 1000 + "s"
      println("end test:  " + Calendar.getInstance().getTime + " -- " + durationInSeconds)
    }
  
    def genData(n: Long, max: Long, seed: Long): Array[(Long, Long)] = {
      val r = scala.util.Random
      r.setSeed(seed) //deterministic generation of arrays
      val a = new Array[(Long, Long)](n.toInt)
      a.map(_ =&gt; (r.nextInt(), r.nextInt()) )
    }
  }
  </code></pre>
  
  <p><strong>Current timings</strong></p>
  
  <p>LongMap[Long] with the above code completes in the following times on my 2018 MacBook Pro:</p>
  
  <ul>
  <li>~3.5 seconds with numEntries = 10 million</li>
  <li>~100 seconds with numEntries = 100 million</li>
  </ul>