Skip to content

Commit 2aee19c

Browse files
samyronbyroot
authored andcommitted
Various small optimizations and cleanups in the parser.
1 parent ac58045 commit 2aee19c

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

ext/json/ext/parser/parser.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,51 @@ static void rvalue_cache_insert_at(rvalue_cache *cache, int index, VALUE rstring
7878
cache->entries[index] = rstring;
7979
}
8080

81-
static inline int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
81+
static ALWAYS_INLINE() int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
8282
{
83+
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && defined(__has_builtin) && __has_builtin(__builtin_bswap64)
84+
const char *rptr;
85+
long rstring_length;
86+
87+
RSTRING_GETMEM(rstring, rptr, rstring_length);
88+
89+
if (length != rstring_length) {
90+
return (int)(length - rstring_length);
91+
}
92+
93+
long i = 0;
94+
95+
for (; i+8 <= length; i += 8) {
96+
uint64_t a, b;
97+
memcpy(&a, str + i, 8);
98+
memcpy(&b, rptr + i, 8);
99+
if (a != b) {
100+
a = __builtin_bswap64(a);
101+
b = __builtin_bswap64(b);
102+
return (a < b) ? -1 : 1;
103+
}
104+
}
105+
106+
for (; i < length; i++) {
107+
unsigned char ca = (unsigned char)str[i];
108+
unsigned char cb = (unsigned char)rptr[i];
109+
if (ca != cb) {
110+
return (ca < cb) ? -1 : 1;
111+
}
112+
}
113+
114+
return 0;
115+
#else
83116
long rstring_length = RSTRING_LEN(rstring);
84117
if (length == rstring_length) {
85118
return memcmp(str, RSTRING_PTR(rstring), length);
86119
} else {
87120
return (int)(length - rstring_length);
88121
}
122+
#endif
89123
}
90124

91-
static VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const long length)
125+
static ALWAYS_INLINE() VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const long length)
92126
{
93127
int low = 0;
94128
int high = cache->length - 1;
@@ -507,7 +541,7 @@ json_eat_comments(JSON_ParserState *state)
507541
}
508542
}
509543

510-
static inline void
544+
static ALWAYS_INLINE() void
511545
json_eat_whitespace(JSON_ParserState *state)
512546
{
513547
while (true) {

0 commit comments

Comments
 (0)