You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "ChrisHegarty (via GitHub)" <gi...@apache.org> on 2024/05/10 16:03:23 UTC

[PR] Fix default flat vector scorer supplier sharing backing array [lucene]

ChrisHegarty opened a new pull request, #13355:
URL: https://github.com/apache/lucene/pull/13355

   This commit fixes an in issue in the default flat vector scorer supplier whereby subsequent scorers created by the supplier can affect previously created scorers.
   
   The issue is that we're sharing the backing array from the vector values, and overwriting it in subsequent scorers. We just need to copy the array to protect the scorer instance from mutation.
   
   The test is a little verbose, but duplicates the issue and is extended in another PR to cover more flat vector scorer implementations.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix default flat vector scorer supplier sharing backing array [lucene]

Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty merged PR #13355:
URL: https://github.com/apache/lucene/pull/13355


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix default flat vector scorer supplier sharing backing array [lucene]

Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on code in PR #13355:
URL: https://github.com/apache/lucene/pull/13355#discussion_r1596969665


##########
lucene/core/src/java/org/apache/lucene/codecs/hnsw/DefaultFlatVectorScorer.java:
##########
@@ -101,7 +102,9 @@ private ByteScoringSupplier(
 
     @Override
     public RandomVectorScorer scorer(int ord) throws IOException {
-      return new ByteVectorScorer(vectors2, vectors1.vectorValue(ord), similarityFunction);
+      byte[] ba = vectors1.vectorValue(ord);
+      return new ByteVectorScorer(
+          vectors2, ArrayUtil.copyOfSubArray(ba, 0, ba.length), similarityFunction);

Review Comment:
   Good catch. The prior implementation stored the ord rather than the vector bytes. We should do the same here, which will avoid the copy. The scorers (not the supplier), do not suffer a similar issue, since their API doe not allow to score against anything other than the first vector.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix default flat vector scorer supplier sharing backing array [lucene]

Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on code in PR #13355:
URL: https://github.com/apache/lucene/pull/13355#discussion_r1596969665


##########
lucene/core/src/java/org/apache/lucene/codecs/hnsw/DefaultFlatVectorScorer.java:
##########
@@ -101,7 +102,9 @@ private ByteScoringSupplier(
 
     @Override
     public RandomVectorScorer scorer(int ord) throws IOException {
-      return new ByteVectorScorer(vectors2, vectors1.vectorValue(ord), similarityFunction);
+      byte[] ba = vectors1.vectorValue(ord);
+      return new ByteVectorScorer(
+          vectors2, ArrayUtil.copyOfSubArray(ba, 0, ba.length), similarityFunction);

Review Comment:
   Good catch. The prior implementation stored the old rather than the vector bytes. We should do the same here, which will avoid the copy. The scorers (not the supplier), do not suffer a similar issue, since their API doe not allow to score against anything other than the first vector.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix default flat vector scorer supplier sharing backing array [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on code in PR #13355:
URL: https://github.com/apache/lucene/pull/13355#discussion_r1596942824


##########
lucene/core/src/java/org/apache/lucene/codecs/hnsw/DefaultFlatVectorScorer.java:
##########
@@ -101,7 +102,9 @@ private ByteScoringSupplier(
 
     @Override
     public RandomVectorScorer scorer(int ord) throws IOException {
-      return new ByteVectorScorer(vectors2, vectors1.vectorValue(ord), similarityFunction);
+      byte[] ba = vectors1.vectorValue(ord);
+      return new ByteVectorScorer(
+          vectors2, ArrayUtil.copyOfSubArray(ba, 0, ba.length), similarityFunction);

Review Comment:
   My concern is that now for every scorer, we are now creating a new array.
   
   For example, in the HNSW graph builder, now instead of just creating a couple of backing arrays per thread, we create and require GC to collect a new one for every node added. This increases the garbage created by millions of arrays.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix default flat vector scorer supplier sharing backing array [lucene]

Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on code in PR #13355:
URL: https://github.com/apache/lucene/pull/13355#discussion_r1596969665


##########
lucene/core/src/java/org/apache/lucene/codecs/hnsw/DefaultFlatVectorScorer.java:
##########
@@ -101,7 +102,9 @@ private ByteScoringSupplier(
 
     @Override
     public RandomVectorScorer scorer(int ord) throws IOException {
-      return new ByteVectorScorer(vectors2, vectors1.vectorValue(ord), similarityFunction);
+      byte[] ba = vectors1.vectorValue(ord);
+      return new ByteVectorScorer(
+          vectors2, ArrayUtil.copyOfSubArray(ba, 0, ba.length), similarityFunction);

Review Comment:
   Good catch. The prior implementation stored the ord rather than the vector bytes. We should do the same here, which will avoid the copy. The scorers (not the supplier), do not suffer a similar issue, since their API does not allow to score against anything other than the first vector.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org