kaivalnp commented on code in PR #15979:
URL: https://github.com/apache/lucene/pull/15979#discussion_r3666926979


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupGroup.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.codecs.lucene106.dedup;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.lucene.internal.hppc.LongIntHashMap;
+import org.apache.lucene.internal.hppc.ObjectCursor;
+import org.apache.lucene.util.Accountable;
+
+/**
+ * Interns vectors so that each distinct value is stored once. {@link 
#addUnique} returns the group
+ * ordinal for a vector, adding it (via {@link #copy}) only if not already 
present. Callers with the
+ * same {@code (dimension, encoding)} share a group, so an identical vector 
across fields is stored
+ * a single time.
+ *
+ * <p>Not thread-safe; a group is confined to the writer that created it.
+ *
+ * @lucene.experimental
+ */
+abstract sealed class DedupGroup<T> implements Accountable
+    permits DedupFlushContext.ByteGroup,
+        DedupFlushContext.FloatGroup,
+        DedupFlushContext.Float16Group,
+        DedupMergeContext.DedupMergeGroup {
+
+  /**
+   * Map for vector hash -> group ord. Only a hint and not the ground truth 
due to possibility of
+   * hash collisions, where a full equality check must be performed.

Review Comment:
   > how often you hit false hash collisions
   
   I don't have this handy -- but it seems measurable in offline runs with 
local changes..
   
   A good hash function is indeed important because it reduces unnecessary 
`equals` checks during flush, and unnecessary index IO + `equals` checks during 
merge.
   
   In earlier local iterations, I kept the hash function as an `int` and used 
`Arrays.hashCode` for a simple start -- which was producing a measurably slower 
indexing time (by a few %) compared to the current `long` hash using 
[murmurhash3](https://github.com/apache/lucene/blob/d18845e6bbfaecf9421579c76fba0d38edde5711/lucene/core/src/java/org/apache/lucene/util/StringHelper.java#L226)
 on byte representations (even with the overhead of converting `float[]` -> 
`byte[]` for computing the hash).
   
   I'm not aware of a better hash function for `float[]` and `float16[]` 
vectors, and ideally one where conversion to `byte[]` is not necessary.
   
   I also found the interesting [Birthday 
Problem](https://en.wikipedia.org/wiki/Birthday_problem) to be relevant here -- 
where hash collisions become a near guarantee with a large number of vectors, 
even with a "good" hashing function.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to