Skip to content

Commit 8f78d79

Browse files
committed
Use ArrayUtil#copyOfSubArray to simplify manual array copy patterns (#15826)
* Use ArrayUtil#copyOfSubArray to simplify manual array copy patterns
1 parent 2c62300 commit 8f78d79

File tree

12 files changed

+25
-37
lines changed

12 files changed

+25
-37
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Other
8585

8686
* GITHUB#15799: Exclude JNA Cleaner from thread leak detection by LuceneTestCase. (Alan Woodward)
8787

88+
* GITHUB#15826: Use ArrayUtil#copyOfSubArray to simplify manual array copy patterns. (Zhang Chao)
89+
8890
======================= Lucene 10.4.0 =======================
8991

9092
API Changes

lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ private static final class PendingTerm extends PendingEntry {
263263

264264
public PendingTerm(BytesRef term, BlockTermState state) {
265265
super(true);
266-
this.termBytes = new byte[term.length];
267-
System.arraycopy(term.bytes, term.offset, termBytes, 0, term.length);
266+
this.termBytes = ArrayUtil.copyOfSubArray(term.bytes, term.offset, term.offset + term.length);
268267
this.state = state;
269268
}
270269

lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ public void add(int value) {
277277
}
278278

279279
public int[] get() {
280-
final int[] arr = new int[upto];
281-
System.arraycopy(ints, 0, arr, 0, upto);
280+
final int[] arr = ArrayUtil.copyOfSubArray(ints, 0, upto);
282281
upto = 0;
283282
return arr;
284283
}
@@ -444,10 +443,9 @@ public DirectField(
444443
if (hasPayloads) {
445444
BytesRef payload = docsAndPositionsEnum.getPayload();
446445
if (payload != null) {
447-
byte[] payloadBytes = new byte[payload.length];
448-
System.arraycopy(
449-
payload.bytes, payload.offset, payloadBytes, 0, payload.length);
450-
payloads[upto][pos] = payloadBytes;
446+
payloads[upto][pos] =
447+
ArrayUtil.copyOfSubArray(
448+
payload.bytes, payload.offset, payload.offset + payload.length);
451449
}
452450
}
453451
posUpto++;
@@ -477,8 +475,7 @@ public DirectField(
477475

478476
// System.out.println(skipCount + " skips: " + field);
479477

480-
this.termBytes = new byte[termOffset];
481-
System.arraycopy(termBytes, 0, this.termBytes, 0, termOffset);
478+
this.termBytes = ArrayUtil.copyOfSubArray(termBytes, 0, termOffset);
482479

483480
// Pack skips:
484481
this.skips = new int[skipCount];

lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,7 @@ private static final class PendingTerm extends PendingEntry {
414414

415415
public PendingTerm(BytesRef term, BlockTermState state) {
416416
super(true);
417-
this.termBytes = new byte[term.length];
418-
System.arraycopy(term.bytes, term.offset, termBytes, 0, term.length);
417+
this.termBytes = ArrayUtil.copyOfSubArray(term.bytes, term.offset, term.offset + term.length);
419418
this.state = state;
420419
}
421420

lucene/core/src/java/org/apache/lucene/document/InetAddressRange.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import org.apache.lucene.document.RangeFieldQuery.QueryType;
2222
import org.apache.lucene.search.Query;
23+
import org.apache.lucene.util.ArrayUtil;
2324
import org.apache.lucene.util.BytesRef;
2425

2526
/**
@@ -182,10 +183,8 @@ protected String toString(byte[] ranges, int dimension) {
182183
* @return The string representation for the range at the provided dimension
183184
*/
184185
private static String toString(byte[] ranges, int dimension) {
185-
byte[] min = new byte[BYTES];
186-
System.arraycopy(ranges, 0, min, 0, BYTES);
187-
byte[] max = new byte[BYTES];
188-
System.arraycopy(ranges, BYTES, max, 0, BYTES);
186+
byte[] min = ArrayUtil.copyOfSubArray(ranges, 0, BYTES);
187+
byte[] max = ArrayUtil.copyOfSubArray(ranges, BYTES, BYTES + BYTES);
189188
return "[" + InetAddressPoint.decode(min) + " : " + InetAddressPoint.decode(max) + "]";
190189
}
191190
}

lucene/core/src/java/org/apache/lucene/index/IndexingChain.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,9 +1299,9 @@ private void invertTokenStream(int docID, IndexableField field, boolean first)
12991299
try {
13001300
termsHashPerField.add(invertState.termAttribute.getBytesRef(), docID);
13011301
} catch (MaxBytesLengthExceededException e) {
1302-
byte[] prefix = new byte[30];
13031302
BytesRef bigTerm = invertState.termAttribute.getBytesRef();
1304-
System.arraycopy(bigTerm.bytes, bigTerm.offset, prefix, 0, 30);
1303+
byte[] prefix =
1304+
ArrayUtil.copyOfSubArray(bigTerm.bytes, bigTerm.offset, bigTerm.offset + 30);
13051305
String msg =
13061306
"Document contains at least one immense term in field=\""
13071307
+ fieldInfo.name
@@ -1372,8 +1372,9 @@ private void invertTerm(int docID, IndexableField field, boolean first) throws I
13721372
try {
13731373
termsHashPerField.add(binaryValue, docID);
13741374
} catch (MaxBytesLengthExceededException e) {
1375-
byte[] prefix = new byte[30];
1376-
System.arraycopy(binaryValue.bytes, binaryValue.offset, prefix, 0, 30);
1375+
byte[] prefix =
1376+
ArrayUtil.copyOfSubArray(
1377+
binaryValue.bytes, binaryValue.offset, binaryValue.offset + 30);
13771378
String msg =
13781379
"Document contains at least one immense term in field=\""
13791380
+ fieldInfo.name

lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int top
7878

7979
if (topN < hits.length) {
8080
ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.COMPARATOR);
81-
ScoreDoc[] subset = new ScoreDoc[topN];
82-
System.arraycopy(hits, 0, subset, 0, topN);
83-
hits = subset;
81+
hits = ArrayUtil.copyOfSubArray(hits, 0, topN);
8482
}
8583
Arrays.sort(hits, ScoreDoc.COMPARATOR);
8684

lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int top
119119

120120
if (topN < hits.length) {
121121
ArrayUtil.select(hits, 0, hits.length, topN, sortDocComparator);
122-
ScoreDoc[] subset = new ScoreDoc[topN];
123-
System.arraycopy(hits, 0, subset, 0, topN);
124-
hits = subset;
122+
hits = ArrayUtil.copyOfSubArray(hits, 0, topN);
125123
}
126124

127125
Arrays.sort(hits, sortDocComparator);

lucene/core/src/java/org/apache/lucene/util/NumericUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ assert sortableBytesToBigInt(result, offset, bigIntSize).equals(bigInt)
256256
* @see #bigIntToSortableBytes(BigInteger, int, byte[], int)
257257
*/
258258
public static BigInteger sortableBytesToBigInt(byte[] encoded, int offset, int length) {
259-
byte[] bigIntBytes = new byte[length];
260-
System.arraycopy(encoded, offset, bigIntBytes, 0, length);
259+
byte[] bigIntBytes = ArrayUtil.copyOfSubArray(encoded, offset, offset + length);
261260
// Flip the sign bit back to the original
262261
bigIntBytes[0] ^= 0x80;
263262
return new BigInteger(bigIntBytes);

lucene/core/src/java/org/apache/lucene/util/PagedBytes.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,7 @@ public Reader freeze(boolean trim) {
226226
throw new IllegalStateException("cannot freeze when copy(BytesRef, BytesRef) was used");
227227
}
228228
if (trim && upto < blockSize) {
229-
final byte[] newBlock = new byte[upto];
230-
System.arraycopy(currentBlock, 0, newBlock, 0, upto);
231-
currentBlock = newBlock;
229+
currentBlock = ArrayUtil.copyOfSubArray(currentBlock, 0, upto);
232230
}
233231
if (currentBlock == null) {
234232
currentBlock = EMPTY_BYTES;

0 commit comments

Comments
 (0)