Call ArrayUtil.copyArray instead of ArrayUtil.copySubArray for full array copy. (#13360)

This commit is contained in:
Bruno Roustant 2024-05-14 11:32:18 +02:00 committed by GitHub
parent fc12cc1847
commit bcb62f56ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 53 additions and 19 deletions

View File

@ -44,7 +44,7 @@ public class ScalarQuantizedVectorScorer implements FlatVectorsScorer {
switch (similarityFunction) { switch (similarityFunction) {
case EUCLIDEAN, DOT_PRODUCT, MAXIMUM_INNER_PRODUCT -> query; case EUCLIDEAN, DOT_PRODUCT, MAXIMUM_INNER_PRODUCT -> query;
case COSINE -> { case COSINE -> {
float[] queryCopy = ArrayUtil.copyOfSubArray(query, 0, query.length); float[] queryCopy = ArrayUtil.copyArray(query);
VectorUtil.l2normalize(queryCopy); VectorUtil.l2normalize(queryCopy);
yield queryCopy; yield queryCopy;
} }

View File

@ -268,7 +268,7 @@ public final class BlendedTermQuery extends Query {
@Override @Override
public final Query rewrite(IndexSearcher indexSearcher) throws IOException { 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) { for (int i = 0; i < contexts.length; ++i) {
if (contexts[i] == null if (contexts[i] == null
|| contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext()) == false) { || contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext()) == false) {

View File

@ -128,6 +128,6 @@ public class KnnByteVectorQuery extends AbstractKnnVectorQuery {
* @return the target query vector of the search. Each vector element is a byte. * @return the target query vector of the search. Each vector element is a byte.
*/ */
public byte[] getTargetCopy() { public byte[] getTargetCopy() {
return ArrayUtil.copyOfSubArray(target, 0, target.length); return ArrayUtil.copyArray(target);
} }
} }

View File

@ -131,6 +131,6 @@ public class KnnFloatVectorQuery extends AbstractKnnVectorQuery {
* @return the target query vector of the search. Each vector element is a float. * @return the target query vector of the search. Each vector element is a float.
*/ */
public float[] getTargetCopy() { public float[] getTargetCopy() {
return ArrayUtil.copyOfSubArray(target, 0, target.length); return ArrayUtil.copyArray(target);
} }
} }

View File

@ -623,6 +623,11 @@ public final class ArrayUtil {
}.select(from, to, k); }.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. * Copies the specified range of the given array into a new sub array.
* *
@ -636,6 +641,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *
@ -649,6 +659,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *
@ -662,6 +677,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *
@ -675,6 +695,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *
@ -688,6 +713,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *
@ -701,6 +731,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *
@ -714,6 +749,11 @@ public final class ArrayUtil {
return copy; 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. * Copies the specified range of the given array into a new sub array.
* *

View File

@ -229,7 +229,7 @@ public class TestBoolean2 extends LuceneTestCase {
// adjust the expected doc numbers according to our filler docs // adjust the expected doc numbers according to our filler docs
if (0 < NUM_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++) { for (int i = 0; i < expDocNrs.length; i++) {
expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]); expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
} }

View File

@ -755,7 +755,7 @@ public class TestPhraseQuery extends LuceneTestCase {
public void testTopPhrases() throws IOException { public void testTopPhrases() throws IOException {
Directory dir = newDirectory(); Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); 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()); Collections.shuffle(Arrays.asList(docs), random());
for (String value : DOCS) { for (String value : DOCS) {
Document doc = new Document(); Document doc = new Document();

View File

@ -103,7 +103,7 @@ public class TestSimpleExplanationsWithFillerDocs extends TestSimpleExplanations
@Override @Override
public void qtest(Query q, int[] expDocNrs) throws Exception { 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++) { for (int i = 0; i < expDocNrs.length; i++) {
expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]); expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
} }

View File

@ -49,7 +49,7 @@ public abstract class BaseSortTestCase extends LuceneTestCase {
public void assertSorted(Entry[] original, Entry[] sorted) { public void assertSorted(Entry[] original, Entry[] sorted) {
assertEquals(original.length, sorted.length); assertEquals(original.length, sorted.length);
Entry[] actuallySorted = ArrayUtil.copyOfSubArray(original, 0, original.length); Entry[] actuallySorted = ArrayUtil.copyArray(original);
Arrays.sort(actuallySorted); Arrays.sort(actuallySorted);
for (int i = 0; i < original.length; ++i) { for (int i = 0; i < original.length; ++i) {
assertEquals(actuallySorted[i].value, sorted[i].value); assertEquals(actuallySorted[i].value, sorted[i].value);

View File

@ -50,10 +50,7 @@ class MockByteVectorValues extends AbstractMockVectorValues<byte[]>
@Override @Override
public MockByteVectorValues copy() { public MockByteVectorValues copy() {
return new MockByteVectorValues( return new MockByteVectorValues(
ArrayUtil.copyOfSubArray(values, 0, values.length), ArrayUtil.copyArray(values), dimension, ArrayUtil.copyArray(denseValues), numVectors);
dimension,
ArrayUtil.copyOfSubArray(denseValues, 0, denseValues.length),
numVectors);
} }
@Override @Override

View File

@ -50,10 +50,7 @@ class MockVectorValues extends AbstractMockVectorValues<float[]>
@Override @Override
public MockVectorValues copy() { public MockVectorValues copy() {
return new MockVectorValues( return new MockVectorValues(
ArrayUtil.copyOfSubArray(values, 0, values.length), ArrayUtil.copyArray(values), dimension, ArrayUtil.copyArray(denseValues), numVectors);
dimension,
ArrayUtil.copyOfSubArray(denseValues, 0, denseValues.length),
numVectors);
} }
@Override @Override

View File

@ -229,7 +229,7 @@ public class TestScalarQuantizedVectorSimilarity extends LuceneTestCase {
int i = 0; int i = 0;
float[] offsets = new float[floats.length]; float[] offsets = new float[floats.length];
for (float[] f : floats) { for (float[] f : floats) {
float[] v = ArrayUtil.copyOfSubArray(f, 0, f.length); float[] v = ArrayUtil.copyArray(f);
VectorUtil.l2normalize(v); VectorUtil.l2normalize(v);
quantized[i] = new byte[v.length]; quantized[i] = new byte[v.length];
offsets[i] = scalarQuantizer.quantize(v, quantized[i], similarityFunction); offsets[i] = scalarQuantizer.quantize(v, quantized[i], similarityFunction);
@ -246,7 +246,7 @@ public class TestScalarQuantizedVectorSimilarity extends LuceneTestCase {
if (curDoc == -1 || curDoc >= floats.length) { if (curDoc == -1 || curDoc >= floats.length) {
throw new IOException("Current doc not set or too many iterations"); 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); VectorUtil.l2normalize(v);
return v; return v;
} }

View File

@ -108,7 +108,7 @@ public class TestDistanceStrategy extends StrategyTestCase {
void checkDistValueSource(Point pt, float... distances) throws IOException { void checkDistValueSource(Point pt, float... distances) throws IOException {
float multiplier = random().nextFloat() * 100f; 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++) { for (int i = 0; i < dists2.length; i++) {
dists2[i] *= multiplier; dists2[i] *= multiplier;
} }