-
Notifications
You must be signed in to change notification settings - Fork 366
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
Merged
byroot
merged 8 commits into
ruby:master
from
samyron:sm/use-segmented-outputstream-and-swar
Aug 28, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5274d5d
Allow for segmented output streams and a SWAR-based basic StringEncod…
samyron b2644ff
Handle the case were the capacity overflows Integer.MAX_VALUE.
samyron 32f3287
Remove the LinkedSegmentedByteListDirectOutputStream in favor of the …
samyron 44b1d87
Use a ternary to determine the capacity of the next segment when grow…
samyron a458201
Use SWAR if there is still at least 4 bytes remaining.
samyron 9ebe105
Ensure the SWAR encoder in the java extension checks every byte.
samyron 052198a
Refactor the SWAR logic into a separate subclass of StringEncoder.
samyron 43a8a83
Refactor the logic to evaluate every byte in the chunk if there is a …
samyron 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
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package json.ext; | ||
|
|
||
| import java.io.OutputStream; | ||
|
|
||
| import org.jcodings.Encoding; | ||
| import org.jruby.util.ByteList; | ||
|
|
||
| abstract class AbstractByteListDirectOutputStream extends OutputStream { | ||
|
|
||
| private static final String PROP_SEGMENTED_BUFFER = "jruby.json.useSegmentedOutputStream"; | ||
| private static final String PROP_SEGMENTED_BUFFER_DEFAULT = "true"; | ||
|
|
||
| private static final boolean USE_SEGMENTED_BUFFER; | ||
|
|
||
| static { | ||
| String useSegmentedOutputStream = System.getProperty(PROP_SEGMENTED_BUFFER, PROP_SEGMENTED_BUFFER_DEFAULT); | ||
| USE_SEGMENTED_BUFFER = Boolean.parseBoolean(useSegmentedOutputStream); | ||
| // XXX Is there a logger we can use here? | ||
| // System.out.println("Using segmented output stream: " + USE_SEGMENTED_BUFFER); | ||
| } | ||
|
|
||
| public static AbstractByteListDirectOutputStream create(int estimatedSize) { | ||
| if (USE_SEGMENTED_BUFFER) { | ||
| return new SegmentedByteListDirectOutputStream(estimatedSize); | ||
| } else { | ||
| return new ByteListDirectOutputStream(estimatedSize); | ||
| } | ||
| } | ||
|
|
||
| public abstract ByteList toByteListDirect(Encoding encoding); | ||
| } |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package json.ext; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| import org.jruby.util.ByteList; | ||
|
|
||
| public class SWARBasicStringEncoder extends StringEncoder { | ||
|
|
||
| public SWARBasicStringEncoder() { | ||
| super(ESCAPE_TABLE); | ||
| } | ||
|
|
||
| @Override | ||
| void encode(ByteList src) throws IOException { | ||
| byte[] hexdig = HEX; | ||
| byte[] scratch = aux; | ||
|
|
||
| byte[] ptrBytes = src.unsafeBytes(); | ||
| int ptr = src.begin(); | ||
| int len = src.realSize(); | ||
|
|
||
| int beg = 0; | ||
| int pos = 0; | ||
|
|
||
| ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, len); | ||
| while (pos + 8 <= len) { | ||
| long x = bb.getLong(ptr + pos); | ||
| if (skipChunk(x)) { | ||
| pos += 8; | ||
| continue; | ||
| } | ||
| int chunkEnd = pos + 8; | ||
| while (pos < chunkEnd) { | ||
| int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]); | ||
| int ch_len = ESCAPE_TABLE[ch]; | ||
| if (ch_len > 0) { | ||
| beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); | ||
| escapeAscii(ch, scratch, hexdig); | ||
| } else { | ||
| pos++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (pos + 4 <= len) { | ||
| int x = bb.getInt(ptr + pos); | ||
| if (skipChunk(x)) { | ||
| pos += 4; | ||
| } | ||
| } | ||
|
|
||
| while (pos < len) { | ||
| int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]); | ||
| int ch_len = ESCAPE_TABLE[ch]; | ||
| if (ch_len > 0) { | ||
| beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); | ||
| escapeAscii(ch, scratch, hexdig); | ||
| } else { | ||
| pos++; | ||
| } | ||
| } | ||
|
|
||
| if (beg < len) { | ||
| append(ptrBytes, ptr + beg, len - beg); | ||
| } | ||
| } | ||
|
|
||
| private boolean skipChunk(long x) { | ||
| long is_ascii = 0x8080808080808080L & ~x; | ||
| long xor2 = x ^ 0x0202020202020202L; | ||
| long lt32_or_eq34 = xor2 - 0x2121212121212121L; | ||
| long sub92 = x ^ 0x5C5C5C5C5C5C5C5CL; | ||
| long eq92 = (sub92 - 0x0101010101010101L); | ||
| return ((lt32_or_eq34 | eq92) & is_ascii) == 0; | ||
| } | ||
|
|
||
| private boolean skipChunk(int x) { | ||
| int is_ascii = 0x80808080 & ~x; | ||
| int xor2 = x ^ 0x02020202; | ||
| int lt32_or_eq34 = xor2 - 0x21212121; | ||
| int sub92 = x ^ 0x5C5C5C5C; | ||
| int eq92 = (sub92 - 0x01010101); | ||
| return ((lt32_or_eq34 | eq92) & is_ascii) == 0; | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
java/src/json/ext/SegmentedByteListDirectOutputStream.java
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package json.ext; | ||
|
|
||
| import org.jcodings.Encoding; | ||
| import org.jruby.util.ByteList; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class SegmentedByteListDirectOutputStream extends AbstractByteListDirectOutputStream { | ||
| private static final int DEFAULT_CAPACITY = 1024; | ||
|
|
||
| private int totalLength; | ||
| // Why 21? The minimum segment size is 1024 bytes. If we double the segment size each time | ||
| // we need a new segment, we only need 21 segments to reach the maximum array size in Java. | ||
| private byte[][] segments = new byte[21][]; | ||
| private int currentSegmentIndex; | ||
| private int currentSegmentLength; | ||
| private byte[] currentSegment; | ||
|
|
||
| SegmentedByteListDirectOutputStream(int size) { | ||
| currentSegment = new byte[Math.max(size, DEFAULT_CAPACITY)]; | ||
| segments[0] = currentSegment; | ||
| } | ||
|
|
||
| public ByteList toByteListDirect(Encoding encoding) { | ||
| byte[] buffer = new byte[totalLength]; | ||
| int pos = 0; | ||
| // We handle the current segment separately. | ||
| for (int i = 0; i < currentSegmentIndex; i++) { | ||
| byte[] segment = segments[i]; | ||
| System.arraycopy(segment, 0, buffer, pos, segment.length); | ||
| pos += segment.length; | ||
| } | ||
| System.arraycopy(currentSegment, 0, buffer, pos, currentSegmentLength); | ||
| return new ByteList(buffer, 0, totalLength, encoding, false); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(int b) throws IOException { | ||
| if (currentSegmentLength == currentSegment.length) { | ||
| if (totalLength + 1 < 0) { | ||
| throw new IOException("Total length exceeds maximum length of an array."); | ||
| } | ||
| currentSegmentIndex++; | ||
| int capacity = currentSegment.length * 2; | ||
| capacity = (capacity < 0) ? DEFAULT_CAPACITY : capacity; | ||
| currentSegment = new byte[capacity]; | ||
| currentSegmentLength = 0; | ||
| segments[currentSegmentIndex] = currentSegment; | ||
| } | ||
| currentSegment[currentSegmentLength++] = (byte) b; | ||
| totalLength++; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] bytes, int start, int length) throws IOException { | ||
| int remaining = length; | ||
|
|
||
| while (remaining > 0) { | ||
| if (currentSegmentLength == currentSegment.length) { | ||
| if (totalLength + remaining < 0) { | ||
| throw new IOException("Total length exceeds maximum length of an array."); | ||
| } | ||
| currentSegmentIndex++; | ||
| int capacity = currentSegment.length << 1; | ||
| capacity = (capacity < 0) ? DEFAULT_CAPACITY : capacity; | ||
| capacity = (capacity < remaining) ? remaining : capacity; | ||
| currentSegment = new byte[capacity]; | ||
| currentSegmentLength = 0; | ||
| segments[currentSegmentIndex] = currentSegment; | ||
| } | ||
| int toWrite = Math.min(remaining, currentSegment.length - currentSegmentLength); | ||
| System.arraycopy(bytes, start, currentSegment, currentSegmentLength, toWrite); | ||
| currentSegmentLength += toWrite; | ||
| start += toWrite; | ||
| remaining -= toWrite; | ||
| } | ||
| totalLength += length; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] bytes) throws IOException { | ||
| write(bytes, 0, bytes.length); | ||
| } | ||
| } | ||
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
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
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.
Why
21? The minimum segment size is1024for the first segment. The code doubles the segment size for each additional segment. Based on this doubling, we only need 21 segments before we hitInteger.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?