Skip to content

Commit 03ec07f

Browse files
committed
Add a method to just reset the BytesStore
1 parent a404e18 commit 03ec07f

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

lucene/core/src/java/org/apache/lucene/util/fst/BytesStore.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,19 @@ public long size() {
337337
return getPosition();
338338
}
339339

340+
/**
341+
* Similar to {@link #truncate(long)} with newLen=0 but keep the first block
342+
* to reduce GC.
343+
*/
344+
public void reset() {
345+
if (blocks.isEmpty()) {
346+
return;
347+
}
348+
nextWrite = 0;
349+
current = blocks.get(0);
350+
blocks.subList(1, blocks.size()).clear();
351+
}
352+
340353
/**
341354
* Pos must be less than the max position written so far! Ie, you cannot "grow" the file with
342355
* this!
@@ -371,13 +384,13 @@ public void finish() {
371384

372385
/** Writes all of our bytes to the target {@link FSTWriter}. */
373386
public void writeTo(FSTWriter out) throws IOException {
374-
// TODO: if the FSTWriter is also BytesStore we are doing double write
375-
// once to reverse the bytes and once to write to the BytesStore
376-
// maybe we should combine it into reverseAndWriteTo()?
377387
reverse(0, getPosition() - 1);
378-
finish();
379388
for (byte[] block : blocks) {
380-
out.writeBytes(block, 0, block.length);
389+
if (block == current) { // last block
390+
out.writeBytes(block, 0, nextWrite);
391+
} else {
392+
out.writeBytes(block, 0, block.length);
393+
}
381394
}
382395
}
383396

lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,6 @@ private CompiledNode compileNode(UnCompiledNode<T> nodeIn) throws IOException {
327327
} else {
328328
node = addNode(nodeIn);
329329
}
330-
// truncate the scratch writer
331-
// TODO: Rather than truncating, we can keep the first block and set nextWrite to 0 to reduce GC
332-
bytes.truncate(0);
333330

334331
assert node != -2;
335332

@@ -358,7 +355,8 @@ long addNode(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException {
358355
return NON_FINAL_END_NODE;
359356
}
360357
}
361-
// System.out.println(" startAddr=" + startAddress);
358+
// reset the scratch writer to prepare for new write
359+
bytes.reset();
362360

363361
// the scratch writer must be cleaned at this point
364362
assert bytes.getPosition() == 0;

0 commit comments

Comments
 (0)