-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Lazily write the FST padding byte #12981
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,9 @@ public class FSTCompiler<T> { | |
|
||
private final IntsRefBuilder lastInput = new IntsRefBuilder(); | ||
|
||
// indicates whether we are not yet to write the padding byte | ||
private boolean paddingBytePending; | ||
|
||
// NOTE: cutting this over to ArrayList instead loses ~6% | ||
// in build performance on 9.8M Wikipedia terms; so we | ||
// left this as an array: | ||
|
@@ -164,15 +167,14 @@ private FSTCompiler( | |
boolean allowFixedLengthArcs, | ||
DataOutput dataOutput, | ||
float directAddressingMaxOversizingFactor, | ||
int version) | ||
throws IOException { | ||
int version) { | ||
this.allowFixedLengthArcs = allowFixedLengthArcs; | ||
this.directAddressingMaxOversizingFactor = directAddressingMaxOversizingFactor; | ||
this.version = version; | ||
// pad: ensure no node gets address 0 which is reserved to mean | ||
// the stop state w/ no arcs | ||
dataOutput.writeByte((byte) 0); | ||
// the stop state w/ no arcs. the actual byte will be written lazily | ||
numBytesWritten++; | ||
paddingBytePending = true; | ||
this.dataOutput = dataOutput; | ||
fst = | ||
new FST<>( | ||
|
@@ -344,7 +346,7 @@ public Builder<T> setVersion(int version) { | |
} | ||
|
||
/** Creates a new {@link FSTCompiler}. */ | ||
public FSTCompiler<T> build() throws IOException { | ||
public FSTCompiler<T> build() { | ||
// create a default DataOutput if not specified | ||
if (dataOutput == null) { | ||
dataOutput = getOnHeapReaderWriter(15); | ||
|
@@ -552,13 +554,27 @@ long addNode(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException { | |
} | ||
|
||
reverseScratchBytes(); | ||
// write the padding byte if needed | ||
if (paddingBytePending) { | ||
writePaddingByte(); | ||
} | ||
scratchBytes.writeTo(dataOutput); | ||
numBytesWritten += scratchBytes.getPosition(); | ||
|
||
nodeCount++; | ||
return numBytesWritten - 1; | ||
} | ||
|
||
/** | ||
* Write the padding byte, ensure no node gets address 0 which is reserved to mean the stop state | ||
* w/ no arcs | ||
*/ | ||
private void writePaddingByte() throws IOException { | ||
assert paddingBytePending; | ||
dataOutput.writeByte((byte) 0); | ||
paddingBytePending = false; | ||
} | ||
|
||
private void writeLabel(DataOutput out, int v) throws IOException { | ||
assert v >= 0 : "v=" + v; | ||
if (fst.metadata.inputType == INPUT_TYPE.BYTE1) { | ||
|
@@ -967,7 +983,11 @@ public FST<T> compile() throws IOException { | |
freezeTail(0); | ||
if (root.numArcs == 0) { | ||
if (fst.metadata.emptyOutput == null) { | ||
// return null for completely empty FST which accepts nothing | ||
return null; | ||
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 add a comment that this means a completely empty FST? Accepts nothing? |
||
} else { | ||
// we haven't written the padding byte so far, but the FST is still valid | ||
writePaddingByte(); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 could we instead add a boolean
paddingBytePending
or so? Set it to true, here, then when the byte is lazily written, set it to false, write the byte, and incrementnumBytesWritten
at that point? Otherwise it's sort of weird to incrementnumBytesWritten
when we didn't actually write the byte yet?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.
numBytesWritten is used for determine the address of the to be written nodes, so if we don't increment it here, it would mess up the address.