Skip to content

Commit b44e3e5

Browse files
committed
consider structural sharing
1 parent 5320f0b commit b44e3e5

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

library/src/scala/collection/immutable/RedBlackTree.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ private[collection] object RedBlackTree {
848848
}
849849

850850
private[this] def _union[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
851-
if(t1 eq null) t2
851+
if((t1 eq null) || (t1 eq t2)) t2
852852
else if(t2 eq null) t1
853853
else {
854854
val (l1, _, r1) = split(t1, t2.key)
@@ -859,6 +859,7 @@ private[collection] object RedBlackTree {
859859

860860
private[this] def _intersect[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
861861
if((t1 eq null) || (t2 eq null)) null
862+
else if (t1 eq t2) t1
862863
else {
863864
val (l1, b, r1) = split(t1, t2.key)
864865
val tl = _intersect(l1, t2.left)
@@ -869,6 +870,7 @@ private[collection] object RedBlackTree {
869870

870871
private[this] def _difference[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
871872
if((t1 eq null) || (t2 eq null)) t1
873+
else if (t1 eq t2) null
872874
else {
873875
val (l1, _, r1) = split(t1, t2.key)
874876
val tl = _difference(l1, t2.left)

0 commit comments

Comments
 (0)