|
| 1 | +package json.ext; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.ByteBuffer; |
| 5 | + |
| 6 | +import org.jruby.util.ByteList; |
| 7 | + |
| 8 | +import jdk.incubator.vector.ByteVector; |
| 9 | +import jdk.incubator.vector.VectorMask; |
| 10 | +import jdk.incubator.vector.VectorOperators; |
| 11 | +import jdk.incubator.vector.VectorSpecies; |
| 12 | + |
| 13 | +class VectorizedStringEncoder extends SWARBasicStringEncoder { |
| 14 | + private static final VectorSpecies<Byte> SP = ByteVector.SPECIES_PREFERRED; |
| 15 | + private static final ByteVector ZERO = ByteVector.zero(SP); |
| 16 | + private static final ByteVector TWO = ByteVector.broadcast(SP, 2); |
| 17 | + private static final ByteVector THIRTY_THREE = ByteVector.broadcast(SP, 33); |
| 18 | + private static final ByteVector BACKSLASH = ByteVector.broadcast(SP, '\\'); |
| 19 | + |
| 20 | + @Override |
| 21 | + public StringEncoder clone() { |
| 22 | + return new VectorizedStringEncoder(); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + void encode(ByteList src) throws IOException { |
| 27 | + byte[] ptrBytes = src.unsafeBytes(); |
| 28 | + int ptr = src.begin(); |
| 29 | + int len = src.realSize(); |
| 30 | + int beg = 0; |
| 31 | + int pos = ptr; |
| 32 | + |
| 33 | + while ((pos + SP.length() <= len)) { |
| 34 | + ByteVector chunk = ByteVector.fromArray(SP, ptrBytes, ptr + pos); |
| 35 | + // bytes are signed in java, so we need to remove negative values |
| 36 | + VectorMask<Byte> negative = chunk.lt(ZERO); |
| 37 | + VectorMask<Byte> tooLowOrDblQuote = chunk.lanewise(VectorOperators.XOR, TWO).lt(THIRTY_THREE).andNot(negative); |
| 38 | + VectorMask<Byte> needsEscape = chunk.eq(BACKSLASH).or(tooLowOrDblQuote); |
| 39 | + if (needsEscape.anyTrue()) { |
| 40 | + int chunkStart = pos; |
| 41 | + long mask = needsEscape.toLong(); |
| 42 | + |
| 43 | + while(mask > 0) { |
| 44 | + // nextMatch inlined |
| 45 | + int index = Long.numberOfTrailingZeros(mask); |
| 46 | + mask &= (mask - 1); |
| 47 | + pos = chunkStart + index; |
| 48 | + int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]); |
| 49 | + |
| 50 | + beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); |
| 51 | + escapeAscii(ch, aux, HEX); |
| 52 | + } |
| 53 | + |
| 54 | + // Skip over any remaining characters in the current chunk |
| 55 | + pos = chunkStart + SP.length(); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + pos += SP.length(); |
| 60 | + } |
| 61 | + |
| 62 | + ByteBuffer bb = ByteBuffer.wrap(ptrBytes, ptr, len); |
| 63 | + if (pos + 8 <= len) { |
| 64 | + long x = bb.getLong(ptr + pos); |
| 65 | + if (skipChunk(x)) { |
| 66 | + pos += 8; |
| 67 | + } else { |
| 68 | + int chunkEnd = ptr + pos + 8; |
| 69 | + while (pos < chunkEnd) { |
| 70 | + int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]); |
| 71 | + int ch_len = ESCAPE_TABLE[ch]; |
| 72 | + if (ch_len > 0) { |
| 73 | + beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); |
| 74 | + escapeAscii(ch, aux, HEX); |
| 75 | + } else { |
| 76 | + pos++; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (pos + 4 <= len) { |
| 83 | + int x = bb.getInt(ptr + pos); |
| 84 | + if (skipChunk(x)) { |
| 85 | + pos += 4; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + while (pos < len) { |
| 90 | + int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]); |
| 91 | + int ch_len = ESCAPE_TABLE[ch]; |
| 92 | + if (ch_len > 0) { |
| 93 | + beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); |
| 94 | + escapeAscii(ch, aux, HEX); |
| 95 | + } else { |
| 96 | + pos++; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (beg < len) { |
| 101 | + append(ptrBytes, ptr + beg, len - beg); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments