-
Notifications
You must be signed in to change notification settings - Fork 350
Java Extension Optimizations #835
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
base: master
Are you sure you want to change the base?
Java Extension Optimizations #835
Conversation
java/src/json/ext/LinkedSegmentedByteListDirectOutputStream.java
Outdated
Show resolved
Hide resolved
private static final int DEFAULT_CAPACITY = 1024; | ||
|
||
private int totalLength; | ||
private byte[][] segments = new byte[21][]; |
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.
Why 21
? The minimum segment size is 1024
for the first segment. The code doubles the segment size for each additional segment. Based on this doubling, we only need 21 segments before we hit Integer.MAX_VALUE
.
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.
Makes sense. 👏
Maybe a comment or well-named constant so nobody else asks that question in the future?
…SegmentedByteListDirectOutputStream.
…ing the output buffer.
Synthetic benchmarks of encoding an array of 128-byte ASCII strings.
SegmetedByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + Scalar (effectively the same code as master)
SegmentedByteListDirectOutputStream + Scalar
ByteListDirectOutputStream + SWAR
Master
|
java/src/json/ext/StringEncoder.java
Outdated
|
||
if (pos + 4 <= len) { | ||
int x = bb.getInt(ptr + pos); | ||
int is_ascii = 0x808080 & ~x; |
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.
This hex number only checks 3 bytes.
Maybe 0x808080
→ 0x80808080
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.
Great catch, thank you! Late night coding without my glasses...
Interestingly no spec failed. I'll try to address that.
As of commit Benchmarks as of this commitSWAR + SegmentedByteListDirectOutputStream
SWAR + ByteListDirectOutputStreamNote: This did seem like a particularly good run, at least for the
|
I'm happy to disable the |
Benchmarks from an Macbook Pro M4. I ran these a bunch of times and the results do vary a bit each run but I grabbed a random sampling. The big surprise is the Note, while I don't have the benchmarks here, if I do run the SegmentedByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + SWAR
SegmentedByteListDirectOutputStream + Scalar
ByteListDirectOuptutStream + Scalar
|
@samyron Great results! I think we could go ahead with this any time, pending my couple of minor review comments that should be addressed. The segmented stream is consistently faster than the old logic, and coupled with SWAR it can be much faster. I'd like to see this land so we can get back to playing with the vector API. |
@headius I'm happy to address the comments but I don't see any review comments on this PR... |
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.
Only minor changes needed
java/src/json/ext/StringEncoder.java
Outdated
@@ -114,6 +115,17 @@ class StringEncoder extends ByteListTranscoder { | |||
|
|||
protected final byte[] escapeTable; | |||
|
|||
private static final String USE_SWAR_BASIC_ENCODER_PROP = "json.useSWARBasicEncoder"; |
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.
Let's prefix this with jruby.
like other properties in JRuby and other libs.
private static final int DEFAULT_CAPACITY = 1024; | ||
|
||
private int totalLength; | ||
private byte[][] segments = new byte[21][]; |
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.
Makes sense. 👏
Maybe a comment or well-named constant so nobody else asks that question in the future?
@samyron D'oh, I had started a review but never submitted it. Just a couple of minor changes and we can merge. |
Ship it! |
Changelog 📓
OutputStream
to reduceSystem.arraycopy
's each time the output buffer is resized.StringEncoder#encode
to include a SWAR-based fast path for basic JSON encoding. The algorithm is from this post. It's the same as the vector-based algorithm in the C extension.These features can be toggled with the system properties
json.useSegmentedOutputStream
andjson.useSWARBasicEncoder
. Both default totrue
. I'm happy to remove these. They made testing and benchmarking much easier.Benchmarks
SegmentedByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + Scalar
SegmentedByteListDirectOutputStream + Scalar
master (as of commit 37e6890)