mikemccand commented on code in PR #15979: URL: https://github.com/apache/lucene/pull/15979#discussion_r4168808020
########## lucene/core/src/java/org/apache/lucene/codecs/dedup/DedupFlatVectorsReader.java: ########## @@ -0,0 +1,421 @@ +/* + * 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.dedup; + +import java.io.IOException; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; +import org.apache.lucene.codecs.CodecUtil; +import org.apache.lucene.codecs.hnsw.FlatVectorsReader; +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; +import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FieldInfos; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.KnnVectorValues; +import org.apache.lucene.index.SegmentReadState; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.internal.hppc.IntObjectHashMap; +import org.apache.lucene.store.ChecksumIndexInput; +import org.apache.lucene.store.DataAccessHint; +import org.apache.lucene.store.FileDataHint; +import org.apache.lucene.store.FileTypeHint; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IOContext.FileOpenHint; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.RandomAccessInput; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.LongValues; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.packed.DirectReader; + +/** + * Reads a {@link DedupFlatVectorsFormat} segment. + * + * <p>Layout: {@code .dvc} holds pool vector bytes (contiguous per pool) followed by per-field DISI + * + packed {@code docOrd → vecOrd} maps. {@code .dvm} holds pool/field metadata. + * + * @lucene.experimental + */ +public final class DedupFlatVectorsReader extends FlatVectorsReader { + + private static final long SHALLOW_SIZE = + RamUsageEstimator.shallowSizeOfInstance(DedupFlatVectorsReader.class); + + private final FlatVectorsScorer vectorScorer; + private final FlatVectorsScorer translatingScorer; + private final FieldInfos fieldInfos; + private final IndexInput vectorData; + private final IOContext dataContext; + + private final IntObjectHashMap<FieldEntry> fields = new IntObjectHashMap<>(); + + public DedupFlatVectorsReader(SegmentReadState state, FlatVectorsScorer scorer) + throws IOException { + this(state, scorer, DataAccessHint.RANDOM); Review Comment: Curiously, this is a mixed case where I would want to hint the OS that I am mostly reading this file `MADV_SEQUENTIAL` (say, in the context of a merge or a query visiting hits in docid order for a segment wanting to load the vector to return or compute dot product), and then on the relatively rare (typically?) seek-back because you have a dup vector with an earlier docid, you'd want that one IO to be hinted as `MADV_RANDOM`, which Linux cannot do. These flags are global to a region of virtual memory (`madvise(...)`), not per page fault/read. Hmm, actually, you could map the same region twice, and I think the kernel keeps the accounting separate, yet will properly share a single copy of the data in the buffer cache. OK I asked Claude Opus 5.5 Medium: https://claude.ai/share/e3c1b5ea-a22e-42b1-ab26-44fdf22c91eb ... seems like we would also `open(...)` the file to get a unique fd for each "purpose" and then it could work well? It's not risk free -- it adds minor page fault costs for Linux to keep the two maps "in sync" (loading a given page from the file only once). This might be cleaner that what Lucene tries to do today (changing the hint during merge even when queries are using that same map)? I'll open a spinoff. Not sure it's a good idea... -- 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]
