Skip to content

Commit

Permalink
Call ArrayUtil.copyArray instead of ArrayUtil.copySubArray for full a…
Browse files Browse the repository at this point in the history
…rray copy. (#13360)
  • Loading branch information
bruno-roustant committed May 14, 2024
1 parent 3fa2ddc commit d2ea004
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static float quantizeQuery(
processedQuery = query;
break;
case COSINE:
float[] queryCopy = ArrayUtil.copyOfSubArray(query, 0, query.length);
float[] queryCopy = ArrayUtil.copyArray(query);
VectorUtil.l2normalize(queryCopy);
processedQuery = queryCopy;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public String toString(String field) {

@Override
public final Query rewrite(IndexSearcher indexSearcher) throws IOException {
final TermStates[] contexts = ArrayUtil.copyOfSubArray(this.contexts, 0, this.contexts.length);
final TermStates[] contexts = ArrayUtil.copyArray(this.contexts);
for (int i = 0; i < contexts.length; ++i) {
if (contexts[i] == null
|| contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext()) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ public int hashCode() {
* @return the target query vector of the search. Each vector element is a byte.
*/
public byte[] getTargetCopy() {
return ArrayUtil.copyOfSubArray(target, 0, target.length);
return ArrayUtil.copyArray(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ public int hashCode() {
* @return the target query vector of the search. Each vector element is a float.
*/
public float[] getTargetCopy() {
return ArrayUtil.copyOfSubArray(target, 0, target.length);
return ArrayUtil.copyArray(target);
}
}
40 changes: 40 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ protected int comparePivot(int j) {
}.select(from, to, k);
}

/** Copies an array into a new array. */
public static byte[] copyArray(byte[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -636,6 +641,11 @@ public static byte[] copyOfSubArray(byte[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static char[] copyArray(char[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -649,6 +659,11 @@ public static char[] copyOfSubArray(char[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static short[] copyArray(short[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -662,6 +677,11 @@ public static short[] copyOfSubArray(short[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static int[] copyArray(int[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -675,6 +695,11 @@ public static int[] copyOfSubArray(int[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static long[] copyArray(long[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -688,6 +713,11 @@ public static long[] copyOfSubArray(long[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static float[] copyArray(float[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -701,6 +731,11 @@ public static float[] copyOfSubArray(float[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static double[] copyArray(double[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand All @@ -714,6 +749,11 @@ public static double[] copyOfSubArray(double[] array, int from, int to) {
return copy;
}

/** Copies an array into a new array. */
public static <T> T[] copyArray(T[] array) {
return copyOfSubArray(array, 0, array.length);
}

/**
* Copies the specified range of the given array into a new sub array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void queriesTest(Query query, int[] expDocNrs) throws Exception {

// adjust the expected doc numbers according to our filler docs
if (0 < NUM_FILLER_DOCS) {
expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);
expDocNrs = ArrayUtil.copyArray(expDocNrs);
for (int i = 0; i < expDocNrs.length; i++) {
expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public void testBackwardPositions() throws Exception {
public void testTopPhrases() throws IOException {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
String[] docs = ArrayUtil.copyOfSubArray(DOCS, 0, DOCS.length);
String[] docs = ArrayUtil.copyArray(DOCS);
Collections.shuffle(Arrays.asList(docs), random());
for (String value : DOCS) {
Document doc = new Document();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static Document makeFillerDoc() {
@Override
public void qtest(Query q, int[] expDocNrs) throws Exception {

expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);
expDocNrs = ArrayUtil.copyArray(expDocNrs);
for (int i = 0; i < expDocNrs.length; i++) {
expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BaseSortTestCase(boolean stable) {

public void assertSorted(Entry[] original, Entry[] sorted) {
assertEquals(original.length, sorted.length);
Entry[] actuallySorted = ArrayUtil.copyOfSubArray(original, 0, original.length);
Entry[] actuallySorted = ArrayUtil.copyArray(original);
Arrays.sort(actuallySorted);
for (int i = 0; i < original.length; ++i) {
assertEquals(actuallySorted[i].value, sorted[i].value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ static MockByteVectorValues fromValues(byte[][] values) {
@Override
public MockByteVectorValues copy() {
return new MockByteVectorValues(
ArrayUtil.copyOfSubArray(values, 0, values.length),
dimension,
ArrayUtil.copyOfSubArray(denseValues, 0, denseValues.length),
numVectors);
ArrayUtil.copyArray(values), dimension, ArrayUtil.copyArray(denseValues), numVectors);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ static MockVectorValues fromValues(float[][] values) {
@Override
public MockVectorValues copy() {
return new MockVectorValues(
ArrayUtil.copyOfSubArray(values, 0, values.length),
dimension,
ArrayUtil.copyOfSubArray(denseValues, 0, denseValues.length),
numVectors);
ArrayUtil.copyArray(values), dimension, ArrayUtil.copyArray(denseValues), numVectors);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private static float[] quantizeVectorsNormalized(
int i = 0;
float[] offsets = new float[floats.length];
for (float[] f : floats) {
float[] v = ArrayUtil.copyOfSubArray(f, 0, f.length);
float[] v = ArrayUtil.copyArray(f);
VectorUtil.l2normalize(v);
quantized[i] = new byte[v.length];
offsets[i] = scalarQuantizer.quantize(v, quantized[i], similarityFunction);
Expand All @@ -246,7 +246,7 @@ public float[] vectorValue() throws IOException {
if (curDoc == -1 || curDoc >= floats.length) {
throw new IOException("Current doc not set or too many iterations");
}
float[] v = ArrayUtil.copyOfSubArray(floats[curDoc], 0, floats[curDoc].length);
float[] v = ArrayUtil.copyArray(floats[curDoc]);
VectorUtil.l2normalize(v);
return v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void testRecipScore() throws IOException {

void checkDistValueSource(Point pt, float... distances) throws IOException {
float multiplier = random().nextFloat() * 100f;
float[] dists2 = ArrayUtil.copyOfSubArray(distances, 0, distances.length);
float[] dists2 = ArrayUtil.copyArray(distances);
for (int i = 0; i < dists2.length; i++) {
dists2[i] *= multiplier;
}
Expand Down

0 comments on commit d2ea004

Please sign in to comment.