Skip to content

Commit

Permalink
[scrooge] Preallocate hashmaps during deserialization
Browse files Browse the repository at this point in the history
Problem
When a map is deserialized in scrooge we create it with default capacity, which means that for larger maps we need to resize it multiple times and each time recalculate key hashes

Solution
Allocate a map with sufficient capacity from the start.

Differential Revision: https://phabricator.twitter.biz/D1174445
  • Loading branch information
mbezoyan authored and jenkins committed Oct 4, 2024
1 parent 22eec97 commit 7a99a04
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions util-core/src/main/scala-2.12-/com/twitter/util/MapUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.twitter.util

import scala.collection.mutable

object MapUtil {

def newHashMap[K, V](initialCapacity: Int, loadFactor: Double = 0.75): mutable.HashMap[K, V] = {
new mutable.HashMap[K, V]() {
this._loadFactor = (loadFactor * 1000).toInt
override protected val initialSize: Int = (size.toLong / loadFactor).toInt
}
}
}
10 changes: 10 additions & 0 deletions util-core/src/main/scala-2.13+/com/twitter/util/MapUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.twitter.util

import scala.collection.mutable

object MapUtil {

def newHashMap[K, V](initialCapacity: Int, loadFactor: Double = 0.75): mutable.HashMap[K, V] = {
new mutable.HashMap[K, V](initialCapacity, loadFactor)
}
}

0 comments on commit 7a99a04

Please sign in to comment.