Skip to content

Commit 1f61034

Browse files
committed
Review cleanups
1 parent 4170e21 commit 1f61034

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

library/src/scala/collection/IterableOnce.scala

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,6 @@ object IterableOnce {
283283
case src: Iterable[A] => src.copyToArray[B](xs, start, len)
284284
case src => src.iterator.copyToArray[B](xs, start, len)
285285
}
286-
287-
@inline private[collection] def checkArraySizeWithinVMLimit(size: Int): Unit = {
288-
import scala.runtime.PStatics.VM_MaxArraySize
289-
if (size > VM_MaxArraySize) {
290-
throw new Exception(s"Size of array-backed collection exceeds VM array size limit of $VM_MaxArraySize. Encountered size: $size")
291-
} else if (size < 0) {
292-
throw new Exception(s"Size of array-backed collection must exceed 0. Encountered size: $size")
293-
}
294-
}
295286
}
296287

297288
/** This implementation trait can be mixed into an `IterableOnce` to get the basic methods that are shared between

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ package collection
1515
package mutable
1616

1717
import java.util.Arrays
18-
19-
import scala.annotation.nowarn
20-
import scala.annotation.tailrec
18+
import scala.annotation.{nowarn, tailrec}
2119
import scala.collection.Stepper.EfficientSplit
2220
import scala.collection.generic.DefaultSerializable
21+
import scala.runtime.PStatics.VM_MaxArraySize
2322

2423
/** An implementation of the `Buffer` class using an array to
2524
* represent the assembled sequence internally. Append, update and random
@@ -307,24 +306,29 @@ object ArrayBuffer extends StrictOptimizedSeqFactory[ArrayBuffer] {
307306

308307
def empty[A]: ArrayBuffer[A] = new ArrayBuffer[A]()
309308

310-
import scala.runtime.PStatics.VM_MaxArraySize
309+
@inline private def checkArrayLengthLimit(length: Int, currentLength: Int): Unit =
310+
if (length > VM_MaxArraySize)
311+
throw new Exception(s"Array of array-backed collection exceeds VM length limit of $VM_MaxArraySize. Requested length: $length; current length: $currentLength")
312+
else if (length < 0)
313+
throw new Exception(s"Overflow while resizing array of array-backed collection. Requested length: $length; current length: $currentLength; increase: ${length - currentLength}")
314+
311315
/**
316+
* The increased size for an array-backed collection.
317+
*
312318
* @param arrayLen the length of the backing array
313319
* @param targetLen the minimum length to resize up to
314-
* @return -1 if no resizing is needed, the greater of targetLen and 2 * arrayLen if neither exceeds VM_MaxArraySize,
315-
* or VM_MaxArraySize otherwise.
320+
* @return
321+
* - `-1` if no resizing is needed, else
322+
* - `VM_MaxArraySize` if `arrayLen` is too large to be doubled, else
323+
* - `max(targetLen, arrayLen * 2, , DefaultInitialSize)`.
324+
* - Throws an exception if `targetLen` exceeds `VM_MaxArraySize` or is negative (overflow).
316325
*/
317326
private[mutable] def resizeUp(arrayLen: Int, targetLen: Int): Int = {
318-
// Hybrid
319327
if (targetLen > 0 && targetLen <= arrayLen) -1
320328
else {
321-
IterableOnce.checkArraySizeWithinVMLimit(targetLen) // safe because `targetSize <= Int.MaxValue`
322-
323-
math.min(
324-
if (targetLen > (Int.MaxValue >> 1)) VM_MaxArraySize
325-
else math.max(targetLen, math.max(arrayLen << 1, DefaultInitialSize)),
326-
VM_MaxArraySize
327-
)
329+
checkArrayLengthLimit(targetLen, arrayLen)
330+
if (arrayLen > VM_MaxArraySize / 2) VM_MaxArraySize
331+
else math.max(targetLen, math.max(arrayLen * 2, DefaultInitialSize))
328332
}
329333
}
330334

library/src/scala/collection/mutable/ArrayBuilder.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,21 @@ object ArrayBuilder {
488488
protected def elems: Array[Unit] = throw new UnsupportedOperationException()
489489

490490
def addOne(elem: Unit): this.type = {
491-
val newSize:Int = size + 1
491+
val newSize = size + 1
492492
ensureSize(newSize)
493493
size = newSize
494494
this
495495
}
496496

497497
override def addAll(xs: IterableOnce[Unit]): this.type = {
498-
val newSize:Int = size + xs.iterator.size
498+
val newSize = size + xs.iterator.size
499499
ensureSize(newSize)
500500
size = newSize
501501
this
502502
}
503503

504504
override def addAll(xs: Array[_ <: Unit], offset: Int, length: Int): this.type = {
505-
val newSize: Int = size + length
505+
val newSize = size + length
506506
ensureSize(newSize)
507507
size = newSize
508508
this
@@ -520,9 +520,8 @@ object ArrayBuilder {
520520
case _ => false
521521
}
522522

523-
protected[this] def resize(size: Int): Unit = ()
523+
protected[this] def resize(size: Int): Unit = capacity = size
524524

525525
override def toString = "ArrayBuilder.ofUnit"
526526
}
527527
}
528-

library/src/scala/runtime/PStatics.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ package scala.runtime
1515
// things that should be in `Statics`, but can't be yet for bincompat reasons
1616
// TODO 3.T: move to `Statics`
1717
private[scala] object PStatics {
18-
final val VM_MaxArraySize:Int = 2147483639 // `Int.MaxValue - 8` traditional soft limit to maximize compatibility with diverse JVMs.
18+
// `Int.MaxValue - 8` traditional soft limit to maximize compatibility with diverse JVMs
19+
// See https://stackoverflow.com/a/8381338 for example
20+
final val VM_MaxArraySize = 2147483639
1921
}

0 commit comments

Comments
 (0)