Skip to content

Commit ddad00b

Browse files
committed
Extract JSON_CPU_LITTLE_ENDIAN_64BITS definition
Only apply these definitions on 64 bits archs, as it's unclear if they have performance benefits or compatibility issues on 32bit archs.
1 parent 3bc1787 commit ddad00b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

ext/json/ext/json.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ruby.h"
55
#include "ruby/encoding.h"
6+
#include <stdint.h>
67

78
#if defined(RUBY_DEBUG) && RUBY_DEBUG
89
# define JSON_ASSERT RUBY_ASSERT
@@ -82,4 +83,10 @@ typedef unsigned char _Bool;
8283
#endif
8384
#endif
8485

86+
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && INTPTR_MAX == INT64_MAX
87+
#define JSON_CPU_LITTLE_ENDIAN_64BITS 1
88+
#else
89+
#define JSON_CPU_LITTLE_ENDIAN_64BITS 0
90+
#endif
91+
8592
#endif // _JSON_H_

ext/json/ext/parser/parser.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ typedef struct JSON_ParserStateStruct {
323323
int current_nesting;
324324
} JSON_ParserState;
325325

326-
static inline ssize_t rest(JSON_ParserState *state) {
326+
static inline size_t rest(JSON_ParserState *state) {
327327
return state->end - state->cursor;
328328
}
329329

@@ -525,7 +525,7 @@ json_eat_whitespace(JSON_ParserState *state)
525525
state->cursor++;
526526

527527
// Heuristic: if we see a newline, there is likely consecutive spaces after it.
528-
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
528+
#if JSON_CPU_LITTLE_ENDIAN_64BITS
529529
while (rest(state) > 8) {
530530
uint64_t chunk;
531531
memcpy(&chunk, state->cursor, sizeof(uint64_t));
@@ -966,7 +966,7 @@ static inline VALUE json_parse_string(JSON_ParserState *state, JSON_ParserConfig
966966
return Qfalse;
967967
}
968968

969-
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
969+
#if JSON_CPU_LITTLE_ENDIAN_64BITS
970970
// From: https://lemire.me/blog/2022/01/21/swar-explained-parsing-eight-digits/
971971
// Additional References:
972972
// https://johnnylee-sde.github.io/Fast-numeric-string-to-int/
@@ -995,8 +995,8 @@ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulat
995995
{
996996
const char *start = state->cursor;
997997

998-
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
999-
while (rest(state) >= 8) {
998+
#if JSON_CPU_LITTLE_ENDIAN_64BITS
999+
while (rest(state) >= sizeof(uint64_t)) {
10001000
uint64_t next_8bytes;
10011001
memcpy(&next_8bytes, state->cursor, sizeof(uint64_t));
10021002

0 commit comments

Comments
 (0)