forked from simdjson/simdjson-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuralIndexer.java
More file actions
320 lines (269 loc) · 14 KB
/
StructuralIndexer.java
File metadata and controls
320 lines (269 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package org.simdjson;
import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.VectorShuffle;
import java.util.Arrays;
import static jdk.incubator.vector.ByteVector.SPECIES_256;
import static jdk.incubator.vector.ByteVector.SPECIES_512;
import static jdk.incubator.vector.VectorOperators.ULE;
class StructuralIndexer {
private static final int VECTOR_BIT_SIZE = VectorUtils.BYTE_SPECIES.vectorBitSize();
private static final int STEP_SIZE = 64;
private static final byte BACKSLASH = (byte) '\\';
private static final byte QUOTE = (byte) '"';
private static final byte SPACE = 0x20;
private static final byte LAST_CONTROL_CHARACTER = (byte) 0x1F;
private static final long EVEN_BITS_MASK = 0x5555555555555555L;
private static final long ODD_BITS_MASK = ~EVEN_BITS_MASK;
private static final byte LOW_NIBBLE_MASK = 0x0f;
private static final ByteVector WHITESPACE_TABLE = VectorUtils.repeat(
new byte[]{' ', 100, 100, 100, 17, 100, 113, 2, 100, '\t', '\n', 112, 100, '\r', 100, 100}
);
private static final ByteVector OP_TABLE = VectorUtils.repeat(
new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ':', '{', ',', '}', 0, 0}
);
private static final byte[] LAST_BLOCK_SPACES = new byte[STEP_SIZE];
static {
Arrays.fill(LAST_BLOCK_SPACES, SPACE);
}
private final BitIndexes bitIndexes;
private final byte[] lastBlock = new byte[STEP_SIZE];
StructuralIndexer(BitIndexes bitIndexes) {
this.bitIndexes = bitIndexes;
}
void index(byte[] buffer, int length) {
bitIndexes.reset();
switch (VECTOR_BIT_SIZE) {
case 256 -> index256(buffer, length);
case 512 -> index512(buffer, length);
default -> throw new UnsupportedOperationException("Unsupported vector width: " + VECTOR_BIT_SIZE * 64);
}
}
private void index256(byte[] buffer, int length) {
long prevInString = 0;
long prevEscaped = 0;
long prevStructurals = 0;
long unescapedCharsError = 0;
long prevScalar = 0;
// Using SPECIES_512 here is not a mistake. Each iteration of the below loop processes two 256-bit chunks,
// so effectively it processes 512 bits at once.
int loopBound = SPECIES_512.loopBound(length);
int offset = 0;
int blockIndex = 0;
for (; offset < loopBound; offset += STEP_SIZE) {
ByteVector chunk0 = ByteVector.fromArray(SPECIES_256, buffer, offset);
ByteVector chunk1 = ByteVector.fromArray(SPECIES_256, buffer, offset + 32);
// string scanning
long backslash0 = chunk0.eq(BACKSLASH).toLong();
long backslash1 = chunk1.eq(BACKSLASH).toLong();
long backslash = backslash0 | (backslash1 << 32);
long escaped;
if (backslash == 0) {
escaped = prevEscaped;
prevEscaped = 0;
} else {
backslash &= ~prevEscaped;
long followsEscape = backslash << 1 | prevEscaped;
long oddSequenceStarts = backslash & ODD_BITS_MASK & ~followsEscape;
long sequencesStartingOnEvenBits = oddSequenceStarts + backslash;
// Here, we check if the unsigned addition above caused an overflow. If that's the case, we store 1 in prevEscaped.
// The formula used to detect overflow was taken from 'Hacker's Delight, Second Edition' by Henry S. Warren, Jr.,
// Chapter 2-13.
prevEscaped = ((oddSequenceStarts >>> 1) + (backslash >>> 1) + ((oddSequenceStarts & backslash) & 1)) >>> 63;
long invertMask = sequencesStartingOnEvenBits << 1;
escaped = (EVEN_BITS_MASK ^ invertMask) & followsEscape;
}
long unescaped0 = chunk0.compare(ULE, LAST_CONTROL_CHARACTER).toLong();
long unescaped1 = chunk1.compare(ULE, LAST_CONTROL_CHARACTER).toLong();
long unescaped = unescaped0 | (unescaped1 << 32);
long quote0 = chunk0.eq(QUOTE).toLong();
long quote1 = chunk1.eq(QUOTE).toLong();
long quote = (quote0 | (quote1 << 32)) & ~escaped;
long inString = prefixXor(quote) ^ prevInString;
prevInString = inString >> 63;
// characters classification
VectorShuffle<Byte> chunk0Low = chunk0.and(LOW_NIBBLE_MASK).toShuffle();
VectorShuffle<Byte> chunk1Low = chunk1.and(LOW_NIBBLE_MASK).toShuffle();
long whitespace0 = chunk0.eq(WHITESPACE_TABLE.rearrange(chunk0Low)).toLong();
long whitespace1 = chunk1.eq(WHITESPACE_TABLE.rearrange(chunk1Low)).toLong();
long whitespace = whitespace0 | (whitespace1 << 32);
ByteVector curlified0 = chunk0.or((byte) 0x20);
ByteVector curlified1 = chunk1.or((byte) 0x20);
long op0 = curlified0.eq(OP_TABLE.rearrange(chunk0Low)).toLong();
long op1 = curlified1.eq(OP_TABLE.rearrange(chunk1Low)).toLong();
long op = op0 | (op1 << 32);
// finish
long scalar = ~(op | whitespace);
long nonQuoteScalar = scalar & ~quote;
long followsNonQuoteScalar = nonQuoteScalar << 1 | prevScalar;
prevScalar = nonQuoteScalar >>> 63;
long potentialScalarStart = scalar & ~followsNonQuoteScalar;
long potentialStructuralStart = op | potentialScalarStart;
bitIndexes.write(blockIndex, prevStructurals);
blockIndex += STEP_SIZE;
prevStructurals = potentialStructuralStart & ~(inString ^ quote);
unescapedCharsError |= unescaped & inString;
}
byte[] remainder = remainder(buffer, length, blockIndex);
ByteVector chunk0 = ByteVector.fromArray(SPECIES_256, remainder, 0);
ByteVector chunk1 = ByteVector.fromArray(SPECIES_256, remainder, 32);
// string scanning
long backslash0 = chunk0.eq(BACKSLASH).toLong();
long backslash1 = chunk1.eq(BACKSLASH).toLong();
long backslash = backslash0 | (backslash1 << 32);
long escaped;
if (backslash == 0) {
escaped = prevEscaped;
} else {
backslash &= ~prevEscaped;
long followsEscape = backslash << 1 | prevEscaped;
long oddSequenceStarts = backslash & ODD_BITS_MASK & ~followsEscape;
long sequencesStartingOnEvenBits = oddSequenceStarts + backslash;
long invertMask = sequencesStartingOnEvenBits << 1;
escaped = (EVEN_BITS_MASK ^ invertMask) & followsEscape;
}
long unescaped0 = chunk0.compare(ULE, LAST_CONTROL_CHARACTER).toLong();
long unescaped1 = chunk1.compare(ULE, LAST_CONTROL_CHARACTER).toLong();
long unescaped = unescaped0 | (unescaped1 << 32);
long quote0 = chunk0.eq(QUOTE).toLong();
long quote1 = chunk1.eq(QUOTE).toLong();
long quote = (quote0 | (quote1 << 32)) & ~escaped;
long inString = prefixXor(quote) ^ prevInString;
prevInString = inString >> 63;
// characters classification
VectorShuffle<Byte> chunk0Low = chunk0.and(LOW_NIBBLE_MASK).toShuffle();
VectorShuffle<Byte> chunk1Low = chunk1.and(LOW_NIBBLE_MASK).toShuffle();
long whitespace0 = chunk0.eq(WHITESPACE_TABLE.rearrange(chunk0Low)).toLong();
long whitespace1 = chunk1.eq(WHITESPACE_TABLE.rearrange(chunk1Low)).toLong();
long whitespace = whitespace0 | (whitespace1 << 32);
ByteVector curlified0 = chunk0.or((byte) 0x20);
ByteVector curlified1 = chunk1.or((byte) 0x20);
long op0 = curlified0.eq(OP_TABLE.rearrange(chunk0Low)).toLong();
long op1 = curlified1.eq(OP_TABLE.rearrange(chunk1Low)).toLong();
long op = op0 | (op1 << 32);
// finish
long scalar = ~(op | whitespace);
long nonQuoteScalar = scalar & ~quote;
long followsNonQuoteScalar = nonQuoteScalar << 1 | prevScalar;
long potentialScalarStart = scalar & ~followsNonQuoteScalar;
long potentialStructuralStart = op | potentialScalarStart;
bitIndexes.write(blockIndex, prevStructurals);
blockIndex += STEP_SIZE;
prevStructurals = potentialStructuralStart & ~(inString ^ quote);
unescapedCharsError |= unescaped & inString;
bitIndexes.write(blockIndex, prevStructurals);
bitIndexes.finish();
if (prevInString != 0) {
throw new JsonParsingException("Unclosed string. A string is opened, but never closed.");
}
if (unescapedCharsError != 0) {
throw new JsonParsingException("Unescaped characters. Within strings, there are characters that should be escaped.");
}
}
private void index512(byte[] buffer, int length) {
long prevInString = 0;
long prevEscaped = 0;
long prevStructurals = 0;
long unescapedCharsError = 0;
long prevScalar = 0;
int loopBound = SPECIES_512.loopBound(length);
int offset = 0;
int blockIndex = 0;
for (; offset < loopBound; offset += STEP_SIZE) {
ByteVector chunk = ByteVector.fromArray(SPECIES_512, buffer, offset);
// string scanning
long backslash = chunk.eq(BACKSLASH).toLong();
long escaped;
if (backslash == 0) {
escaped = prevEscaped;
prevEscaped = 0;
} else {
backslash &= ~prevEscaped;
long followsEscape = backslash << 1 | prevEscaped;
long oddSequenceStarts = backslash & ODD_BITS_MASK & ~followsEscape;
long sequencesStartingOnEvenBits = oddSequenceStarts + backslash;
// Here, we check if the unsigned addition above caused an overflow. If that's the case, we store 1 in prevEscaped.
// The formula used to detect overflow was taken from 'Hacker's Delight, Second Edition' by Henry S. Warren, Jr.,
// Chapter 2-13.
prevEscaped = ((oddSequenceStarts >>> 1) + (backslash >>> 1) + ((oddSequenceStarts & backslash) & 1)) >>> 63;
long invertMask = sequencesStartingOnEvenBits << 1;
escaped = (EVEN_BITS_MASK ^ invertMask) & followsEscape;
}
long unescaped = chunk.compare(ULE, LAST_CONTROL_CHARACTER).toLong();
long quote = chunk.eq(QUOTE).toLong() & ~escaped;
long inString = prefixXor(quote) ^ prevInString;
prevInString = inString >> 63;
// characters classification
VectorShuffle<Byte> chunkLow = chunk.and(LOW_NIBBLE_MASK).toShuffle();
long whitespace = chunk.eq(WHITESPACE_TABLE.rearrange(chunkLow)).toLong();
ByteVector curlified = chunk.or((byte) 0x20);
long op = curlified.eq(OP_TABLE.rearrange(chunkLow)).toLong();
// finish
long scalar = ~(op | whitespace);
long nonQuoteScalar = scalar & ~quote;
long followsNonQuoteScalar = nonQuoteScalar << 1 | prevScalar;
prevScalar = nonQuoteScalar >>> 63;
long potentialScalarStart = scalar & ~followsNonQuoteScalar;
long potentialStructuralStart = op | potentialScalarStart;
bitIndexes.write(blockIndex, prevStructurals);
blockIndex += STEP_SIZE;
prevStructurals = potentialStructuralStart & ~(inString ^ quote);
unescapedCharsError |= unescaped & inString;
}
byte[] remainder = remainder(buffer, length, blockIndex);
ByteVector chunk = ByteVector.fromArray(SPECIES_512, remainder, 0);
// string scanning
long backslash = chunk.eq(BACKSLASH).toLong();
long escaped;
if (backslash == 0) {
escaped = prevEscaped;
} else {
backslash &= ~prevEscaped;
long followsEscape = backslash << 1 | prevEscaped;
long oddSequenceStarts = backslash & ODD_BITS_MASK & ~followsEscape;
long sequencesStartingOnEvenBits = oddSequenceStarts + backslash;
long invertMask = sequencesStartingOnEvenBits << 1;
escaped = (EVEN_BITS_MASK ^ invertMask) & followsEscape;
}
long unescaped = chunk.compare(ULE, LAST_CONTROL_CHARACTER).toLong();
long quote = chunk.eq(QUOTE).toLong() & ~escaped;
long inString = prefixXor(quote) ^ prevInString;
prevInString = inString >> 63;
// characters classification
VectorShuffle<Byte> chunkLow = chunk.and(LOW_NIBBLE_MASK).toShuffle();
long whitespace = chunk.eq(WHITESPACE_TABLE.rearrange(chunkLow)).toLong();
ByteVector curlified = chunk.or((byte) 0x20);
long op = curlified.eq(OP_TABLE.rearrange(chunkLow)).toLong();
// finish
long scalar = ~(op | whitespace);
long nonQuoteScalar = scalar & ~quote;
long followsNonQuoteScalar = nonQuoteScalar << 1 | prevScalar;
long potentialScalarStart = scalar & ~followsNonQuoteScalar;
long potentialStructuralStart = op | potentialScalarStart;
bitIndexes.write(blockIndex, prevStructurals);
blockIndex += STEP_SIZE;
prevStructurals = potentialStructuralStart & ~(inString ^ quote);
unescapedCharsError |= unescaped & inString;
bitIndexes.write(blockIndex, prevStructurals);
bitIndexes.finish();
if (prevInString != 0) {
throw new JsonParsingException("Unclosed string. A string is opened, but never closed.");
}
if (unescapedCharsError != 0) {
throw new JsonParsingException("Unescaped characters. Within strings, there are characters that should be escaped.");
}
}
private byte[] remainder(byte[] buffer, int length, int idx) {
System.arraycopy(LAST_BLOCK_SPACES, 0, lastBlock, 0, lastBlock.length);
System.arraycopy(buffer, idx, lastBlock, 0, length - idx);
return lastBlock;
}
private static long prefixXor(long bitmask) {
bitmask ^= bitmask << 1;
bitmask ^= bitmask << 2;
bitmask ^= bitmask << 4;
bitmask ^= bitmask << 8;
bitmask ^= bitmask << 16;
bitmask ^= bitmask << 32;
return bitmask;
}
}