Skip to content

Apache Lucene open-source search software

Use ArrayUtil#copyOfSubArray to simplify manual array copy patterns (#15826)

Zhang Chao8169afebd260
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9db4d195d4b..8a9fd8d43a6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -281,6 +281,8 @@ Other
* GITHUB#15799: Exclude JNA Cleaner from thread leak detection by LuceneTestCase. (Alan Woodward)
+* GITHUB#15826: Use ArrayUtil#copyOfSubArray to simplify manual array copy patterns. (Zhang Chao)
+
======================= Lucene 10.4.0 =======================
API Changes
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java
index 67f4a966b0f..1584e32d849 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java
@@ -260,8 +260,7 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {
public PendingTerm(BytesRef term, BlockTermState state) {
super(true);
- this.termBytes = new byte[term.length];
- System.arraycopy(term.bytes, term.offset, termBytes, 0, term.length);
+ this.termBytes = ArrayUtil.copyOfSubArray(term.bytes, term.offset, term.offset + term.length);
this.state = state;
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java
index cbf6c7e660f..2d1d012cc33 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java
@@ -277,8 +277,7 @@ public final class DirectPostingsFormat extends PostingsFormat {
}
public int[] get() {
- final int[] arr = new int[upto];
- System.arraycopy(ints, 0, arr, 0, upto);
+ final int[] arr = ArrayUtil.copyOfSubArray(ints, 0, upto);
upto = 0;
return arr;
}
@@ -444,10 +443,9 @@ public final class DirectPostingsFormat extends PostingsFormat {
if (hasPayloads) {
BytesRef payload = docsAndPositionsEnum.getPayload();
if (payload != null) {
- byte[] payloadBytes = new byte[payload.length];
- System.arraycopy(
- payload.bytes, payload.offset, payloadBytes, 0, payload.length);
- payloads[upto][pos] = payloadBytes;
+ payloads[upto][pos] =
+ ArrayUtil.copyOfSubArray(
+ payload.bytes, payload.offset, payload.offset + payload.length);
}
}
posUpto++;
@@ -477,8 +475,7 @@ public final class DirectPostingsFormat extends PostingsFormat {
// System.out.println(skipCount + " skips: " + field);
- this.termBytes = new byte[termOffset];
- System.arraycopy(termBytes, 0, this.termBytes, 0, termOffset);
+ this.termBytes = ArrayUtil.copyOfSubArray(termBytes, 0, termOffset);
// Pack skips:
this.skips = new int[skipCount];
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java
index 1ce0ebd3509..2ca29a37c28 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java
@@ -411,8 +411,7 @@ public final class Lucene103BlockTreeTermsWriter extends FieldsConsumer {
public PendingTerm(BytesRef term, BlockTermState state) {
super(true);
- this.termBytes = new byte[term.length];
- System.arraycopy(term.bytes, term.offset, termBytes, 0, term.length);
+ this.termBytes = ArrayUtil.copyOfSubArray(term.bytes, term.offset, term.offset + term.length);
this.state = state;
}
diff --git a/lucene/core/src/java/org/apache/lucene/document/InetAddressRange.java b/lucene/core/src/java/org/apache/lucene/document/InetAddressRange.java
index 48edf68cada..8817359b3f4 100644
--- a/lucene/core/src/java/org/apache/lucene/document/InetAddressRange.java
+++ b/lucene/core/src/java/org/apache/lucene/document/InetAddressRange.java
@@ -20,6 +20,7 @@ import java.net.InetAddress;
import java.util.Arrays;
import org.apache.lucene.document.RangeFieldQuery.QueryType;
import org.apache.lucene.search.Query;
+import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BytesRef;
/**
@@ -182,10 +183,8 @@ public class InetAddressRange extends Field {
* @return The string representation for the range at the provided dimension
*/
private static String toString(byte[] ranges, int dimension) {
- byte[] min = new byte[BYTES];
- System.arraycopy(ranges, 0, min, 0, BYTES);
- byte[] max = new byte[BYTES];
- System.arraycopy(ranges, BYTES, max, 0, BYTES);
+ byte[] min = ArrayUtil.copyOfSubArray(ranges, 0, BYTES);
+ byte[] max = ArrayUtil.copyOfSubArray(ranges, BYTES, BYTES + BYTES);
return "[" + InetAddressPoint.decode(min) + " : " + InetAddressPoint.decode(max) + "]";
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java
index 80d18cea435..fd5be625493 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java
@@ -1290,9 +1290,9 @@ final class IndexingChain implements Accountable {
try {
termsHashPerField.add(invertState.termAttribute.getBytesRef(), docID);
} catch (MaxBytesLengthExceededException e) {
- byte[] prefix = new byte[30];
BytesRef bigTerm = invertState.termAttribute.getBytesRef();
- System.arraycopy(bigTerm.bytes, bigTerm.offset, prefix, 0, 30);
+ byte[] prefix =
+ ArrayUtil.copyOfSubArray(bigTerm.bytes, bigTerm.offset, bigTerm.offset + 30);
String msg =
"Document contains at least one immense term in field=\""
+ fieldInfo.name
@@ -1363,8 +1363,9 @@ final class IndexingChain implements Accountable {
try {
termsHashPerField.add(binaryValue, docID);
} catch (MaxBytesLengthExceededException e) {
- byte[] prefix = new byte[30];
- System.arraycopy(binaryValue.bytes, binaryValue.offset, prefix, 0, 30);
+ byte[] prefix =
+ ArrayUtil.copyOfSubArray(
+ binaryValue.bytes, binaryValue.offset, binaryValue.offset + 30);
String msg =
"Document contains at least one immense term in field=\""
+ fieldInfo.name
diff --git a/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java b/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java
index 8b1cb344549..cc60a566c14 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java
@@ -78,9 +78,7 @@ public abstract class DoubleValuesSourceRescorer extends Rescorer {
if (topN < hits.length) {
ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.COMPARATOR);
- ScoreDoc[] subset = new ScoreDoc[topN];
- System.arraycopy(hits, 0, subset, 0, topN);
- hits = subset;
+ hits = ArrayUtil.copyOfSubArray(hits, 0, topN);
}
Arrays.sort(hits, ScoreDoc.COMPARATOR);
diff --git a/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java b/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java
index 27d88504b30..d818da08aed 100644
--- a/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java
@@ -119,9 +119,7 @@ public abstract class QueryRescorer extends Rescorer {
if (topN < hits.length) {
ArrayUtil.select(hits, 0, hits.length, topN, sortDocComparator);
- ScoreDoc[] subset = new ScoreDoc[topN];
- System.arraycopy(hits, 0, subset, 0, topN);
- hits = subset;
+ hits = ArrayUtil.copyOfSubArray(hits, 0, topN);
}
Arrays.sort(hits, sortDocComparator);
diff --git a/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java b/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
index 6fd711a4ba4..2d2d971056b 100644
--- a/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
@@ -257,8 +257,7 @@ public final class NumericUtils {
* @see #bigIntToSortableBytes(BigInteger, int, byte[], int)
*/
public static BigInteger sortableBytesToBigInt(byte[] encoded, int offset, int length) {
- byte[] bigIntBytes = new byte[length];
- System.arraycopy(encoded, offset, bigIntBytes, 0, length);
+ byte[] bigIntBytes = ArrayUtil.copyOfSubArray(encoded, offset, offset + length);
// Flip the sign bit back to the original
bigIntBytes[0] ^= 0x80;
return new BigInteger(bigIntBytes);
diff --git a/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java b/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
index e63b75418f9..7babf031d1e 100644
--- a/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
+++ b/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
@@ -226,9 +226,7 @@ public final class PagedBytes implements Accountable {
throw new IllegalStateException("cannot freeze when copy(BytesRef, BytesRef) was used");
}
if (trim && upto < blockSize) {
- final byte[] newBlock = new byte[upto];
- System.arraycopy(currentBlock, 0, newBlock, 0, upto);
- currentBlock = newBlock;
+ currentBlock = ArrayUtil.copyOfSubArray(currentBlock, 0, upto);
}
if (currentBlock == null) {
currentBlock = EMPTY_BYTES;
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
index 8c7185393f6..925201b2c5d 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
@@ -1372,9 +1372,7 @@ public final class Operations {
if (upto < states.length) {
// There were dead states
- int[] newStates = new int[upto];
- System.arraycopy(states, 0, newStates, 0, upto);
- states = newStates;
+ states = ArrayUtil.copyOfSubArray(states, 0, upto);
}
// Reverse the order:
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/FloatHeap.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/FloatHeap.java
index e1a267a3c01..dd86f8632b8 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/FloatHeap.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/FloatHeap.java
@@ -17,6 +17,8 @@
package org.apache.lucene.util.hnsw;
+import org.apache.lucene.util.ArrayUtil;
+
/**
* A bounded min heap that stores floats. The top element is the lowest value of the heap.
*
@@ -59,9 +61,7 @@ public final class FloatHeap {
}
public float[] getHeap() {
- float[] result = new float[size];
- System.arraycopy(this.heap, 1, result, 0, size);
- return result;
+ return ArrayUtil.copyOfSubArray(this.heap, 1, 1 + size);
}
/**