-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[util-core] Allocate a hash table of sufficient size in MapUtil.newHa…
…shMap Differential Revision: https://phabricator.twitter.biz/D1175381
- Loading branch information
Showing
2 changed files
with
11 additions
and
4 deletions.
There are no files selected for viewing
11 changes: 9 additions & 2 deletions
11
util-core/src/main/scala-2.12-/com/twitter/util/MapUtil.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
package com.twitter.util | ||
|
||
import java.lang.Integer.numberOfLeadingZeros | ||
import scala.collection.mutable | ||
|
||
object MapUtil { | ||
|
||
def newHashMap[K, V](initialCapacity: Int, loadFactor: Double = 0.75): mutable.HashMap[K, V] = { | ||
def newHashMap[K, V](expectedSize: 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 | ||
override protected def initialSize: Int = { | ||
roundUpToNextPowerOfTwo((expectedSize / loadFactor).toInt max 4) | ||
} | ||
} | ||
} | ||
|
||
private[this] def roundUpToNextPowerOfTwo(target: Int): Int = { | ||
1 << -numberOfLeadingZeros(target - 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters