Skip to content

Commit c54a92c

Browse files
committed
Audit sizeHint
1 parent b32c9dd commit c54a92c

12 files changed

+15
-26
lines changed

library/src/scala/collection/ArrayOps.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,8 +1174,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
11741174
/** A copy of this array with all elements of a collection appended. */
11751175
def appendedAll[B >: A : ClassTag](suffix: IterableOnce[B]): Array[B] = {
11761176
val b = ArrayBuilder.make[B]
1177-
val k = suffix.knownSize
1178-
if(k >= 0) b.sizeHint(k + xs.length)
1177+
b.sizeHint(suffix, delta = xs.length)
11791178
b.addAll(xs)
11801179
b.addAll(suffix)
11811180
b.result()

library/src/scala/collection/Factory.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object Factory {
6161
private class ArrayFactory[A: ClassTag] extends Factory[A, Array[A]] with Serializable {
6262
def fromSpecific(it: IterableOnce[A]): Array[A] = {
6363
val b = newBuilder
64-
b.sizeHint(scala.math.max(0, it.knownSize))
64+
b.sizeHint(it, delta = 0)
6565
b ++= it
6666
b.result()
6767
}

library/src/scala/collection/StrictOptimizedSeqOps.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,15 @@ trait StrictOptimizedSeqOps [+A, +CC[_], +C]
3434

3535
override def prepended[B >: A](elem: B): CC[B] = {
3636
val b = iterableFactory.newBuilder[B]
37-
if (knownSize >= 0) {
38-
b.sizeHint(size + 1)
39-
}
37+
b.sizeHint(this, delta = 1)
4038
b += elem
4139
b ++= this
4240
b.result()
4341
}
4442

4543
override def appended[B >: A](elem: B): CC[B] = {
4644
val b = iterableFactory.newBuilder[B]
47-
if (knownSize >= 0) {
48-
b.sizeHint(size + 1)
49-
}
45+
b.sizeHint(this, delta = 1)
5046
b ++= this
5147
b += elem
5248
b.result()

library/src/scala/collection/immutable/StrictOptimizedSeqOps.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ package scala
1414
package collection
1515
package immutable
1616

17-
/**
18-
* Trait that overrides operations to take advantage of strict builders.
19-
*/
17+
/** Trait that overrides operations to take advantage of strict builders.
18+
*/
2019
trait StrictOptimizedSeqOps[+A, +CC[_], +C]
2120
extends Any
2221
with SeqOps[A, CC, C]
@@ -41,9 +40,7 @@ trait StrictOptimizedSeqOps[+A, +CC[_], +C]
4140
override def updated[B >: A](index: Int, elem: B): CC[B] = {
4241
if (index < 0) throw new IndexOutOfBoundsException(s"$index is out of bounds (min 0, max ${if (knownSize>=0) knownSize else "unknown"})")
4342
val b = iterableFactory.newBuilder[B]
44-
if (knownSize >= 0) {
45-
b.sizeHint(size)
46-
}
43+
b.sizeHint(this)
4744
var i = 0
4845
val it = iterator
4946
while (i < index && it.hasNext) {

library/src/scala/collection/immutable/WrappedString.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ final class WrappedString(private val self: String) extends AbstractSeq[Char] wi
125125
object WrappedString extends SpecificIterableFactory[Char, WrappedString] {
126126
def fromSpecific(it: IterableOnce[Char]): WrappedString = {
127127
val b = newBuilder
128-
val s = it.knownSize
129-
if(s >= 0) b.sizeHint(s)
128+
b.sizeHint(it)
130129
b ++= it
131130
b.result()
132131
}

library/src/scala/collection/mutable/ArraySeq.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ sealed abstract class ArraySeq[T]
4343

4444
override protected def fromSpecific(coll: scala.collection.IterableOnce[T]): ArraySeq[T] = {
4545
val b = ArrayBuilder.make(elemTag).asInstanceOf[ArrayBuilder[T]]
46-
val s = coll.knownSize
47-
if(s > 0) b.sizeHint(s)
46+
b.sizeHint(coll, delta = 0)
4847
b ++= coll
4948
ArraySeq.make(b.result())
5049
}

library/src/scala/collection/mutable/CollisionProofHashMap.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ final class CollisionProofHashMap[K, V](initialCapacity: Int, loadFactor: Double
174174
}
175175

176176
override def addAll(xs: IterableOnce[(K, V)]): this.type = {
177-
val k = xs.knownSize
178-
if(k > 0) sizeHint(contentSize + k)
177+
sizeHint(xs, delta = contentSize)
179178
super.addAll(xs)
180179
}
181180

library/src/scala/collection/mutable/HashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class HashMap[K, V](initialCapacity: Int, loadFactor: Double)
9595
}
9696

9797
override def addAll(xs: IterableOnce[(K, V)]): this.type = {
98-
sizeHint(xs.knownSize)
98+
sizeHint(xs)
9999

100100
xs match {
101101
case hm: immutable.HashMap[K, V] =>

library/src/scala/collection/mutable/HashSet.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ final class HashSet[A](initialCapacity: Int, loadFactor: Double)
9191
}
9292

9393
override def addAll(xs: IterableOnce[A]): this.type = {
94-
sizeHint(xs.knownSize)
94+
sizeHint(xs, delta = 0)
9595
xs match {
9696
case hs: immutable.HashSet[A] =>
9797
hs.foreachWithHash((k, h) => addElem(k, improveHash(h)))

library/src/scala/collection/mutable/LinkedHashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ object LinkedHashMap extends MapFactory[LinkedHashMap] {
480480

481481
def from[K, V](it: collection.IterableOnce[(K, V)]) = {
482482
val newlhm = empty[K, V]
483-
newlhm.sizeHint(it.knownSize)
483+
newlhm.sizeHint(it, delta = 0)
484484
newlhm.addAll(it)
485485
newlhm
486486
}

0 commit comments

Comments
 (0)