kaivalnp commented on code in PR #15979: URL: https://github.com/apache/lucene/pull/15979#discussion_r3647565541
########## lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlushContext.java: ########## @@ -0,0 +1,268 @@ +/* + * 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 static java.nio.ByteOrder.LITTLE_ENDIAN; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.ORD_UNKNOWN; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.alignBytes; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.hashBytes; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeEndOfFields; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeEndOfGroups; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeFieldInfo; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeGroupInfo; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.lucene.codecs.KnnVectorsWriter; +import org.apache.lucene.codecs.hnsw.FlatFieldVectorsWriter; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.GroupInfo; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.GroupKey; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.OrdToVecOrd; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.OrdToVecOrdArrayList; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.OrdToVecOrdMappedArrayList; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.WriteFieldInfo; +import org.apache.lucene.index.DocsWithFieldSet; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.Sorter; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.internal.hppc.IntArrayList; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.Accountable; +import org.apache.lucene.util.RamUsageEstimator; + +/** + * Buffers vectors added during a flush and de-duplicates them in memory. Fields sharing a {@link + * DedupUtil.GroupKey} intern into the same {@link DedupGroup}; on {@link #flush} each group's + * distinct vectors are written once, followed by per-field metadata mapping document ordinals to + * group ordinals. + * + * @lucene.experimental + */ +final class DedupFlushContext implements Accountable { + private final Map<GroupKey, DedupGroup<?>> groups; + private final List<FieldData> fieldDataList; + + DedupFlushContext() { + this.groups = new HashMap<>(); + this.fieldDataList = new ArrayList<>(); + } + + private static DedupGroup<?> getGroup(GroupKey groupKey) { + return switch (groupKey.encoding()) { + case BYTE -> new ByteGroup(groupKey.dimension()); + case FLOAT32 -> new FloatGroup(groupKey.dimension()); + }; + } + + @Override + public long ramBytesUsed() { + long total = 0; + for (DedupGroup<?> group : groups.values()) { + total += group.ramBytesUsed(); + } + for (FieldData data : fieldDataList) { + total += data.ramBytesUsed(); + } + return total; + } + + FlatFieldVectorsWriter<?> addField(FieldInfo fieldInfo) { + GroupKey groupKey = new GroupKey(fieldInfo); + DedupGroup<?> group = groups.computeIfAbsent(groupKey, DedupFlushContext::getGroup); + DedupFlatFieldVectorsWriter<?> fieldVectorsWriter = new DedupFlatFieldVectorsWriter<>(group); + + fieldDataList.add(new FieldData(fieldInfo, groupKey, fieldVectorsWriter)); + return fieldVectorsWriter; + } + + void flush(IndexOutput meta, IndexOutput vectorData, int maxDoc, Sorter.DocMap sortMap) + throws IOException { + + Map<GroupKey, Integer> groupOrds = new HashMap<>(); + + int groupOrd = 0; + for (Map.Entry<GroupKey, DedupGroup<?>> entry : groups.entrySet()) { + GroupKey groupKey = entry.getKey(); + DedupGroup<?> group = entry.getValue(); + + int groupSize = group.size(); + long vectorDataOffset = alignBytes(vectorData, groupKey.encoding()); + + // TODO: Write in sorted order for faster merge? (with sequential IO) + for (int ord = 0; ord < groupSize; ord++) { + byte[] bytes = group.serialize(ord); Review Comment: None of the current implementations allocate a new `byte[]` for every call. The flush-byte implementation [returns the array held on-heap](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlushContext.java#L194-L197) (which is already kept on-heap for other things like HNSW graph construction). The flush-float implementation [allocates a single array per-group](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlushContext.java#L215-L220), which is [re-used for serialization](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlushContext.java#L244-L251). For the merge implementations (both [byte](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupMergeContext.java#L218-L221) and [float](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupMergeContext.java#L288-L295)), the underlying vector values use a single buffer to load the vector into a `byte[]` or `float[]`, and the float-merge implementation [re-uses a buffer](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupMergeContext.java#L247-L252) to convert `float[]` to `byte[]` like the flush path. I was avoiding the caller having to pass their own scratch, because it is an unnecessary copy for the byte paths (both flush and merge), and I also wanted the caller to avoid having to size the scratch buffer themselves. -- 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]
