-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
ATN serialization improvements (Java only for demo) #3505
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48c67ed
Allow ATN serialization of values more than 65535 (writeCompactUInt32)
KvanTTT 373fb0f
Full support of positive int range in serializer and deserializer (up…
KvanTTT 52936cc
Get rid of excess ATN serialization, remove excess allocations
KvanTTT 815fd26
Move decode method from ATNSerializer to ATNDeserializerHelper (to te…
KvanTTT 59a8ca3
Implement base64 encoding instead of plain chars (up to 2x compact ge…
KvanTTT ec78ce0
Zero-allocation deserialization for base64 string
KvanTTT 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 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
50 changes: 0 additions & 50 deletions
50
runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest2.interp
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
42 changes: 42 additions & 0 deletions
42
runtime/Java/src/org/antlr/v4/runtime/atn/ATNDataReader.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.antlr.v4.runtime.atn; | ||
|
||
import java.util.UUID; | ||
|
||
public abstract class ATNDataReader { | ||
protected abstract byte readByte(); | ||
|
||
public int read() { | ||
int value = readByte(); | ||
if ((value & 0b1000_0000) == 0) { | ||
return value; | ||
} | ||
|
||
int mask = value & 0b111_00000; | ||
if (mask == ATNDataWriter.MinusOneMask) { | ||
return -1; | ||
} | ||
else { | ||
return mask == ATNDataWriter.OneByteMask | ||
? value | ||
: mask == ATNDataWriter.TwoByteMask | ||
? (value & ATNDataWriter.ValueMask) << 8 | readByte() & 0xFF | ||
: mask == ATNDataWriter.ThreeByteMask | ||
? (value & ATNDataWriter.ValueMask) << 16 | readByte() << 8 & 0xFF00 | readByte() & 0xFF | ||
: (readByte() << 24 & 0xFF000000) | (readByte() << 16 & 0xFF0000) | (readByte() << 8 & 0xFF00) | readByte() & 0xFF; | ||
} | ||
} | ||
|
||
public UUID readUUID() { | ||
long leastSigBits = ((long) readInt32() & 0x00000000FFFFFFFFL) | ((long) readInt32() << 32); | ||
long mostSigBits = ((long) readInt32() & 0x00000000FFFFFFFFL) | ((long) readInt32() << 32); | ||
return new UUID(mostSigBits, leastSigBits); | ||
} | ||
|
||
public int readInt32() { | ||
return readUInt16() | (readUInt16() << 16); | ||
} | ||
|
||
public int readUInt16() { | ||
return readByte() & 0xFF | readByte() << 8 & 0xFF00; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
runtime/Java/src/org/antlr/v4/runtime/atn/ATNDataReaderBase64.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.antlr.v4.runtime.atn; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ATNDataReaderBase64 extends ATNDataReader { | ||
private static final char[] toBase64 = { | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' | ||
}; | ||
|
||
private static final int[] fromBase64 = new int[256]; | ||
static { | ||
Arrays.fill(fromBase64, -1); | ||
for (int i = 0; i < toBase64.length; i++) | ||
fromBase64[toBase64[i]] = i; | ||
fromBase64['='] = -2; | ||
} | ||
|
||
private final String string; | ||
private int charPos; | ||
private int rest; | ||
private int restBitsCount; | ||
|
||
public ATNDataReaderBase64(String string) { | ||
this.string = string; | ||
} | ||
|
||
@Override | ||
public byte readByte() { | ||
int result = 0; | ||
if (restBitsCount == 0) { | ||
int b1 = fromBase64[string.charAt(charPos++)]; | ||
int b2 = fromBase64[string.charAt(charPos++)]; | ||
result = b1 << 2 & 0b1111_1100 | b2 >> 4 & 0b0000_0011; | ||
rest = b2 & 0b0000_1111; | ||
restBitsCount = 4; | ||
} | ||
else if (restBitsCount == 4) { | ||
int b = fromBase64[string.charAt(charPos++)]; | ||
result = rest << 4 & 0b1111_0000 | b >> 2 & 0b0000_1111; | ||
rest = b & 0b0000_0011; | ||
restBitsCount = 2; | ||
} | ||
else if (restBitsCount == 2) { | ||
int b = fromBase64[string.charAt(charPos++)]; | ||
result = rest << 6 & 0b1100_0000 | b & 0b0011_1111; | ||
restBitsCount = 0; | ||
} | ||
return (byte)result; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
runtime/Java/src/org/antlr/v4/runtime/atn/ATNDataReaderByteBuffer.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.antlr.v4.runtime.atn; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class ATNDataReaderByteBuffer extends ATNDataReader { | ||
private final ByteBuffer byteBuffer; | ||
|
||
public ATNDataReaderByteBuffer(ByteBuffer byteBuffer) { | ||
this.byteBuffer = byteBuffer; | ||
if (byteBuffer.position() != 0) { | ||
byteBuffer.flip(); | ||
} | ||
} | ||
|
||
@Override | ||
public byte readByte() { | ||
return byteBuffer.get(); | ||
} | ||
} |
Oops, something went wrong.
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.
Is there a standard base64 decoder somewhere?
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.
Oh i see base64 below now. Hmm...adding general comment outside.
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.
It's the binary reader that uses abstract
readByte
calls. It reads mostly integers from compact format (described below).readByte
is implemented inATNDataReaderBase64
(that reads from base64 string) and inATNDataReaderByteBuffer
.