diff options
author | Paul Phillips <paulp@improving.org> | 2012-11-30 05:19:07 +0100 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-12-27 15:47:31 -0800 |
commit | 1697132ec8e0df21c98a1420d186c58e02af69ab (patch) | |
tree | 808b0728c868d627873eca3a28acd5832d5e2b3e /src/library/scala/collection/generic | |
parent | d3099c0d3ef363f4f1815409051da2edfec81f30 (diff) | |
download | scala-1697132ec8e0df21c98a1420d186c58e02af69ab.tar.gz scala-1697132ec8e0df21c98a1420d186c58e02af69ab.tar.bz2 scala-1697132ec8e0df21c98a1420d186c58e02af69ab.zip |
Eliminate allocations in Growable.
Diffstat (limited to 'src/library/scala/collection/generic')
-rw-r--r-- | src/library/scala/collection/generic/Growable.scala | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/library/scala/collection/generic/Growable.scala b/src/library/scala/collection/generic/Growable.scala index cb75212e3d..52a0d32de1 100644 --- a/src/library/scala/collection/generic/Growable.scala +++ b/src/library/scala/collection/generic/Growable.scala @@ -6,10 +6,11 @@ ** |/ ** \* */ - package scala.collection package generic +import scala.annotation.tailrec + /** This trait forms part of collections that can be augmented * using a `+=` operator and that can be cleared of all elements using * a `clear` method. @@ -45,7 +46,19 @@ trait Growable[-A] extends Clearable { * @param xs the TraversableOnce producing the elements to $add. * @return the $coll itself. */ - def ++=(xs: TraversableOnce[A]): this.type = { xs.seq foreach += ; this } + def ++=(xs: TraversableOnce[A]): this.type = { + @tailrec def loop(xs: collection.LinearSeq[A]) { + if (xs.nonEmpty) { + this += xs.head + loop(xs.tail) + } + } + xs.seq match { + case xs: collection.LinearSeq[_] => loop(xs) + case xs => xs foreach += + } + this + } /** Clears the $coll's contents. After this operation, the * $coll is empty. |