From 05a6102a99a80f3b87966843c486e25c03351f63 Mon Sep 17 00:00:00 2001 From: Mikhail Bezoyan Date: Fri, 4 Oct 2024 15:56:49 +0000 Subject: [PATCH] [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 --- .../scala/com/twitter/scrooge/internal/TProtocols.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scrooge-core/src/main/scala/com/twitter/scrooge/internal/TProtocols.scala b/scrooge-core/src/main/scala/com/twitter/scrooge/internal/TProtocols.scala index b41241d9..be698227 100644 --- a/scrooge-core/src/main/scala/com/twitter/scrooge/internal/TProtocols.scala +++ b/scrooge-core/src/main/scala/com/twitter/scrooge/internal/TProtocols.scala @@ -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 @@ -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 }