Skip to content

Commit fc4c60c

Browse files
committed
Fallback to ArrayUtil.oversize if we overvflow
The default way is to always grow the byte[] to the next power of two of the requested length. This can overflow even if there might still be enough space. We now fall back to Lucene's oversize implementation in case of an overflow and try if we can make it fit.
1 parent e9f05a0 commit fc4c60c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

core/src/main/java/org/neo4j/gds/core/loading/ChunkedAdjacencyLists.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
*/
2020
package org.neo4j.gds.core.loading;
2121

22+
import org.neo4j.gds.collections.ArrayUtil;
2223
import org.neo4j.gds.collections.DrainingIterator;
23-
import org.neo4j.gds.collections.hsl.HugeSparseByteArrayList;
2424
import org.neo4j.gds.collections.hsa.HugeSparseCollections;
25+
import org.neo4j.gds.collections.hsl.HugeSparseByteArrayList;
2526
import org.neo4j.gds.collections.hsl.HugeSparseIntList;
2627
import org.neo4j.gds.collections.hsl.HugeSparseLongArrayList;
2728
import org.neo4j.gds.collections.hsl.HugeSparseLongList;
@@ -220,15 +221,31 @@ private long[] ensurePropertyCapacity(long index, int pos, int required, int pro
220221
)
221222
);
222223
} else if (currentProperties.length <= pos + required) {
223-
// int newLength = ArrayUtil.oversize(pos + required, Long.BYTES);
224-
int newLength = BitUtil.nextHighestPowerOfTwo(pos + required);
224+
var newLength = getNewLength(pos + required);
225225
currentProperties = Arrays.copyOf(currentProperties, newLength);
226226
this.properties[propertyIndex].set(index, currentProperties);
227227
}
228228

229229
return currentProperties;
230230
}
231231

232+
static int getNewLength(int minLength) {
233+
int newLength = BitUtil.nextHighestPowerOfTwo(minLength);
234+
if (newLength < 0) {
235+
// If we overflow, we try to grow by ~1/8th and :fingers_crossed: it's enough.
236+
newLength = ArrayUtil.oversize(minLength, Long.BYTES);
237+
}
238+
if (newLength < 0) {
239+
throw new IllegalArgumentException(
240+
formatWithLocale(
241+
"Encountered numeric overflow in compressed buffer. Required a minimum length of %d.",
242+
minLength
243+
)
244+
);
245+
}
246+
return newLength;
247+
}
248+
232249
public long capacity() {
233250
return targetLists.capacity();
234251
}

0 commit comments

Comments
 (0)