From d2ea0046a9b99ace3f3b7c91e1d334d45f0f2292 Mon Sep 17 00:00:00 2001 From: Bruno Roustant <33934988+bruno-roustant@users.noreply.github.com> Date: Tue, 14 May 2024 11:32:18 +0200 Subject: [PATCH] Call ArrayUtil.copyArray instead of ArrayUtil.copySubArray for full array copy. (#13360) --- .../hnsw/ScalarQuantizedVectorScorer.java | 2 +- .../lucene/search/BlendedTermQuery.java | 2 +- .../lucene/search/KnnByteVectorQuery.java | 2 +- .../lucene/search/KnnFloatVectorQuery.java | 2 +- .../org/apache/lucene/util/ArrayUtil.java | 40 +++++++++++++++++++ .../apache/lucene/search/TestBoolean2.java | 2 +- .../apache/lucene/search/TestPhraseQuery.java | 2 +- .../TestSimpleExplanationsWithFillerDocs.java | 2 +- .../apache/lucene/util/BaseSortTestCase.java | 2 +- .../util/hnsw/MockByteVectorValues.java | 5 +-- .../lucene/util/hnsw/MockVectorValues.java | 5 +-- .../TestScalarQuantizedVectorSimilarity.java | 4 +- .../lucene/spatial/TestDistanceStrategy.java | 2 +- 13 files changed, 53 insertions(+), 19 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java b/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java index a4f339dda4b8..bc9c66a1febd 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java @@ -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; diff --git a/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java index 05d868194860..8b7d3e80fcda 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java @@ -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) { diff --git a/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java index e3d733e516f4..5b60e680e100 100644 --- a/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java @@ -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); } } diff --git a/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java index 91cf4474e1cc..b06e81eb5d82 100644 --- a/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java @@ -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); } } diff --git a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java index 336b5b3e6bbb..722df2ff6879 100644 --- a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java +++ b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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[] copyArray(T[] array) { + return copyOfSubArray(array, 0, array.length); + } + /** * Copies the specified range of the given array into a new sub array. * diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java b/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java index ccb8c954c284..b0634e56da42 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java @@ -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]); } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java index 05cb9df5397a..f91f1a5e6f4c 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java @@ -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(); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java index 93dd4402c4c9..f799cb896a5c 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java @@ -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]); } diff --git a/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java b/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java index e887448578f1..ba4ed6fa82be 100644 --- a/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java +++ b/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java @@ -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); diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java index fdc2566c022e..a3b17b9a621e 100644 --- a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java +++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java @@ -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 diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java index c10f80e20a85..f183f6c99a67 100644 --- a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java +++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java @@ -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 diff --git a/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java b/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java index 809cb8d13b2d..bdba822d4eca 100644 --- a/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java +++ b/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java @@ -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); @@ -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; } diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java index e3cf440d2b1d..31212621e4a6 100644 --- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java +++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java @@ -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; }