Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static int numWords(long numBits) {
throw new IllegalArgumentException("numBits must be positive, but got " + numBits);
}
long numWords = (long) Math.ceil(numBits / 64.0);
if (numWords > Integer.MAX_VALUE) {
if (numWords > Integer.MAX_VALUE - 8) {
throw new IllegalArgumentException("Can't allocate enough space for " + numBits + " bits");
}
return (int) numWords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class LongArray {
private final long length;

public LongArray(MemoryBlock memory) {
assert memory.size() < (long) Integer.MAX_VALUE * 8: "Array size > 4 billion elements";
assert memory.size() < (long) Integer.MAX_VALUE * 8: "Array size > 2.1 billion elements";
Copy link

@ascii766164696D ascii766164696D Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert memory.size() <= (long) (Integer.MAX_VALUE - 8) * 8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the same assert below?

public static MemoryBlock fromLongArray(final long[] array) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also add the exact number in the error message instead of 2.1 billion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure whether the JVM array size limit applied here, because this represents a non-native array. Still wanted to fix the comment as I saw it. If an array exists, its length is valid, so didn't think that part of MemoryBlock represented an issue.

this.memory = memory;
this.baseObj = memory.getBaseObject();
this.baseOffset = memory.getBaseOffset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public interface HashMapGrowthStrategy {
HashMapGrowthStrategy DOUBLING = new Doubling();

class Doubling implements HashMapGrowthStrategy {

private static final int ARRAY_MAX = Integer.MAX_VALUE - 8;
Copy link

@ascii766164696D ascii766164696D Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a comment why this value is chosen as the max
Like here
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/ArrayList.java#l223


@Override
public int nextCapacity(int currentCapacity) {
assert (currentCapacity > 0);
int doubleCapacity = currentCapacity * 2;
// Guard against overflow
return (currentCapacity * 2 > 0) ? (currentCapacity * 2) : Integer.MAX_VALUE;
return (doubleCapacity > 0 && doubleCapacity <= ARRAY_MAX) ? doubleCapacity : ARRAY_MAX;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private class EncryptedBlockData(
// This is used by the block transfer service to replicate blocks. The upload code reads
// all bytes into memory to send the block to the remote executor, so it's ok to do this
// as long as the block fits in a Java array.
assert(blockSize <= Int.MaxValue, "Block is too large to be wrapped in a byte buffer.")
assert(blockSize <= Int.MaxValue - 8, "Block is too large to be wrapped in a byte buffer.")
val dst = ByteBuffer.allocate(blockSize.toInt)
val in = open()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,20 @@ private[spark] class CompactBuffer[T: ClassTag] extends Seq[T] with Serializable

/** Increase our size to newSize and grow the backing array if needed. */
private def growToSize(newSize: Int): Unit = {
if (newSize < 0) {
throw new UnsupportedOperationException("Can't grow buffer past Int.MaxValue elements")
val arrayMax = Int.MaxValue - 8
if (newSize < 0 || newSize - 2 > arrayMax) {
throw new UnsupportedOperationException(s"Can't grow buffer past $arrayMax elements")
}
val capacity = if (otherElements != null) otherElements.length + 2 else 2
if (newSize > capacity) {
var newArrayLen = 8
var newArrayLen = 8L
while (newSize - 2 > newArrayLen) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove - 2 now since newArrayLen is a Long

Copy link

@ascii766164696D ascii766164696D Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see that it's reserved, wasn't clear to me why - 2, seems like a magic number to me, I see it in other places too.

newArrayLen *= 2
if (newArrayLen == Int.MinValue) {
// Prevent overflow if we double from 2^30 to 2^31, which will become Int.MinValue.
// Note that we set the new array length to Int.MaxValue - 2 so that our capacity
// calculation above still gives a positive integer.
newArrayLen = Int.MaxValue - 2
}
}
val newArray = new Array[T](newArrayLen)
if (newArrayLen > arrayMax) {
newArrayLen = arrayMax
}
val newArray = new Array[T](newArrayLen.toInt)
if (otherElements != null) {
System.arraycopy(otherElements, 0, newArray, 0, otherElements.length)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private[spark] class PartitionedPairBuffer[K, V](initialCapacity: Int = 64)
throw new IllegalStateException(s"Can't insert more than ${MAXIMUM_CAPACITY} elements")
}
val newCapacity =
if (capacity * 2 < 0 || capacity * 2 > MAXIMUM_CAPACITY) { // Overflow
if (capacity * 2 > MAXIMUM_CAPACITY) { // Overflow
MAXIMUM_CAPACITY
} else {
capacity * 2
Expand Down Expand Up @@ -96,5 +96,5 @@ private[spark] class PartitionedPairBuffer[K, V](initialCapacity: Int = 64)
}

private object PartitionedPairBuffer {
val MAXIMUM_CAPACITY = Int.MaxValue / 2 // 2 ^ 30 - 1
val MAXIMUM_CAPACITY = (Int.MaxValue - 8) / 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth adding the Int type even though it's already an Int.

Also the comment at the line 28 should be changed to 1073741819 i.e. - 8/2

}
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ object DenseMatrix {
*/
@Since("2.0.0")
def zeros(numRows: Int, numCols: Int): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, new Array[Double](numRows * numCols))
}
Expand All @@ -497,7 +497,7 @@ object DenseMatrix {
*/
@Since("2.0.0")
def ones(numRows: Int, numCols: Int): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(1.0))
}
Expand Down Expand Up @@ -527,7 +527,7 @@ object DenseMatrix {
*/
@Since("2.0.0")
def rand(numRows: Int, numCols: Int, rng: Random): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextDouble()))
}
Expand All @@ -541,7 +541,7 @@ object DenseMatrix {
*/
@Since("2.0.0")
def randn(numRows: Int, numCols: Int, rng: Random): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextGaussian()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ class Word2Vec extends Serializable with Logging {
val newSentences = sentences.repartition(numPartitions).cache()
val initRandom = new XORShiftRandom(seed)

if (vocabSize.toLong * vectorSize >= Int.MaxValue) {
if (vocabSize.toLong * vectorSize >= Int.MaxValue - 8) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be just >

throw new RuntimeException("Please increase minCount or decrease vectorSize in Word2Vec" +
" to avoid an OOM. You are highly recommended to make your vocabSize*vectorSize, " +
"which is " + vocabSize + "*" + vectorSize + " for now, less than `Int.MaxValue`.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ object DenseMatrix {
*/
@Since("1.3.0")
def zeros(numRows: Int, numCols: Int): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, new Array[Double](numRows * numCols))
}
Expand All @@ -469,7 +469,7 @@ object DenseMatrix {
*/
@Since("1.3.0")
def ones(numRows: Int, numCols: Int): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(1.0))
}
Expand Down Expand Up @@ -499,7 +499,7 @@ object DenseMatrix {
*/
@Since("1.3.0")
def rand(numRows: Int, numCols: Int, rng: Random): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextDouble()))
}
Expand All @@ -513,7 +513,7 @@ object DenseMatrix {
*/
@Since("1.3.0")
def randn(numRows: Int, numCols: Int, rng: Random): DenseMatrix = {
require(numRows.toLong * numCols <= Int.MaxValue,
require(numRows.toLong * numCols <= Int.MaxValue - 8,
s"$numRows x $numCols dense matrix is too large to allocate")
new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextGaussian()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ class BlockMatrix @Since("1.3.0") (
s"Int.MaxValue. Currently numRows: ${numRows()}")
require(numCols() < Int.MaxValue, "The number of columns of this matrix should be less than " +
s"Int.MaxValue. Currently numCols: ${numCols()}")
require(numRows() * numCols() < Int.MaxValue, "The length of the values array must be " +
s"less than Int.MaxValue. Currently numRows * numCols: ${numRows() * numCols()}")
require(numRows() * numCols() < Int.MaxValue - 8, "The length of the values array must be " +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<=

s"less than ${Int.MaxValue - 8}. Currently numRows * numCols: ${numRows() * numCols()}")
val m = numRows().toInt
val n = numCols().toInt
val mem = m * n / 125000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ public BufferHolder(UnsafeRow row, int initialSize) {
* Grows the buffer by at least neededSize and points the row to the buffer.
*/
public void grow(int neededSize) {
if (neededSize > Integer.MAX_VALUE - totalSize()) {
int arrayMax = Integer.MAX_VALUE - 8;
if (neededSize > arrayMax - totalSize()) {
throw new UnsupportedOperationException(
"Cannot grow BufferHolder by size " + neededSize + " because the size after growing " +
"exceeds size limitation " + Integer.MAX_VALUE);
"exceeds size limitation " + arrayMax);
}
final int length = totalSize() + neededSize;
if (buffer.length < length) {
// This will not happen frequently, because the buffer is re-used.
int newLength = length < Integer.MAX_VALUE / 2 ? length * 2 : Integer.MAX_VALUE;
int newLength = length < arrayMax / 2 ? length * 2 : arrayMax;
final byte[] tmp = new byte[newLength];
Platform.copyMemory(
buffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public final int appendStruct(boolean isNull) {
* Upper limit for the maximum capacity for this column.
*/
@VisibleForTesting
protected int MAX_CAPACITY = Integer.MAX_VALUE;
protected int MAX_CAPACITY = Integer.MAX_VALUE - 8;

/**
* Number of nulls in this column. This is an optimization for the reader, to skip NULL checks.
Expand Down