Skip to content

Apache Lucene open-source search software

Reduce memory usage of `NeighborArray` (2) (#15777)

Viliam Durina7a0e79ffcf53
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 034d232bf90..e5fc7bee5a6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -126,7 +126,7 @@ Optimizations
* GITHUB#15085, GITHUB#15092: Hunspell suggestions: Ensure candidate roots are not worse before updating. (Ilia Permiashkin)
-* GITHUB#15597: Reduce memory usage of NeighborArray (Viliam Durina)
+* GITHUB#15597, GITHUB#15777: Reduce memory usage of NeighborArray (Viliam Durina)
Bug Fixes
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/internal/hppc/FloatArrayList.java b/lucene/core/src/java/org/apache/lucene/internal/hppc/FloatArrayList.java
index 728c0aa96d1..59818341c03 100644
--- a/lucene/core/src/java/org/apache/lucene/internal/hppc/FloatArrayList.java
+++ b/lucene/core/src/java/org/apache/lucene/internal/hppc/FloatArrayList.java
@@ -307,9 +307,9 @@ public class FloatArrayList implements Iterable<FloatCursor>, Cloneable, Account
}
/**
- * Sets the number of stored elements to zero. Releases and initializes the internal storage array
- * to default values. To clear the list without cleaning the buffer, simply set the {@link
- * #elementsCount} field to zero.
+ * Sets the number of stored elements to zero. Initializes the internal storage array to default
+ * values. To clear the list without cleaning the buffer, simply set the {@link #elementsCount}
+ * field to zero.
*/
public void clear() {
Arrays.fill(buffer, 0, elementsCount, 0f);
diff --git a/lucene/core/src/java/org/apache/lucene/internal/hppc/IntArrayList.java b/lucene/core/src/java/org/apache/lucene/internal/hppc/IntArrayList.java
index 992c924630d..c43704ca5db 100644
--- a/lucene/core/src/java/org/apache/lucene/internal/hppc/IntArrayList.java
+++ b/lucene/core/src/java/org/apache/lucene/internal/hppc/IntArrayList.java
@@ -308,9 +308,9 @@ public class IntArrayList implements Iterable<IntCursor>, Cloneable, Accountable
}
/**
- * Sets the number of stored elements to zero. Releases and initializes the internal storage array
- * to default values. To clear the list without cleaning the buffer, simply set the {@link
- * #elementsCount} field to zero.
+ * Sets the number of stored elements to zero. Initializes the internal storage array to default
+ * values. To clear the list without cleaning the buffer, simply set the {@link #elementsCount}
+ * field to zero.
*/
public void clear() {
Arrays.fill(buffer, 0, elementsCount, 0);
diff --git a/lucene/core/src/java/org/apache/lucene/internal/hppc/LongArrayList.java b/lucene/core/src/java/org/apache/lucene/internal/hppc/LongArrayList.java
index dcc37c1a527..2c1d1256ff7 100644
--- a/lucene/core/src/java/org/apache/lucene/internal/hppc/LongArrayList.java
+++ b/lucene/core/src/java/org/apache/lucene/internal/hppc/LongArrayList.java
@@ -308,9 +308,9 @@ public class LongArrayList implements Iterable<LongCursor>, Cloneable, Accountab
}
/**
- * Sets the number of stored elements to zero. Releases and initializes the internal storage array
- * to default values. To clear the list without cleaning the buffer, simply set the {@link
- * #elementsCount} field to zero.
+ * Sets the number of stored elements to zero. Initializes the internal storage array to default
+ * values. To clear the list without cleaning the buffer, simply set the {@link #elementsCount}
+ * field to zero.
*/
public void clear() {
Arrays.fill(buffer, 0, elementsCount, 0L);
diff --git a/lucene/core/src/java/org/apache/lucene/internal/hppc/MaxSizedFloatArrayList.java b/lucene/core/src/java/org/apache/lucene/internal/hppc/MaxSizedFloatArrayList.java
deleted file mode 100644
index 31799af4ca7..00000000000
--- a/lucene/core/src/java/org/apache/lucene/internal/hppc/MaxSizedFloatArrayList.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.lucene.internal.hppc;
-
-import static org.apache.lucene.internal.hppc.HashContainers.DEFAULT_EXPECTED_ELEMENTS;
-
-import org.apache.lucene.util.ArrayUtil;
-import org.apache.lucene.util.RamUsageEstimator;
-
-/**
- * An array-backed list of {@code float} with a maximum size limit.
- *
- * @lucene.internal
- */
-public class MaxSizedFloatArrayList extends FloatArrayList {
- private static final long BASE_RAM_BYTES_USED =
- RamUsageEstimator.shallowSizeOfInstance(MaxSizedFloatArrayList.class);
-
- final int maxSize;
-
- /** New instance with sane defaults. */
- public MaxSizedFloatArrayList(int maxSize) {
- this(maxSize, DEFAULT_EXPECTED_ELEMENTS);
- }
-
- /**
- * New instance with sane defaults.
- *
- * @param maxSize The maximum size this list can grow to
- * @param expectedElements The expected number of elements guaranteed not to cause buffer
- * expansion (inclusive).
- */
- public MaxSizedFloatArrayList(int maxSize, int expectedElements) {
- super(expectedElements);
- assert expectedElements <= maxSize
- : "expectedElements (" + expectedElements + ") must be <= maxSize (" + maxSize + ")";
- this.maxSize = maxSize;
- }
-
- /** Creates a new list from the elements of another list in its iteration order. */
- public MaxSizedFloatArrayList(MaxSizedFloatArrayList list) {
- super(list.size());
- this.maxSize = list.maxSize;
- addAll(list);
- }
-
- @Override
- protected void ensureBufferSpace(int expectedAdditions) {
- if (elementsCount + expectedAdditions > maxSize) {
- throw new IllegalStateException("Cannot grow beyond maxSize: " + maxSize);
- }
- if (elementsCount + expectedAdditions > buffer.length) {
- this.buffer = ArrayUtil.growInRange(buffer, elementsCount + expectedAdditions, maxSize);
- }
- }
-
- @Override
- public int hashCode() {
- int h = 1, max = elementsCount;
- h = 31 * h + maxSize;
- for (int i = 0; i < max; i++) {
- h = 31 * h + BitMixer.mix(this.buffer[i]);
- }
- return h;
- }
-
- /**
- * Returns <code>true</code> only if the other object is an instance of the same class and with
- * the same elements and maxSize.
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null || getClass() != obj.getClass()) {
- return false;
- }
- MaxSizedFloatArrayList other = (MaxSizedFloatArrayList) obj;
- return maxSize == other.maxSize && super.equals(obj);
- }
-
- @Override
- public long ramBytesUsed() {
- return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(buffer);
- }
-}
diff --git a/lucene/core/src/java/org/apache/lucene/internal/hppc/MaxSizedIntArrayList.java b/lucene/core/src/java/org/apache/lucene/internal/hppc/MaxSizedIntArrayList.java
deleted file mode 100644
index 8640b3fb40a..00000000000
--- a/lucene/core/src/java/org/apache/lucene/internal/hppc/MaxSizedIntArrayList.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.lucene.internal.hppc;
-
-import static org.apache.lucene.internal.hppc.HashContainers.DEFAULT_EXPECTED_ELEMENTS;
-
-import org.apache.lucene.util.ArrayUtil;
-import org.apache.lucene.util.RamUsageEstimator;
-
-/**
- * An array-backed list of {@code int} with a maximum size limit.
- *
- * @lucene.internal
- */
-public class MaxSizedIntArrayList extends IntArrayList {
- private static final long BASE_RAM_BYTES_USED =
- RamUsageEstimator.shallowSizeOfInstance(MaxSizedIntArrayList.class);
-
- final int maxSize;
-
- /** New instance with sane defaults. */
- public MaxSizedIntArrayList(int maxSize) {
- this(maxSize, DEFAULT_EXPECTED_ELEMENTS);
- }
-
- /**
- * New instance with sane defaults.
- *
- * @param maxSize The maximum size this list can grow to
- * @param expectedElements The expected number of elements guaranteed not to cause buffer
- * expansion (inclusive).
- */
- public MaxSizedIntArrayList(int maxSize, int expectedElements) {
- super(expectedElements);
- assert expectedElements <= maxSize
- : "expectedElements (" + expectedElements + ") must be <= maxSize (" + maxSize + ")";
- this.maxSize = maxSize;
- }
-
- /** Creates a new list from the elements of another list in its iteration order. */
- public MaxSizedIntArrayList(MaxSizedIntArrayList list) {
- super(list.size());
- this.maxSize = list.maxSize;
- addAll(list);
- }
-
- @Override
- protected void ensureBufferSpace(int expectedAdditions) {
- if (elementsCount + expectedAdditions > maxSize) {
- throw new IllegalStateException("Cannot grow beyond maxSize: " + maxSize);
- }
- if (elementsCount + expectedAdditions > buffer.length) {
- this.buffer = ArrayUtil.growInRange(buffer, elementsCount + expectedAdditions, maxSize);
- }
- }
-
- @Override
- public int hashCode() {
- int h = 1, max = elementsCount;
- h = 31 * h + maxSize;
- for (int i = 0; i < max; i++) {
- h = 31 * h + BitMixer.mix(this.buffer[i]);
- }
- return h;
- }
-
- /**
- * Returns <code>true</code> only if the other object is an instance of the same class and with
- * the same elements and maxSize.
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null || getClass() != obj.getClass()) {
- return false;
- }
- MaxSizedIntArrayList other = (MaxSizedIntArrayList) obj;
- return maxSize == other.maxSize && super.equals(obj);
- }
-
- @Override
- public long ramBytesUsed() {
- return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(buffer);
- }
-}
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java
index e8ebef29d44..3ddba160b44 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java
@@ -20,8 +20,9 @@ package org.apache.lucene.util.hnsw;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.LongConsumer;
-import org.apache.lucene.internal.hppc.MaxSizedFloatArrayList;
-import org.apache.lucene.internal.hppc.MaxSizedIntArrayList;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.RamUsageEstimator;
/**
@@ -39,8 +40,8 @@ public class NeighborArray {
private final boolean scoresDescOrder;
private int size;
private final int maxSize;
- private final MaxSizedFloatArrayList scores;
- private final MaxSizedIntArrayList nodes;
+ private float[] scores;
+ private int[] nodes;
private int sortedNodeSize;
private final LongConsumer onHeapMemoryUsageListener;
@@ -50,13 +51,13 @@ public class NeighborArray {
public NeighborArray(int maxSize, boolean descOrder, LongConsumer onHeapMemoryUsageListener) {
this.maxSize = maxSize;
- nodes = new MaxSizedIntArrayList(maxSize, maxSize / 8);
- scores = new MaxSizedFloatArrayList(maxSize, maxSize / 8);
+ nodes = new int[maxSize / 8];
+ scores = new float[maxSize / 8];
this.scoresDescOrder = descOrder;
this.onHeapMemoryUsageListener = onHeapMemoryUsageListener;
if (onHeapMemoryUsageListener != null) {
onHeapMemoryUsageListener.accept(
- BASE_RAM_BYTES_USED + nodes.ramBytesUsed() + scores.ramBytesUsed());
+ BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(nodes) + RamUsageEstimator.sizeOf(scores));
}
}
@@ -70,36 +71,49 @@ public class NeighborArray {
throw new IllegalStateException("No growth is allowed");
}
if (size > 0) {
- float previousScore = scores.get(size - 1);
+ float previousScore = scores[size - 1];
assert ((scoresDescOrder && (previousScore >= newScore))
|| (scoresDescOrder == false && (previousScore <= newScore)))
- : "Nodes are added in the incorrect order! Comparing "
+ : "Nodes are added in an incorrect order! Comparing "
+ newScore
+ " to "
- + Arrays.toString(scores.toArray());
+ + IntStream.range(0, size)
+ .mapToObj(i -> "" + scores[i])
+ .collect(Collectors.joining(", ", "[", "]"));
}
- int previousLength = nodes.buffer.length;
- nodes.add(newNode);
- scores.add(newScore);
- alertOnHeapMemoryUsageChange(nodes.buffer.length, previousLength);
+ growArrays();
+ nodes[size] = newNode;
+ scores[size] = newScore;
++size;
++sortedNodeSize;
}
+ /// Grow the [#scores] and [#nodes] fields if they are full, up to [#maxSize]. Grow in larger steps.
+ private void growArrays() {
+ if (size == maxSize) {
+ throw new IllegalStateException("Cannot grow beyond maxSize: " + maxSize);
+ }
+ if (size == nodes.length) {
+ int oldLength = nodes.length;
+ nodes = ArrayUtil.growInRange(nodes, size + 1, maxSize);
+ scores = ArrayUtil.growInRange(scores, size + 1, maxSize);
+ alertOnHeapMemoryUsageChange(nodes.length, oldLength);
+ }
+ }
+
/** Add node and newScore but do not insert as sorted */
public void addOutOfOrder(int newNode, float newScore) {
if (size == maxSize) {
throw new IllegalStateException("No growth is allowed");
}
- int previousLength = nodes.buffer.length;
- nodes.add(newNode);
- scores.add(newScore);
- alertOnHeapMemoryUsageChange(nodes.buffer.length, previousLength);
+ growArrays();
+ nodes[size] = newNode;
+ scores[size] = newScore;
size++;
}
private void alertOnHeapMemoryUsageChange(int newLength, int previousLength) {
- if (newLength > previousLength && onHeapMemoryUsageListener != null) {
+ if (onHeapMemoryUsageListener != null) {
int lengthDelta = newLength - previousLength;
onHeapMemoryUsageListener.accept(
(long) (lengthDelta) * Integer.BYTES + (long) (lengthDelta) * Float.BYTES);
@@ -163,8 +177,8 @@ public class NeighborArray {
/** insert the first unsorted node into its sorted position */
private int insertSortedInternal(RandomVectorScorer scorer) throws IOException {
assert sortedNodeSize < size : "Call this method only when there's unsorted node";
- int tmpNode = nodes.get(sortedNodeSize);
- float tmpScore = scores.get(sortedNodeSize);
+ int tmpNode = nodes[sortedNodeSize];
+ float tmpScore = scores[sortedNodeSize];
if (Float.isNaN(tmpScore)) {
tmpScore = scorer.score(tmpNode);
@@ -175,19 +189,11 @@ public class NeighborArray {
? descSortFindRightMostInsertionPoint(tmpScore, sortedNodeSize)
: ascSortFindRightMostInsertionPoint(tmpScore, sortedNodeSize);
System.arraycopy(
- nodes.buffer,
- insertionPoint,
- nodes.buffer,
- insertionPoint + 1,
- sortedNodeSize - insertionPoint);
+ nodes, insertionPoint, nodes, insertionPoint + 1, sortedNodeSize - insertionPoint);
System.arraycopy(
- scores.buffer,
- insertionPoint,
- scores.buffer,
- insertionPoint + 1,
- sortedNodeSize - insertionPoint);
- nodes.buffer[insertionPoint] = tmpNode;
- scores.buffer[insertionPoint] = tmpScore;
+ scores, insertionPoint, scores, insertionPoint + 1, sortedNodeSize - insertionPoint);
+ nodes[insertionPoint] = tmpNode;
+ scores[insertionPoint] = tmpScore;
++sortedNodeSize;
return insertionPoint;
}
@@ -208,7 +214,7 @@ public class NeighborArray {
* @lucene.internal
*/
public int[] nodes() {
- return nodes.buffer;
+ return nodes;
}
/**
@@ -218,19 +224,18 @@ public class NeighborArray {
* @return the score at the given index
*/
public float getScores(int i) {
- return scores.get(i);
+ return scores[i];
}
public void clear() {
size = 0;
sortedNodeSize = 0;
- nodes.clear();
- scores.clear();
+ Arrays.fill(nodes, 0, size, 0);
+ Arrays.fill(scores, 0, size, 0f);
}
void removeLast() {
- nodes.removeLast();
- scores.removeLast();
+ assert size > 0;
size--;
sortedNodeSize = Math.min(sortedNodeSize, size);
}
@@ -240,8 +245,8 @@ public class NeighborArray {
removeLast();
return;
}
- nodes.removeAt(idx);
- scores.removeAt(idx);
+ System.arraycopy(nodes, idx + 1, nodes, idx, size - idx - 1);
+ System.arraycopy(scores, idx + 1, scores, idx, size - idx - 1);
if (idx < sortedNodeSize) {
sortedNodeSize--;
}
@@ -254,11 +259,11 @@ public class NeighborArray {
}
private int ascSortFindRightMostInsertionPoint(float newScore, int bound) {
- int insertionPoint = Arrays.binarySearch(scores.buffer, 0, bound, newScore);
+ int insertionPoint = Arrays.binarySearch(scores, 0, bound, newScore);
if (insertionPoint >= 0) {
// find the right most position with the same score
while ((insertionPoint < bound - 1)
- && (scores.get(insertionPoint + 1) == scores.get(insertionPoint))) {
+ && (scores[insertionPoint + 1] == scores[insertionPoint])) {
insertionPoint++;
}
insertionPoint++;
@@ -273,7 +278,7 @@ public class NeighborArray {
int end = bound - 1;
while (start <= end) {
int mid = (start + end) / 2;
- if (scores.get(mid) < newScore) end = mid - 1;
+ if (scores[mid] < newScore) end = mid - 1;
else start = mid + 1;
}
return start;
@@ -292,7 +297,7 @@ public class NeighborArray {
// no unchecked node left
break;
}
- scorer.setScoringOrdinal(nodes.get(i));
+ scorer.setScoringOrdinal(nodes[i]);
if (isWorstNonDiverse(i, uncheckedIndexes, uncheckedCursor, scorer)) {
return i;
}
@@ -306,11 +311,11 @@ public class NeighborArray {
private boolean isWorstNonDiverse(
int candidateIndex, int[] uncheckedIndexes, int uncheckedCursor, RandomVectorScorer scorer)
throws IOException {
- float minAcceptedSimilarity = scores.get(candidateIndex);
+ float minAcceptedSimilarity = scores[candidateIndex];
if (candidateIndex == uncheckedIndexes[uncheckedCursor]) {
// the candidate itself is unchecked
for (int i = candidateIndex - 1; i >= 0; i--) {
- float neighborSimilarity = scorer.score(nodes.get(i));
+ float neighborSimilarity = scorer.score(nodes[i]);
// candidate node is too similar to node i given its score relative to the base node
if (neighborSimilarity >= minAcceptedSimilarity) {
return true;
@@ -321,7 +326,7 @@ public class NeighborArray {
// inserted) unchecked nodes
assert candidateIndex > uncheckedIndexes[uncheckedCursor];
for (int i = uncheckedCursor; i >= 0; i--) {
- float neighborSimilarity = scorer.score(nodes.get(uncheckedIndexes[i]));
+ float neighborSimilarity = scorer.score(nodes[uncheckedIndexes[i]]);
// candidate node is too similar to node i given its score relative to the base node
if (neighborSimilarity >= minAcceptedSimilarity) {
return true;
diff --git a/lucene/core/src/test/org/apache/lucene/internal/hppc/TestMaxSizedFloatArrayList.java b/lucene/core/src/test/org/apache/lucene/internal/hppc/TestMaxSizedFloatArrayList.java
deleted file mode 100644
index 62a7b91a5fd..00000000000
--- a/lucene/core/src/test/org/apache/lucene/internal/hppc/TestMaxSizedFloatArrayList.java
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.lucene.internal.hppc;
-
-import org.junit.Test;
-
-/** Tests for {@link MaxSizedFloatArrayList}. */
-public class TestMaxSizedFloatArrayList extends TestFloatArrayList {
- private static final int MAX_SIZE = 5;
-
- @Test
- public void testMaxSizeConstructor() {
- // Test valid maxSize
- MaxSizedFloatArrayList list = new MaxSizedFloatArrayList(MAX_SIZE, 3);
- assertEquals(3, list.buffer.length);
- assertEquals(MAX_SIZE, list.maxSize);
-
- // Test invalid maxSize (expectedElements > maxSize)
- if (TEST_ASSERTS_ENABLED) {
- expectThrows(
- AssertionError.class,
- () -> {
- new MaxSizedFloatArrayList(2, 3);
- });
- }
- }
-
- @Test
- public void testMaxSizeLimit() {
- MaxSizedFloatArrayList list = new MaxSizedFloatArrayList(MAX_SIZE);
-
- // Fill up to maxSize
- for (int i = 0; i < MAX_SIZE; i++) {
- list.add(i + 0.5f);
- }
- assertEquals(MAX_SIZE, list.size());
-
- // Try to add beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.add(MAX_SIZE + 0.5f);
- });
-
- // Try to add array beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.add(new float[] {1.5f, 2.5f, 3.5f});
- });
-
- // Try to addAll beyond maxSize
- FloatArrayList otherList = new FloatArrayList();
- otherList.add(1.5f);
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.addAll(otherList);
- });
- }
-
- @Test
- public void testMaxSizeWithInsert() {
- MaxSizedFloatArrayList list = new MaxSizedFloatArrayList(MAX_SIZE);
-
- // Fill up to maxSize - 1
- for (int i = 0; i < MAX_SIZE - 1; i++) {
- list.add(i + 0.5f);
- }
- assertEquals(MAX_SIZE - 1, list.size());
-
- // Insert at the end (should succeed)
- list.insert(MAX_SIZE - 1, (MAX_SIZE - 1) + 0.5f);
- assertEquals(MAX_SIZE, list.size());
-
- // Try to insert beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.insert(0, MAX_SIZE + 0.5f);
- });
- }
-
- @Test
- public void testMaxSizeWithResize() {
- MaxSizedFloatArrayList list = new MaxSizedFloatArrayList(MAX_SIZE);
-
- // Try to resize beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.resize(MAX_SIZE + 1);
- });
-
- // Resize to maxSize (should succeed)
- list.resize(MAX_SIZE);
- assertEquals(MAX_SIZE, list.size());
- }
-
- @Test
- public void testMaxSizeWithEnsureCapacity() {
- MaxSizedFloatArrayList list = new MaxSizedFloatArrayList(MAX_SIZE);
-
- // Try to ensure capacity beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.ensureCapacity(MAX_SIZE + 1);
- });
-
- // Ensure capacity to maxSize (should succeed)
- list.ensureCapacity(MAX_SIZE);
- // Buffer can be larger than maxSize due to growth strategy, but not smaller if capacity is
- // ensured to maxSize
- assertTrue(list.buffer.length >= MAX_SIZE || list.buffer.length == 0 && MAX_SIZE == 0);
- }
-
- @Test
- public void testMaxSizeWithCopyConstructor() {
- MaxSizedFloatArrayList original = new MaxSizedFloatArrayList(MAX_SIZE);
- original.add(1.5f, 2.5f, 3.5f);
-
- MaxSizedFloatArrayList copy = new MaxSizedFloatArrayList(original);
- assertEquals(original.maxSize, copy.maxSize);
- assertEquals(original.size(), copy.size());
- assertArrayEquals(original.toArray(), copy.toArray(), 0.001f);
- }
-
- @Test
- public void testHashCodeWithMaxSize() {
- MaxSizedFloatArrayList list1 = new MaxSizedFloatArrayList(5);
- MaxSizedFloatArrayList list2 = new MaxSizedFloatArrayList(10);
-
- // Same elements but different maxSize should have different hash codes
- list1.add(1.5f, 2.5f, 3.5f);
- list2.add(1.5f, 2.5f, 3.5f);
- assertNotEquals(list1.hashCode(), list2.hashCode());
-
- // Same maxSize and same elements should have same hash code
- MaxSizedFloatArrayList list3 = new MaxSizedFloatArrayList(5);
- list3.add(1.5f, 2.5f, 3.5f);
- assertEquals(list1.hashCode(), list3.hashCode());
- }
-
- @Test
- public void testEqualsWithMaxSize() {
- MaxSizedFloatArrayList list1 = new MaxSizedFloatArrayList(5);
- MaxSizedFloatArrayList list2 = new MaxSizedFloatArrayList(10);
- MaxSizedFloatArrayList list3 = new MaxSizedFloatArrayList(5);
-
- // Same elements but different maxSize should not be equal
- list1.add(1.5f, 2.5f, 3.5f);
- list2.add(1.5f, 2.5f, 3.5f);
- assertNotEquals(list1, list2);
-
- // Same maxSize and same elements should be equal
- list3.add(1.5f, 2.5f, 3.5f);
- assertEquals(list1, list3);
-
- // Different elements but same maxSize should not be equal
- list3.clear();
- list3.add(1.5f, 2.5f, 4.5f);
- assertNotEquals(list1, list3);
- }
-}
diff --git a/lucene/core/src/test/org/apache/lucene/internal/hppc/TestMaxSizedIntArrayList.java b/lucene/core/src/test/org/apache/lucene/internal/hppc/TestMaxSizedIntArrayList.java
deleted file mode 100644
index 408a0f17de5..00000000000
--- a/lucene/core/src/test/org/apache/lucene/internal/hppc/TestMaxSizedIntArrayList.java
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.lucene.internal.hppc;
-
-import org.junit.Test;
-
-/** Tests for {@link MaxSizedIntArrayList}. */
-public class TestMaxSizedIntArrayList extends TestIntArrayList {
- private static final int MAX_SIZE = 5;
-
- @Test
- public void testMaxSizeConstructor() {
- // Test valid maxSize
- MaxSizedIntArrayList list = new MaxSizedIntArrayList(MAX_SIZE, 3);
- assertEquals(3, list.buffer.length);
- assertEquals(MAX_SIZE, list.maxSize);
-
- // Test invalid maxSize (expectedElements > maxSize)
- if (TEST_ASSERTS_ENABLED) {
- expectThrows(
- AssertionError.class,
- () -> {
- new MaxSizedIntArrayList(2, 3);
- });
- }
- }
-
- @Test
- public void testMaxSizeLimit() {
- MaxSizedIntArrayList list = new MaxSizedIntArrayList(MAX_SIZE);
-
- // Fill up to maxSize
- for (int i = 0; i < MAX_SIZE; i++) {
- list.add(i);
- }
- assertEquals(MAX_SIZE, list.size());
-
- // Try to add beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.add(MAX_SIZE);
- });
-
- // Try to add array beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.add(new int[] {1, 2, 3});
- });
-
- // Try to addAll beyond maxSize
- IntArrayList otherList = new IntArrayList();
- otherList.add(1);
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.addAll(otherList);
- });
- }
-
- @Test
- public void testMaxSizeWithInsert() {
- MaxSizedIntArrayList list = new MaxSizedIntArrayList(MAX_SIZE);
-
- // Fill up to maxSize - 1
- for (int i = 0; i < MAX_SIZE - 1; i++) {
- list.add(i);
- }
- assertEquals(MAX_SIZE - 1, list.size());
-
- // Insert at the end (should succeed)
- list.insert(MAX_SIZE - 1, MAX_SIZE - 1);
- assertEquals(MAX_SIZE, list.size());
-
- // Try to insert beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.insert(0, MAX_SIZE);
- });
- }
-
- @Test
- public void testMaxSizeWithResize() {
- MaxSizedIntArrayList list = new MaxSizedIntArrayList(MAX_SIZE);
-
- // Try to resize beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.resize(MAX_SIZE + 1);
- });
-
- // Resize to maxSize (should succeed)
- list.resize(MAX_SIZE);
- assertEquals(MAX_SIZE, list.size());
- }
-
- @Test
- public void testMaxSizeWithEnsureCapacity() {
- MaxSizedIntArrayList list = new MaxSizedIntArrayList(MAX_SIZE);
-
- // Try to ensure capacity beyond maxSize
- expectThrows(
- IllegalStateException.class,
- () -> {
- list.ensureCapacity(MAX_SIZE + 1);
- });
-
- // Ensure capacity to maxSize (should succeed)
- list.ensureCapacity(MAX_SIZE);
- assertEquals(MAX_SIZE, list.buffer.length);
- }
-
- @Test
- public void testMaxSizeWithCopyConstructor() {
- MaxSizedIntArrayList original = new MaxSizedIntArrayList(MAX_SIZE);
- original.add(1, 2, 3);
-
- MaxSizedIntArrayList copy = new MaxSizedIntArrayList(original);
- assertEquals(original.maxSize, copy.maxSize);
- assertEquals(original.size(), copy.size());
- assertArrayEquals(original.toArray(), copy.toArray());
- }
-
- @Test
- public void testHashCodeWithMaxSize() {
- MaxSizedIntArrayList list1 = new MaxSizedIntArrayList(5);
- MaxSizedIntArrayList list2 = new MaxSizedIntArrayList(10);
-
- // Same elements but different maxSize should have different hash codes
- list1.add(1, 2, 3);
- list2.add(1, 2, 3);
- assertNotEquals(list1.hashCode(), list2.hashCode());
-
- // Same maxSize and same elements should have same hash code
- MaxSizedIntArrayList list3 = new MaxSizedIntArrayList(5);
- list3.add(1, 2, 3);
- assertEquals(list1.hashCode(), list3.hashCode());
- }
-
- @Test
- public void testEqualsWithMaxSize() {
- MaxSizedIntArrayList list1 = new MaxSizedIntArrayList(5);
- MaxSizedIntArrayList list2 = new MaxSizedIntArrayList(10);
- MaxSizedIntArrayList list3 = new MaxSizedIntArrayList(5);
-
- // Same elements but different maxSize should not be equal
- list1.add(1, 2, 3);
- list2.add(1, 2, 3);
- assertNotEquals(list1, list2);
-
- // Same maxSize and same elements should be equal
- list3.add(1, 2, 3);
- assertEquals(list1, list3);
-
- // Different elements but same maxSize should not be equal
- list3.clear();
- list3.add(1, 2, 4);
- assertNotEquals(list1, list3);
- }
-}
diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/TestNeighborArray.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/TestNeighborArray.java
index 0bc28ba2ad8..b070d7c34bd 100644
--- a/lucene/core/src/test/org/apache/lucene/util/hnsw/TestNeighborArray.java
+++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/TestNeighborArray.java
@@ -30,8 +30,7 @@ public class TestNeighborArray extends LuceneTestCase {
if (TEST_ASSERTS_ENABLED) {
AssertionError ex = expectThrows(AssertionError.class, () -> neighbors.addInOrder(2, 0.9f));
- assert ex.getMessage().startsWith("Nodes are added in the incorrect order!")
- : ex.getMessage();
+ assert ex.getMessage().startsWith("Nodes are added in an incorrect order!") : ex.getMessage();
}
neighbors.insertSorted(3, 0.9f);
@@ -82,8 +81,7 @@ public class TestNeighborArray extends LuceneTestCase {
if (TEST_ASSERTS_ENABLED) {
AssertionError ex = expectThrows(AssertionError.class, () -> neighbors.addInOrder(2, 0.15f));
- assert ex.getMessage().startsWith("Nodes are added in the incorrect order!")
- : ex.getMessage();
+ assert ex.getMessage().startsWith("Nodes are added in an incorrect order!") : ex.getMessage();
}
neighbors.insertSorted(3, 0.3f);
@@ -191,7 +189,7 @@ public class TestNeighborArray extends LuceneTestCase {
assertScoresEqual(new float[] {7, 6, 5, 4, 3, 2, 1}, neighbors2);
}
- public void testAddwithScoringFunction() throws IOException {
+ public void testAddWithScoringFunction() throws IOException {
NeighborArray neighbors = new NeighborArray(10, true);
neighbors.addOutOfOrder(1, Float.NaN);
if (TEST_ASSERTS_ENABLED) {
@@ -209,7 +207,7 @@ public class TestNeighborArray extends LuceneTestCase {
assertScoresEqual(new float[] {7, 6, 5, 4, 3, 2, 1}, neighbors);
}
- public void testAddwithScoringFunctionLargeOrd() throws IOException {
+ public void testAddWithScoringFunctionLargeOrd() throws IOException {
NeighborArray neighbors = new NeighborArray(10, true);
neighbors.addOutOfOrder(11, Float.NaN);
if (TEST_ASSERTS_ENABLED) {