Skip to content

Commit b18742e

Browse files
committed
heuristic to skip chunks of leading whitespace when parsing
1 parent 8f5c7f9 commit b18742e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ext/json/ext/parser/parser.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,22 @@ json_eat_comments(JSON_ParserState *state)
591591
static inline void
592592
json_eat_whitespace(JSON_ParserState *state)
593593
{
594+
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
595+
// Heuristic: if we see a newline, there may be consecutive of spaces after it.
596+
if (RB_UNLIKELY(state->cursor < state->end && *state->cursor == '\n')) {
597+
state->cursor++;
598+
599+
while (state->cursor+sizeof(uint64_t) <= state->end) {
600+
uint64_t chunk;
601+
memcpy(&chunk, state->cursor, sizeof(uint64_t));
602+
if (chunk != 0x2020202020202020) {
603+
break;
604+
}
605+
state->cursor += sizeof(uint64_t);
606+
}
607+
}
608+
#endif
609+
594610
while (state->cursor < state->end && RB_UNLIKELY(whitespace[(unsigned char)*state->cursor])) {
595611
if (RB_LIKELY(*state->cursor != '/')) {
596612
state->cursor++;

0 commit comments

Comments
 (0)