Skip to content

Commit 43a8a83

Browse files
samyronbyroot
authored andcommitted
Refactor the logic to evaluate every byte in the chunk if there is a byte in that chunk that needs escaping.
1 parent 052198a commit 43a8a83

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

java/src/json/ext/AbstractByteListDirectOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
abstract class AbstractByteListDirectOutputStream extends OutputStream {
99

10-
private static final String PROP_SEGMENTED_BUFFER = "json.useSegmentedOutputStream";
10+
private static final String PROP_SEGMENTED_BUFFER = "jruby.json.useSegmentedOutputStream";
1111
private static final String PROP_SEGMENTED_BUFFER_DEFAULT = "true";
1212

1313
private static final boolean USE_SEGMENTED_BUFFER;

java/src/json/ext/SWARBasicStringEncoder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ void encode(ByteList src) throws IOException {
3030
pos += 8;
3131
continue;
3232
}
33-
for (int i = 0; i < 8; i++) {
34-
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos + i]);
33+
int chunkEnd = pos + 8;
34+
while (pos < chunkEnd) {
35+
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
3536
int ch_len = ESCAPE_TABLE[ch];
3637
if (ch_len > 0) {
37-
beg = pos = flushPos(pos + i, beg, ptrBytes, ptr, 1);
38+
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
3839
escapeAscii(ch, scratch, hexdig);
39-
break;
40+
} else {
41+
pos++;
4042
}
4143
}
4244
}

java/src/json/ext/SegmentedByteListDirectOutputStream.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class SegmentedByteListDirectOutputStream extends AbstractByteListDirectO
99
private static final int DEFAULT_CAPACITY = 1024;
1010

1111
private int totalLength;
12+
// Why 21? The minimum segment size is 1024 bytes. If we double the segment size each time
13+
// we need a new segment, we only need 21 segments to reach the maximum array size in Java.
1214
private byte[][] segments = new byte[21][];
1315
private int currentSegmentIndex;
1416
private int currentSegmentLength;

java/src/json/ext/StringEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class StringEncoder extends ByteListTranscoder {
114114

115115
protected final byte[] escapeTable;
116116

117-
private static final String USE_SWAR_BASIC_ENCODER_PROP = "json.useSWARBasicEncoder";
117+
private static final String USE_SWAR_BASIC_ENCODER_PROP = "jruby.json.useSWARBasicEncoder";
118118
private static final String USE_SWAR_BASIC_ENCODER_DEFAULT = "true";
119119
private static final boolean USE_BASIC_SWAR_ENCODER;
120120

0 commit comments

Comments
 (0)