Skip to content

Commit 7a99a04

Browse files
mbezoyanjenkins
authored andcommitted
[scrooge] Preallocate hashmaps during deserialization
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
1 parent 22eec97 commit 7a99a04

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.twitter.util
2+
3+
import scala.collection.mutable
4+
5+
object MapUtil {
6+
7+
def newHashMap[K, V](initialCapacity: Int, loadFactor: Double = 0.75): mutable.HashMap[K, V] = {
8+
new mutable.HashMap[K, V]() {
9+
this._loadFactor = (loadFactor * 1000).toInt
10+
override protected val initialSize: Int = (size.toLong / loadFactor).toInt
11+
}
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.twitter.util
2+
3+
import scala.collection.mutable
4+
5+
object MapUtil {
6+
7+
def newHashMap[K, V](initialCapacity: Int, loadFactor: Double = 0.75): mutable.HashMap[K, V] = {
8+
new mutable.HashMap[K, V](initialCapacity, loadFactor)
9+
}
10+
}

0 commit comments

Comments
 (0)