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 1cbcb4a commit 05a6102
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.twitter.scrooge.internal
import com.twitter.scrooge.TFieldBlob
import com.twitter.scrooge.ThriftEnum
import com.twitter.scrooge.ThriftUnion
import com.twitter.util.MapUtil
import java.nio.ByteBuffer
import java.util.function.ObjDoubleConsumer
import java.util.function.ObjLongConsumer
Expand Down Expand Up @@ -68,18 +69,19 @@ final class TProtocols private[TProtocols] {
readValue: TProtocol => V
): collection.Map[K, V] = {
val tmap = protocol.readMapBegin()
if (tmap.size == 0) {
val size = tmap.size
if (size == 0) {
protocol.readMapEnd()
Map.empty[K, V]
} else {
val map = new mutable.HashMap[K, V]()
val map = MapUtil.newHashMap[K, V](size)
var i = 0
do {
val key = readKey(protocol)
val value = readValue(protocol)
map(key) = value
i += 1
} while (i < tmap.size)
} while (i < size)
protocol.readMapEnd()
map
}
Expand Down

0 comments on commit 05a6102

Please sign in to comment.