You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by zh...@apache.org on 2025/06/16 09:34:41 UTC
(lucene) branch branch_10x updated: Use IntArrayList/IntHashSet to replace usages of List/Set of Integer (#14774)
This is an automated email from the ASF dual-hosted git repository.
zhangchao pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/branch_10x by this push:
new 9c0cf3dcce4 Use IntArrayList/IntHashSet to replace usages of List/Set of Integer (#14774)
9c0cf3dcce4 is described below
commit 9c0cf3dcce43bf0ef7b1faa81bcd0c0e778b3e2e
Author: Zhang Chao <80...@qq.com>
AuthorDate: Mon Jun 16 17:21:13 2025 +0800
Use IntArrayList/IntHashSet to replace usages of List/Set of Integer (#14774)
* Replace List<Integer> by IntArrayList
* Replace Set<Integer> by IntHashSet
* mark UpdateGraphsUtils as lucene internal
---
lucene/CHANGES.txt | 2 ++
.../org/apache/lucene/util/automaton/RegExp.java | 32 +++++++++-------------
.../lucene/util/hnsw/MergingHnswGraphBuilder.java | 8 +++---
.../apache/lucene/util/hnsw/UpdateGraphsUtils.java | 9 +++---
.../apache/lucene/util/hnsw/HnswGraphTestCase.java | 3 +-
.../lucene/sandbox/codecs/quantization/KMeans.java | 10 +++----
6 files changed, 31 insertions(+), 33 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 97de79a48f8..389e7dc078b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -82,6 +82,8 @@ Optimizations
* GITHUB#14753: Implement IndexedDISI#docIDRunEnd. (Ge Song)
+* GITHUB#14774: Use IntArrayList/IntHashSet to replace usages of List/Set of Integer. (Zhang Chao)
+
Bug Fixes
---------------------
* GITHUB#14654: ValueSource.fromDoubleValuesSource(dvs).getSortField() would throw errors when
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java
index c5e80fa8c0c..5676bb66c4c 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java
@@ -31,7 +31,6 @@ package org.apache.lucene.util.automaton;
import java.io.IOException;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -40,6 +39,7 @@ import java.util.Map;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
+import org.apache.lucene.internal.hppc.IntArrayList;
/**
* Regular Expression extension to <code>Automaton</code>.
@@ -767,14 +767,14 @@ public class RegExp {
* @return the original codepoint and the set of alternates
*/
private int[] toCaseInsensitiveChar(int codepoint) {
- List<Integer> list = new ArrayList<>();
+ IntArrayList list = new IntArrayList();
CaseFolding.expand(
codepoint,
(int variant) -> {
list.add(variant);
});
- Collections.sort(list);
- return list.stream().mapToInt(Integer::intValue).toArray();
+ list.sort();
+ return list.toArray();
}
/**
@@ -785,7 +785,7 @@ public class RegExp {
* activated by optional flag.
*/
private void expandCaseInsensitiveRange(
- int start, int end, List<Integer> rangeStarts, List<Integer> rangeEnds) {
+ int start, int end, IntArrayList rangeStarts, IntArrayList rangeEnds) {
if (start > end)
throw new IllegalArgumentException(
"invalid range: from (" + start + ") cannot be > to (" + end + ")");
@@ -1341,8 +1341,8 @@ public class RegExp {
}
final RegExp parseCharClasses() throws IllegalArgumentException {
- ArrayList<Integer> starts = new ArrayList<>();
- ArrayList<Integer> ends = new ArrayList<>();
+ IntArrayList starts = new IntArrayList();
+ IntArrayList ends = new IntArrayList();
do {
// look for escape
@@ -1385,20 +1385,17 @@ public class RegExp {
// not sure why we bother optimizing nodes, same automaton...
// definitely saves time vs fixing toString()-based tests.
if (starts.size() == 1) {
- if (starts.get(0).intValue() == ends.get(0).intValue()) {
+ if (starts.get(0) == ends.get(0)) {
return makeChar(flags, starts.get(0));
} else {
return makeCharRange(flags, starts.get(0), ends.get(0));
}
} else {
- return makeCharClass(
- flags,
- starts.stream().mapToInt(Integer::intValue).toArray(),
- ends.stream().mapToInt(Integer::intValue).toArray());
+ return makeCharClass(flags, starts.toArray(), ends.toArray());
}
}
- void expandPreDefined(List<Integer> starts, List<Integer> ends) {
+ void expandPreDefined(IntArrayList starts, IntArrayList ends) {
if (peek("\\")) {
// escape
starts.add((int) '\\');
@@ -1472,13 +1469,10 @@ public class RegExp {
final RegExp matchPredefinedCharacterClass() {
// See https://docs.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
if (match('\\') && peek("\\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) {
- var starts = new ArrayList<Integer>();
- var ends = new ArrayList<Integer>();
+ var starts = new IntArrayList();
+ var ends = new IntArrayList();
expandPreDefined(starts, ends);
- return makeCharClass(
- flags,
- starts.stream().mapToInt(Integer::intValue).toArray(),
- ends.stream().mapToInt(Integer::intValue).toArray());
+ return makeCharClass(flags, starts.toArray(), ends.toArray());
}
return null;
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java
index 5c5ff442b16..3704472498b 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java
@@ -20,7 +20,7 @@ package org.apache.lucene.util.hnsw;
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
import java.io.IOException;
-import java.util.Set;
+import org.apache.lucene.internal.hppc.IntCursor;
import org.apache.lucene.internal.hppc.IntHashSet;
import org.apache.lucene.util.BitSet;
@@ -143,11 +143,11 @@ public final class MergingHnswGraphBuilder extends HnswGraphBuilder {
/** Merge the smaller graph into the current larger graph. */
private void updateGraph(HnswGraph gS, int[] ordMapS) throws IOException {
int size = gS.size();
- Set<Integer> j = UpdateGraphsUtils.computeJoinSet(gS);
+ IntHashSet j = UpdateGraphsUtils.computeJoinSet(gS);
// for nodes that in the join set, add them directly to the graph
- for (int node : j) {
- addGraphNode(ordMapS[node]);
+ for (IntCursor node : j) {
+ addGraphNode(ordMapS[node.value]);
}
// for each node outside of j set:
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateGraphsUtils.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateGraphsUtils.java
index 408b529d6e1..667ba37b7e3 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateGraphsUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateGraphsUtils.java
@@ -20,13 +20,14 @@ package org.apache.lucene.util.hnsw;
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
import java.io.IOException;
-import java.util.HashSet;
-import java.util.Set;
+import org.apache.lucene.internal.hppc.IntHashSet;
import org.apache.lucene.util.LongHeap;
/**
* Utility class for updating a big graph with smaller graphs. This is used during merging of
* segments containing HNSW graphs.
+ *
+ * @lucene.internal
*/
public class UpdateGraphsUtils {
@@ -37,11 +38,11 @@ public class UpdateGraphsUtils {
*
* @return a set of nodes that best cover the graph
*/
- public static Set<Integer> computeJoinSet(HnswGraph graph) throws IOException {
+ public static IntHashSet computeJoinSet(HnswGraph graph) throws IOException {
int k; // coverage for the current node
int size = graph.size();
LongHeap heap = new LongHeap(size);
- Set<Integer> j = new HashSet<>();
+ IntHashSet j = new IntHashSet();
boolean[] stale = new boolean[size];
short[] counts = new short[size];
long gExit = 0L;
diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/HnswGraphTestCase.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/HnswGraphTestCase.java
index f199d662b06..e5c32cdbf86 100644
--- a/lucene/core/src/test/org/apache/lucene/util/hnsw/HnswGraphTestCase.java
+++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/HnswGraphTestCase.java
@@ -61,6 +61,7 @@ import org.apache.lucene.index.StoredFields;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.internal.hppc.IntHashSet;
import org.apache.lucene.search.AbstractKnnCollector;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
@@ -537,7 +538,7 @@ abstract class HnswGraphTestCase<T> extends LuceneTestCase {
HnswGraphBuilder builder = HnswGraphBuilder.create(scorerSupplier, M, beamWidth, seed);
HnswGraph graph = builder.build(vectors.size());
- Set<Integer> j = UpdateGraphsUtils.computeJoinSet(graph);
+ IntHashSet j = UpdateGraphsUtils.computeJoinSet(graph);
assertTrue(
"Join set size [" + j.size() + "] is not less than graph size [" + graph.size() + "]",
j.size() < graph.size());
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/quantization/KMeans.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/quantization/KMeans.java
index 88d2adba5fa..036f4396e3a 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/quantization/KMeans.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/quantization/KMeans.java
@@ -21,12 +21,12 @@ import static org.apache.lucene.sandbox.codecs.quantization.SampleReader.createS
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashSet;
import java.util.List;
import java.util.Random;
-import java.util.Set;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.internal.hppc.IntCursor;
+import org.apache.lucene.internal.hppc.IntHashSet;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.VectorUtil;
import org.apache.lucene.util.hnsw.NeighborQueue;
@@ -190,14 +190,14 @@ public class KMeans {
* centroids
*/
private float[][] initializeForgy() throws IOException {
- Set<Integer> selection = new HashSet<>();
+ IntHashSet selection = new IntHashSet();
while (selection.size() < numCentroids) {
selection.add(random.nextInt(numVectors));
}
float[][] initialCentroids = new float[numCentroids][];
int i = 0;
- for (Integer selectedIdx : selection) {
- float[] vector = vectors.vectorValue(selectedIdx);
+ for (IntCursor selectedIdx : selection) {
+ float[] vector = vectors.vectorValue(selectedIdx.value);
initialCentroids[i++] = ArrayUtil.copyOfSubArray(vector, 0, vector.length);
}
return initialCentroids;