Skip to content

Commit 54b0730

Browse files
authored
Merge pull request scala/scala#8979 from mkeskells/2.12.x_RB_set_ops
Optimise TreeSet/TreeMap set operations for structural sharing
2 parents a8a9a0c + b44e3e5 commit 54b0730

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
@@ -850,7 +850,7 @@ private[collection] object RedBlackTree {
850850
}
851851

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

862862
private[this] def _intersect[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
863863
if((t1 eq null) || (t2 eq null)) null
864+
else if (t1 eq t2) t1
864865
else {
865866
val (l1, b, r1) = split(t1, t2.key)
866867
val tl = _intersect(l1, t2.left)
@@ -871,6 +872,7 @@ private[collection] object RedBlackTree {
871872

872873
private[this] def _difference[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
873874
if((t1 eq null) || (t2 eq null)) t1
875+
else if (t1 eq t2) null
874876
else {
875877
val (l1, _, r1) = split(t1, t2.key)
876878
val tl = _difference(l1, t2.left)

0 commit comments

Comments
 (0)