Skip to content

Commit e90fbf3

Browse files
authored
Merge pull request scala/scala#7224 from retronym/topic/champ-iterator-small
Reduce allocations within HashMap iterator for small maps
2 parents 7e7ac18 + 9a2f1f5 commit e90fbf3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

library/src/scala/collection/immutable/ChampCommon.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,14 @@ private[immutable] abstract class ChampBaseIterator[T <: Node[T]] {
105105
protected var currentValueNode: T = _
106106

107107
private[this] var currentStackLevel: Int = -1
108-
private[this] val nodeCursorsAndLengths: Array[Int] = new Array[Int](MaxDepth * 2)
109-
private[this] val nodes: Array[T] = new Array[Node[T]](MaxDepth).asInstanceOf[Array[T]]
108+
private[this] var nodeCursorsAndLengths: Array[Int] = _
109+
private[this] var nodes: Array[T] = _
110+
private def initNodes(): Unit = {
111+
if (nodeCursorsAndLengths eq null) {
112+
nodeCursorsAndLengths = new Array[Int](MaxDepth * 2)
113+
nodes = new Array[Node[T]](MaxDepth).asInstanceOf[Array[T]]
114+
}
115+
}
110116

111117
def this(rootNode: T) = {
112118
this()
@@ -121,6 +127,7 @@ private[immutable] abstract class ChampBaseIterator[T <: Node[T]] {
121127
}
122128

123129
private final def pushNode(node: T): Unit = {
130+
initNodes()
124131
currentStackLevel = currentStackLevel + 1
125132

126133
val cursorIndex = currentStackLevel * 2

0 commit comments

Comments
 (0)