-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Move synonym map off-heap for SynonymGraphFilter #13054
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 |
---|---|---|
@@ -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 | ||
* FST and output words are kept off-heap. | ||
* | ||
* @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 |
||
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 |
||
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 | ||
// 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 { | ||
private static final String FST_FILE = "synonyms.fst"; | ||
private static final String WORDS_FILE = "synonyms.wrd"; | ||
private static final String METADATA_FILE = "synonyms.mdt"; | ||
|
||
private IndexOutput getFSTOutput(Directory directory) throws IOException { | ||
return directory.createOutput(FST_FILE, IOContext.DEFAULT); | ||
} | ||
|
||
private WordsOutput getWordsOutput(Directory directory) throws IOException { | ||
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); | ||
} | ||
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, if you're using
SynonymMapDirectory
, you get off-heap FST and on-heap words.If you don't use
SynonymMapDirectory
(i.e. you're using the existing constructor or the arg-less version ofSynonymMap.Builder.build()
), then everything is on-heap like before.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
seek
ing involved. Also, the vast majority of heap savings came from moving the FST.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.