Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit b955281

Browse files
committed
use sizeHintIfCheap in insertAll
1 parent e418495 commit b955281

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/main/scala/strawman/collection/mutable/ArrayDeque.scala

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package strawman
1+
package scala
22
package collection.mutable
33

44
import scala.collection.{GenSeq, generic, mutable}
@@ -80,20 +80,26 @@ class ArrayDeque[A] private(var array: Array[AnyRef], var start: Int, var end: I
8080
override def insertAll(idx: Int, elems: scala.collection.Traversable[A]): Unit = {
8181
ArrayDeque.checkIndex(idx, this)
8282
if (elems.isEmpty) return
83-
val srcLength = elems.size
84-
val finalLength = srcLength + this.length
85-
// Either we resize right away or move prefix right or suffix left
86-
if (2*finalLength >= array.length - 1) {
87-
val array2 = ArrayDeque.alloc(finalLength)
88-
copySliceToArray(srcStart = 0, dest = array2, destStart = 0, maxItems = idx)
89-
elems.copyToArray(array2.asInstanceOf[Array[A]], idx)
90-
copySliceToArray(srcStart = idx, dest = array2, destStart = idx + srcLength, maxItems = size)
91-
set(array = array2, start = 0, end = finalLength)
92-
} else {
93-
val suffix = drop(idx)
94-
end = (start + idx) & mask
95-
elems.foreach(appendAssumingCapacity)
96-
suffix.foreach(appendAssumingCapacity)
83+
elems.sizeHintIfCheap match {
84+
case srcLength if srcLength >= 0 =>
85+
val finalLength = srcLength + this.length
86+
// Either we resize right away or move prefix right or suffix left
87+
if (2*finalLength >= array.length - 1) {
88+
val array2 = ArrayDeque.alloc(finalLength)
89+
copySliceToArray(srcStart = 0, dest = array2, destStart = 0, maxItems = idx)
90+
elems.copyToArray(array2.asInstanceOf[Array[A]], idx)
91+
copySliceToArray(srcStart = idx, dest = array2, destStart = idx + srcLength, maxItems = size)
92+
set(array = array2, start = 0, end = finalLength)
93+
} else {
94+
val suffix = drop(idx)
95+
end = (start + idx) & mask
96+
elems.foreach(appendAssumingCapacity)
97+
suffix.foreach(appendAssumingCapacity)
98+
}
99+
case _ => //expensive to compute size
100+
val suffix = drop(idx)
101+
end = (start + idx) & mask
102+
this ++= elems ++= suffix
97103
}
98104
}
99105

@@ -257,7 +263,7 @@ class ArrayDeque[A] private(var array: Array[AnyRef], var start: Int, var end: I
257263
private[this] def ensureCapacity() = {
258264
/* We resize when we are 1 element short intentionally (and not when array is actually full)
259265
* This is because when array is full, start is equal to end (which is also true when array is empty)
260-
* Making it hard to distinguish between the full and empty case*/
266+
* making it hard to distinguish between the full and empty cases */
261267
if (size == array.length - 1) resize(array.length)
262268
}
263269

src/test/scala/strawman/collection/test/ArrayDequeSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package strawman
22
package collection.test
33

4-
import strawman.collection.mutable.ArrayDeque
4+
import scala.collection.mutable.ArrayDeque
55

66
import org.scalatest._
77

0 commit comments

Comments
 (0)