Skip to content

Commit 282d5bc

Browse files
committed
Override TreeMap.{apply,contains} to avoid allocation
1 parent 5d9a65d commit 282d5bc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

library/src/scala/collection/immutable/TreeMap.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ final class TreeMap[K, +V] private (private val tree: RB.Tree[K, V])(implicit va
132132
else resultOrNull.value
133133
}
134134

135+
// override for performance -- no Some allocation
136+
override def apply(key: K): V = {
137+
val resultOrNull = RB.lookup(tree, key)
138+
if (resultOrNull eq null) default(key)
139+
else resultOrNull.value
140+
}
141+
142+
// override for performance -- no Some allocation
143+
override def contains(key: K): Boolean = RB.contains(tree, key)
144+
135145
def removed(key: K): TreeMap[K,V] =
136146
newMapOrSelf(RB.delete(tree, key))
137147

0 commit comments

Comments
 (0)