-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Move synonym map off-heap for SynonymGraphFilter #13054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| import org.apache.lucene.internal.hppc.IntArrayList; | ||
| import org.apache.lucene.internal.hppc.IntHashSet; | ||
| import org.apache.lucene.store.ByteArrayDataOutput; | ||
| import org.apache.lucene.store.IndexOutput; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.apache.lucene.util.BytesRefBuilder; | ||
| import org.apache.lucene.util.BytesRefHash; | ||
|
|
@@ -53,12 +54,24 @@ public class SynonymMap { | |
| public final FST<BytesRef> fst; | ||
|
|
||
| /** map<ord, outputword> */ | ||
| public final BytesRefHash words; | ||
| public final SynonymDictionary words; | ||
|
|
||
| /** maxHorizontalContext: maximum context we need on the tokenstream */ | ||
| public final int maxHorizontalContext; | ||
|
|
||
| public SynonymMap(FST<BytesRef> fst, BytesRefHash words, int maxHorizontalContext) { | ||
| this( | ||
| fst, | ||
| new SynonymDictionary() { | ||
| @Override | ||
| public void get(int id, BytesRef scratch) { | ||
| words.get(id, scratch); | ||
| } | ||
| }, | ||
| maxHorizontalContext); | ||
| } | ||
|
|
||
| SynonymMap(FST<BytesRef> fst, SynonymDictionary words, int maxHorizontalContext) { | ||
| this.fst = fst; | ||
| this.words = words; | ||
| this.maxHorizontalContext = maxHorizontalContext; | ||
|
|
@@ -218,12 +231,26 @@ public void add(CharsRef input, CharsRef output, boolean includeOrig) { | |
| add(input, countWords(input), output, countWords(output), includeOrig); | ||
| } | ||
|
|
||
| /** Builds an {@link SynonymMap} and returns it. */ | ||
| /** Buils a {@link SynonymMap} and returns it. */ | ||
| public SynonymMap build() throws IOException { | ||
| return build(null); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a {@link SynonymMap} and returns it. If directory is non-null, it will write the | ||
| * compiled SynonymMap to disk and return an off-heap version. | ||
| */ | ||
| public SynonymMap build(SynonymMapDirectory directory) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ability to save a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When implementing the new build() method which accepts a directory path parameter for off-heap SynonymMap storage, I encountered Current implementation looks like something like this: Error observed: |
||
| ByteSequenceOutputs outputs = ByteSequenceOutputs.getSingleton(); | ||
| // TODO: are we using the best sharing options? | ||
| FSTCompiler<BytesRef> fstCompiler = | ||
| new FSTCompiler.Builder<>(FST.INPUT_TYPE.BYTE4, outputs).build(); | ||
| FSTCompiler.Builder<BytesRef> fstCompilerBuilder = | ||
| new FSTCompiler.Builder<>(FST.INPUT_TYPE.BYTE4, outputs); | ||
| IndexOutput fstOutput = null; | ||
| if (directory != null) { | ||
| fstOutput = directory.fstOutput(); | ||
| fstCompilerBuilder.dataOutput(fstOutput); | ||
| } | ||
| FSTCompiler<BytesRef> fstCompiler = fstCompilerBuilder.build(); | ||
|
|
||
| BytesRefBuilder scratch = new BytesRefBuilder(); | ||
| ByteArrayDataOutput scratchOutput = new ByteArrayDataOutput(); | ||
|
|
@@ -290,11 +317,28 @@ public SynonymMap build() throws IOException { | |
| fstCompiler.add(Util.toUTF32(input, scratchIntsRef), scratch.toBytesRef()); | ||
| } | ||
|
|
||
| FST<BytesRef> fst = FST.fromFSTReader(fstCompiler.compile(), fstCompiler.getFSTReader()); | ||
| FST.FSTMetadata<BytesRef> fstMetaData = fstCompiler.compile(); | ||
| if (directory != null) { | ||
| fstOutput.close(); // TODO -- Should fstCompiler.compile take care of this? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the idea is a caller could in theory write multiple FSTs into a single |
||
| try (SynonymMapDirectory.WordsOutput wordsOutput = directory.wordsOutput()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better on-disk layout might be to write a single big But save all that for later! vInt prefix length encoding is fine for starters! Progress not perfection! |
||
| BytesRef scratchRef = new BytesRef(); | ||
| for (int i = 0; i < words.size(); i++) { | ||
| words.get(i, scratchRef); | ||
| wordsOutput.addWord(scratchRef); | ||
| } | ||
| } | ||
| directory.writeMetadata(words.size(), maxHorizontalContext, fstMetaData); | ||
| return directory.readMap(); | ||
| } | ||
| FST<BytesRef> fst = FST.fromFSTReader(fstMetaData, fstCompiler.getFSTReader()); | ||
| return new SynonymMap(fst, words, maxHorizontalContext); | ||
| } | ||
| } | ||
|
|
||
| abstract static class SynonymDictionary { | ||
| public abstract void get(int id, BytesRef scratch) throws IOException; | ||
| } | ||
|
|
||
| /** | ||
| * Abstraction for parsing synonym files. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| * 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.analysis.synonym; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.store.FSDirectory; | ||
| import org.apache.lucene.store.IOContext; | ||
| import org.apache.lucene.store.IndexInput; | ||
| import org.apache.lucene.store.IndexOutput; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.apache.lucene.util.fst.ByteSequenceOutputs; | ||
| import org.apache.lucene.util.fst.FST; | ||
| import org.apache.lucene.util.fst.OffHeapFSTStore; | ||
|
|
||
| /** | ||
| * Wraps an {@link FSDirectory} to read and write a compiled {@link SynonymMap}. When reading, the | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm any reason why it must be an |
||
| * FST and output words are kept off-heap. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does the user control separately whether FST and output words are off heap or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, if you're using If you don't use The numbers I posted in #13054 (comment) (which, granted, was just a single synthetic benchmark) seemed to suggest (to me, at least) that the "sweet spot" is off-heap FST with on-heap words. The performance hit from moving words off-heap (at least with my implementation) was pretty bad. Lots of I'm happy to bring back off-heap words as an option if we think someone would be willing to take that perf hit for slightly lower heap utilization. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm can you fix the javadoc above to explain that words are on-heap and FST is off-heap? +1 to default to that sweet spot. I wonder in practice what the "typical" size of FST vs words is? Like does the FST dominate the storage? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Aha, you answered this in an earlier comment:
It's wild that the FST is so much larger than the words... I'm not yet understanding why. |
||
| * | ||
| * @lucene.experimental | ||
| */ | ||
| public class SynonymMapDirectory implements Closeable { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mark this with Or: could this be package private? Does the user need to create this wrapper themselves for some reason? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking that the user would create a Given the issue you called out below regarding the need to Alternatively, I'd be happy to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we ever want to bring back the off-heap words, it seems like |
||
| private final SynonymMapFormat synonymMapFormat = | ||
| new SynonymMapFormat(); // TODO -- Should this be more flexible/codec-like? Less? | ||
| private final Directory directory; | ||
| private final List<Closeable> resources = new ArrayList<>(); | ||
|
|
||
| public SynonymMapDirectory(Path path) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to store several There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, since I split the synonyms across three files ( That said, I suppose it wouldn't be hard to combine those three into a single file (with a Then a single filesystem directory could have something like: What do you think? (I'm also happy to let each serialized There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave it as is for now (three separate files)? But let's mark things Hmm, also: will these synonym files be backwards compatible across releases? Across major releases? I would say we should not promise across major releases? Furthermore, we should enforce that not-promise, by writing the major release into the metadata somewhere and checking if that changed between writing and reading and throw a clear exception if so? Within minor releases maybe we allow backcompat? If so, we need to add some testing to confirm syns written in 10.x are still readable/usable in 10.y? |
||
| directory = FSDirectory.open(path); | ||
| } | ||
|
|
||
| IndexOutput fstOutput() throws IOException { | ||
| return synonymMapFormat.getFSTOutput(directory); | ||
| } | ||
|
|
||
| WordsOutput wordsOutput() throws IOException { | ||
| return synonymMapFormat.getWordsOutput(directory); | ||
| } | ||
|
|
||
| void writeMetadata(int wordCount, int maxHorizontalContext, FST.FSTMetadata<BytesRef> fstMetadata) | ||
| throws IOException { | ||
| synonymMapFormat.writeMetadata( | ||
| directory, new SynonymMetadata(wordCount, maxHorizontalContext, fstMetadata)); | ||
| } | ||
|
|
||
| SynonymMap readMap() throws IOException { | ||
| CloseableSynonymMap closeableSynonymMap = synonymMapFormat.readSynonymMap(directory); | ||
| resources.add(closeableSynonymMap); | ||
| return closeableSynonymMap.map; | ||
| } | ||
|
|
||
| boolean hasSynonyms() throws IOException { | ||
| // TODO should take the path to the synonyms file to compare file hash against file used to | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoa, what would this |
||
| // build the directory | ||
| return directory.listAll().length > 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| for (Closeable c : resources) { | ||
| c.close(); | ||
| } | ||
| directory.close(); | ||
| } | ||
|
|
||
| /** | ||
| * Abstraction to support writing individual output words to the directory. Should be closed after | ||
| * the last word is written. | ||
| */ | ||
| abstract static class WordsOutput implements Closeable { | ||
| public abstract void addWord(BytesRef word) throws IOException; | ||
| } | ||
|
|
||
| private record CloseableSynonymMap(SynonymMap map, IndexInput indexInput) implements Closeable { | ||
| @Override | ||
| public void close() throws IOException { | ||
| indexInput.close(); | ||
| } | ||
| } | ||
|
|
||
| private record SynonymMetadata( | ||
| int wordCount, int maxHorizontalContext, FST.FSTMetadata<BytesRef> fstMetadata) {} | ||
|
|
||
| private static class SynonymMapFormat { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to keep this format private for now. |
||
| private static final String FST_FILE = "synonyms.fst"; | ||
| private static final String WORDS_FILE = "synonyms.wrd"; | ||
| private static final String METADATA_FILE = "synonyms.mdt"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh yes up to three files, so I think it makes sense for user to pass |
||
|
|
||
| private IndexOutput getFSTOutput(Directory directory) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
| return directory.createOutput(FST_FILE, IOContext.DEFAULT); | ||
| } | ||
|
|
||
| private WordsOutput getWordsOutput(Directory directory) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| IndexOutput wordsOutput = directory.createOutput(WORDS_FILE, IOContext.DEFAULT); | ||
| return new WordsOutput() { | ||
| @Override | ||
| public void close() throws IOException { | ||
| wordsOutput.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public void addWord(BytesRef word) throws IOException { | ||
| wordsOutput.writeVInt(word.length); | ||
| wordsOutput.writeBytes(word.bytes, word.offset, word.length); | ||
| } | ||
| }; | ||
| } | ||
| ; | ||
|
|
||
| private void writeMetadata(Directory directory, SynonymMetadata synonymMetadata) | ||
| throws IOException { | ||
| try (IndexOutput metadataOutput = directory.createOutput(METADATA_FILE, IOContext.DEFAULT)) { | ||
| metadataOutput.writeVInt(synonymMetadata.wordCount); | ||
| metadataOutput.writeVInt(synonymMetadata.maxHorizontalContext); | ||
| synonymMetadata.fstMetadata.save(metadataOutput); | ||
| } | ||
| directory.sync(List.of(FST_FILE, WORDS_FILE, METADATA_FILE)); | ||
| } | ||
|
|
||
| private SynonymMetadata readMetadata(Directory directory) throws IOException { | ||
| try (IndexInput metadataInput = directory.openInput(METADATA_FILE, IOContext.READONCE)) { | ||
| int wordCount = metadataInput.readVInt(); | ||
| int maxHorizontalContext = metadataInput.readVInt(); | ||
| FST.FSTMetadata<BytesRef> fstMetadata = | ||
| FST.readMetadata(metadataInput, ByteSequenceOutputs.getSingleton()); | ||
| return new SynonymMetadata(wordCount, maxHorizontalContext, fstMetadata); | ||
| } | ||
| } | ||
|
|
||
| private CloseableSynonymMap readSynonymMap(Directory directory) throws IOException { | ||
| SynonymMetadata synonymMetadata = readMetadata(directory); | ||
| IndexInput in = directory.openInput(FST_FILE, IOContext.DEFAULT); | ||
| FST<BytesRef> fst = | ||
| FST.fromFSTReader( | ||
| synonymMetadata.fstMetadata, new OffHeapFSTStore(in, 0, synonymMetadata.fstMetadata)); | ||
| OnHeapSynonymDictionary words; | ||
| try (IndexInput wordsInput = directory.openInput(WORDS_FILE, IOContext.DEFAULT)) { | ||
| words = new OnHeapSynonymDictionary(synonymMetadata.wordCount, wordsInput); | ||
| } | ||
| SynonymMap map = new SynonymMap(fst, words, synonymMetadata.maxHorizontalContext); | ||
| return new CloseableSynonymMap(map, in); | ||
| } | ||
|
|
||
| private static class OnHeapSynonymDictionary extends SynonymMap.SynonymDictionary { | ||
| private final int[] bytesStartArray; | ||
| private final byte[] wordBytes; | ||
|
|
||
| private OnHeapSynonymDictionary(int wordCount, IndexInput wordsFile) throws IOException { | ||
| bytesStartArray = new int[wordCount + 1]; | ||
| int pos = 0; | ||
| for (int i = 0; i < wordCount; i++) { | ||
| bytesStartArray[i] = pos; | ||
| int size = wordsFile.readVInt(); | ||
| pos += size; | ||
| wordsFile.seek(wordsFile.getFilePointer() + size); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||
| } | ||
| bytesStartArray[wordCount] = pos; | ||
| wordsFile.seek(0); | ||
| wordBytes = new byte[pos]; | ||
| for (int i = 0; i < wordCount; i++) { | ||
| int size = wordsFile.readVInt(); | ||
| wordsFile.readBytes(wordBytes, bytesStartArray[i], size); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void get(int id, BytesRef scratch) { | ||
| scratch.bytes = wordBytes; | ||
| scratch.offset = bytesStartArray[id]; | ||
| scratch.length = bytesStartArray[id + 1] - bytesStartArray[id]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, waaaaay up above, the javadoc for
Builder, it mentionsFSTSynonymMaptwice -- can you fix those toSynonymMapinstead? That must be holdover from ancient naming...Also, it's a bit annoying that GH does not allow me to put comments on parts of the code you did not change :) I guess this is GH's appempt to keep me in "eyes on the prize" mode ... so I only comment on stuff changed in the PR.