Skip to content

Commit 5220119

Browse files
committed
Restore delegation of sizeHint
1 parent 9b42170 commit 5220119

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

library/src/scala/collection/mutable/ArrayBuffer.scala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,12 @@ object ArrayBuffer extends StrictOptimizedSeqFactory[ArrayBuffer] {
299299
else new ArrayBuffer[B] ++= coll
300300
}
301301

302-
def newBuilder[A]: Builder[A, ArrayBuffer[A]] = new GrowableBuilder[A, ArrayBuffer[A]](empty[A])
302+
def newBuilder[A]: Builder[A, ArrayBuffer[A]] = new GrowableBuilder[A, ArrayBuffer[A]](empty[A]) {
303+
override def sizeHint(size: Int): Unit = elems.sizeHint(size)
304+
}
303305

304306
def empty[A]: ArrayBuffer[A] = new ArrayBuffer[A]()
305307

306-
@inline private def checkArrayLengthLimit(length: Int, currentLength: Int): Unit =
307-
if (length > VM_MaxArraySize)
308-
throw new Exception(s"Array of array-backed collection exceeds VM length limit of $VM_MaxArraySize. Requested length: $length; current length: $currentLength")
309-
else if (length < 0)
310-
throw new Exception(s"Overflow while resizing array of array-backed collection. Requested length: $length; current length: $currentLength; increase: ${length - currentLength}")
311-
312308
/**
313309
* The increased size for an array-backed collection.
314310
*
@@ -317,13 +313,19 @@ object ArrayBuffer extends StrictOptimizedSeqFactory[ArrayBuffer] {
317313
* @return
318314
* - `-1` if no resizing is needed, else
319315
* - `VM_MaxArraySize` if `arrayLen` is too large to be doubled, else
320-
* - `max(targetLen, arrayLen * 2, , DefaultInitialSize)`.
316+
* - `max(targetLen, arrayLen * 2, DefaultInitialSize)`.
321317
* - Throws an exception if `targetLen` exceeds `VM_MaxArraySize` or is negative (overflow).
322318
*/
323319
private[mutable] def resizeUp(arrayLen: Int, targetLen: Int): Int = {
320+
def checkArrayLengthLimit(): Unit =
321+
if (targetLen > VM_MaxArraySize)
322+
throw new Exception(s"Array of array-backed collection exceeds VM length limit of $VM_MaxArraySize. Requested length: $targetLen; current length: $arrayLen")
323+
else if (targetLen < 0)
324+
throw new Exception(s"Overflow while resizing array of array-backed collection. Requested length: $targetLen; current length: $arrayLen; increase: ${targetLen - arrayLen}")
325+
324326
if (targetLen > 0 && targetLen <= arrayLen) -1
325327
else {
326-
checkArrayLengthLimit(targetLen, arrayLen)
328+
checkArrayLengthLimit()
327329
if (arrayLen > VM_MaxArraySize / 2) VM_MaxArraySize
328330
else math.max(targetLen, math.max(arrayLen * 2, DefaultInitialSize))
329331
}

0 commit comments

Comments
 (0)