Skip to content

Commit f8c1f0e

Browse files
committed
Move output words on-heap
1 parent ecb9f91 commit f8c1f0e

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,38 +134,40 @@ public SynonymMap readSynonymMap(Directory directory) throws IOException {
134134
synonymMetadata.fstMetadata,
135135
directory.openInput(FST_FILE, IOContext.DEFAULT),
136136
new OffHeapFSTStore());
137-
IndexInput wordsInput = directory.openInput(WORDS_FILE, IOContext.READ);
138-
int[] bytesStartArray = new int[synonymMetadata.wordCount];
139-
for (int i = 0; i < synonymMetadata.wordCount; i++) {
140-
bytesStartArray[i] = Math.toIntExact(wordsInput.getFilePointer());
141-
int length = wordsInput.readVInt();
142-
wordsInput.seek(wordsInput.getFilePointer() + length);
137+
OnHeapBytesRefHashLike words;
138+
try (IndexInput wordsInput = directory.openInput(WORDS_FILE, IOContext.DEFAULT)) {
139+
words = new OnHeapBytesRefHashLike(synonymMetadata.wordCount, wordsInput);
143140
}
144-
return new SynonymMap(
145-
fst,
146-
new OffHeapBytesRefHashLike(bytesStartArray, wordsInput),
147-
synonymMetadata.maxHorizontalContext);
141+
return new SynonymMap(fst, words, synonymMetadata.maxHorizontalContext);
148142
}
149143

150-
private static class OffHeapBytesRefHashLike extends SynonymMap.BytesRefHashLike {
144+
private static class OnHeapBytesRefHashLike extends SynonymMap.BytesRefHashLike {
151145
private final int[] bytesStartArray;
152-
private final IndexInput wordsFile;
153-
154-
public OffHeapBytesRefHashLike(int[] bytesStartArray, IndexInput wordsFile) {
155-
this.bytesStartArray = bytesStartArray;
156-
this.wordsFile = wordsFile;
146+
private final byte[] wordBytes;
147+
148+
public OnHeapBytesRefHashLike(int wordCount, IndexInput wordsFile) throws IOException {
149+
bytesStartArray = new int[wordCount + 1];
150+
int pos = 0;
151+
for (int i = 0; i < wordCount; i++) {
152+
bytesStartArray[i] = pos;
153+
int size = wordsFile.readVInt();
154+
pos += size;
155+
wordsFile.seek(wordsFile.getFilePointer() + size);
156+
}
157+
bytesStartArray[wordCount] = pos;
158+
wordsFile.seek(0);
159+
wordBytes = new byte[pos];
160+
for (int i = 0; i < wordCount; i++) {
161+
int size = wordsFile.readVInt();
162+
wordsFile.readBytes(wordBytes, bytesStartArray[i], size);
163+
}
157164
}
158165

159166
@Override
160-
public void get(int id, BytesRef scratch) throws IOException {
161-
wordsFile.seek(bytesStartArray[id]);
162-
int length = wordsFile.readVInt();
163-
if (scratch.bytes.length < length) {
164-
scratch.bytes = new byte[length];
165-
}
166-
wordsFile.readBytes(scratch.bytes, 0, length);
167-
scratch.offset = 0;
168-
scratch.length = length;
167+
public void get(int id, BytesRef scratch) {
168+
scratch.bytes = wordBytes;
169+
scratch.offset = bytesStartArray[id];
170+
scratch.length = bytesStartArray[id + 1] - bytesStartArray[id];
169171
}
170172
}
171173

0 commit comments

Comments
 (0)