oj 3.16.0 → 3.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +107 -0
  3. data/README.md +0 -16
  4. data/ext/oj/cache.c +4 -2
  5. data/ext/oj/cache.h +3 -2
  6. data/ext/oj/compat.c +3 -3
  7. data/ext/oj/custom.c +17 -11
  8. data/ext/oj/dump.c +624 -90
  9. data/ext/oj/dump.h +9 -2
  10. data/ext/oj/dump_compat.c +15 -9
  11. data/ext/oj/dump_leaf.c +1 -1
  12. data/ext/oj/dump_object.c +32 -17
  13. data/ext/oj/dump_strict.c +20 -14
  14. data/ext/oj/extconf.rb +11 -9
  15. data/ext/oj/fast.c +50 -32
  16. data/ext/oj/intern.c +24 -7
  17. data/ext/oj/mimic_json.c +17 -11
  18. data/ext/oj/object.c +21 -13
  19. data/ext/oj/oj.c +387 -140
  20. data/ext/oj/oj.h +59 -55
  21. data/ext/oj/parse.c +248 -47
  22. data/ext/oj/parser.c +179 -89
  23. data/ext/oj/parser.h +4 -2
  24. data/ext/oj/rails.c +64 -42
  25. data/ext/oj/reader.c +1 -1
  26. data/ext/oj/rxclass.c +1 -1
  27. data/ext/oj/rxclass.h +1 -1
  28. data/ext/oj/safe.c +230 -0
  29. data/ext/oj/safe.h +79 -0
  30. data/ext/oj/saj.c +15 -10
  31. data/ext/oj/scp.c +3 -6
  32. data/ext/oj/simd.h +219 -0
  33. data/ext/oj/sparse.c +6 -3
  34. data/ext/oj/stream_writer.c +38 -28
  35. data/ext/oj/strict.c +2 -4
  36. data/ext/oj/string_writer.c +54 -22
  37. data/ext/oj/usual.c +57 -35
  38. data/ext/oj/usual.h +1 -0
  39. data/ext/oj/val_stack.c +13 -2
  40. data/ext/oj/wab.c +5 -4
  41. data/lib/oj/mimic.rb +1 -5
  42. data/lib/oj/schandler.rb +5 -4
  43. data/lib/oj/version.rb +1 -1
  44. data/pages/Encoding.md +1 -1
  45. metadata +36 -100
  46. data/test/_test_active.rb +0 -75
  47. data/test/_test_active_mimic.rb +0 -95
  48. data/test/_test_mimic_rails.rb +0 -123
  49. data/test/activerecord/result_test.rb +0 -31
  50. data/test/activesupport6/abstract_unit.rb +0 -44
  51. data/test/activesupport6/decoding_test.rb +0 -133
  52. data/test/activesupport6/encoding_test.rb +0 -507
  53. data/test/activesupport6/encoding_test_cases.rb +0 -98
  54. data/test/activesupport6/test_common.rb +0 -17
  55. data/test/activesupport6/test_helper.rb +0 -163
  56. data/test/activesupport6/time_zone_test_helpers.rb +0 -39
  57. data/test/activesupport7/abstract_unit.rb +0 -49
  58. data/test/activesupport7/decoding_test.rb +0 -125
  59. data/test/activesupport7/encoding_test.rb +0 -486
  60. data/test/activesupport7/encoding_test_cases.rb +0 -104
  61. data/test/activesupport7/time_zone_test_helpers.rb +0 -47
  62. data/test/files.rb +0 -29
  63. data/test/foo.rb +0 -14
  64. data/test/helper.rb +0 -39
  65. data/test/isolated/shared.rb +0 -309
  66. data/test/isolated/test_mimic_after.rb +0 -13
  67. data/test/isolated/test_mimic_alone.rb +0 -12
  68. data/test/isolated/test_mimic_as_json.rb +0 -45
  69. data/test/isolated/test_mimic_before.rb +0 -13
  70. data/test/isolated/test_mimic_define.rb +0 -28
  71. data/test/isolated/test_mimic_rails_after.rb +0 -22
  72. data/test/isolated/test_mimic_rails_before.rb +0 -21
  73. data/test/isolated/test_mimic_redefine.rb +0 -15
  74. data/test/json_gem/json_addition_test.rb +0 -216
  75. data/test/json_gem/json_common_interface_test.rb +0 -155
  76. data/test/json_gem/json_encoding_test.rb +0 -107
  77. data/test/json_gem/json_ext_parser_test.rb +0 -21
  78. data/test/json_gem/json_fixtures_test.rb +0 -36
  79. data/test/json_gem/json_generator_test.rb +0 -407
  80. data/test/json_gem/json_generic_object_test.rb +0 -90
  81. data/test/json_gem/json_parser_test.rb +0 -477
  82. data/test/json_gem/json_string_matching_test.rb +0 -42
  83. data/test/json_gem/test_helper.rb +0 -30
  84. data/test/mem.rb +0 -34
  85. data/test/perf.rb +0 -102
  86. data/test/perf_compat.rb +0 -128
  87. data/test/perf_dump.rb +0 -50
  88. data/test/perf_fast.rb +0 -162
  89. data/test/perf_file.rb +0 -62
  90. data/test/perf_object.rb +0 -134
  91. data/test/perf_once.rb +0 -59
  92. data/test/perf_parser.rb +0 -183
  93. data/test/perf_saj.rb +0 -101
  94. data/test/perf_scp.rb +0 -140
  95. data/test/perf_simple.rb +0 -289
  96. data/test/perf_strict.rb +0 -137
  97. data/test/perf_wab.rb +0 -129
  98. data/test/prec.rb +0 -23
  99. data/test/sample/change.rb +0 -13
  100. data/test/sample/dir.rb +0 -18
  101. data/test/sample/doc.rb +0 -35
  102. data/test/sample/file.rb +0 -47
  103. data/test/sample/group.rb +0 -15
  104. data/test/sample/hasprops.rb +0 -15
  105. data/test/sample/layer.rb +0 -11
  106. data/test/sample/line.rb +0 -20
  107. data/test/sample/oval.rb +0 -10
  108. data/test/sample/rect.rb +0 -9
  109. data/test/sample/shape.rb +0 -34
  110. data/test/sample/text.rb +0 -19
  111. data/test/sample.rb +0 -54
  112. data/test/sample_json.rb +0 -37
  113. data/test/test_compat.rb +0 -567
  114. data/test/test_custom.rb +0 -554
  115. data/test/test_debian.rb +0 -50
  116. data/test/test_fast.rb +0 -526
  117. data/test/test_file.rb +0 -250
  118. data/test/test_gc.rb +0 -60
  119. data/test/test_generate.rb +0 -21
  120. data/test/test_hash.rb +0 -39
  121. data/test/test_integer_range.rb +0 -72
  122. data/test/test_null.rb +0 -376
  123. data/test/test_object.rb +0 -1016
  124. data/test/test_parser.rb +0 -11
  125. data/test/test_parser_debug.rb +0 -27
  126. data/test/test_parser_saj.rb +0 -337
  127. data/test/test_parser_usual.rb +0 -217
  128. data/test/test_rails.rb +0 -35
  129. data/test/test_saj.rb +0 -188
  130. data/test/test_scp.rb +0 -431
  131. data/test/test_strict.rb +0 -431
  132. data/test/test_various.rb +0 -801
  133. data/test/test_wab.rb +0 -311
  134. data/test/test_writer.rb +0 -380
  135. data/test/tests.rb +0 -33
  136. data/test/tests_mimic.rb +0 -23
  137. data/test/tests_mimic_addition.rb +0 -16
data/ext/oj/oj.h CHANGED
@@ -104,61 +104,64 @@ typedef enum {
104
104
  } StreamWriterType;
105
105
 
106
106
  typedef struct _dumpOpts {
107
- bool use;
108
- char indent_str[16];
109
- char before_sep[16];
110
- char after_sep[16];
111
- char hash_nl[16];
112
- char array_nl[16];
113
- uint8_t indent_size;
114
- uint8_t before_size;
115
- uint8_t after_size;
116
- uint8_t hash_size;
117
- uint8_t array_size;
118
- char nan_dump; // NanDump
119
- bool omit_nil;
120
- bool omit_null_byte;
121
- int max_depth;
107
+ bool use;
108
+ char indent_str[16];
109
+ char before_sep[16];
110
+ char after_sep[16];
111
+ char hash_nl[16];
112
+ char array_nl[16];
113
+ uint8_t indent_size;
114
+ uint8_t before_size;
115
+ uint8_t after_size;
116
+ uint8_t hash_size;
117
+ uint8_t array_size;
118
+ char nan_dump; // NanDump
119
+ bool omit_nil;
120
+ bool omit_null_byte;
121
+ int max_depth;
122
+ const char *only;
123
+ const char *except;
122
124
  } *DumpOpts;
123
125
 
124
126
  typedef struct _options {
125
- int indent; // indention for dump, default 2
126
- char circular; // YesNo
127
- char auto_define; // YesNo
128
- char sym_key; // YesNo
129
- char escape_mode; // Escape_Mode
130
- char mode; // Mode
131
- char class_cache; // YesNo
132
- char time_format; // TimeFormat
133
- char bigdec_as_num; // YesNo
134
- char bigdec_load; // BigLoad
135
- char compat_bigdec; // boolean (0 or 1)
136
- char to_hash; // YesNo
137
- char to_json; // YesNo
138
- char as_json; // YesNo
139
- char raw_json; // YesNo
140
- char nilnil; // YesNo
141
- char empty_string; // YesNo
142
- char allow_gc; // allow GC during parse
143
- char quirks_mode; // allow single JSON values instead of documents
144
- char allow_invalid; // YesNo - allow invalid unicode
145
- char create_ok; // YesNo allow create_id
146
- char allow_nan; // YEsyNo for parsing only
147
- char trace; // YesNo
148
- char safe; // YesNo
149
- char sec_prec_set; // boolean (0 or 1)
150
- char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
151
- char cache_keys; // YesNo
152
- char cache_str; // string short than or equal to this are cache
153
- int64_t int_range_min; // dump numbers below as string
154
- int64_t int_range_max; // dump numbers above as string
155
- const char *create_id; // 0 or string
156
- size_t create_id_len; // length of create_id
157
- int sec_prec; // second precision when dumping time
158
- char float_prec; // float precision, linked to float_fmt
159
- char float_fmt[7]; // float format for dumping, if empty use Ruby
160
- VALUE hash_class; // class to use in place of Hash on load
161
- VALUE array_class; // class to use in place of Array on load
127
+ int indent; // indention for dump, default 2
128
+ char circular; // YesNo
129
+ char auto_define; // YesNo
130
+ char sym_key; // YesNo
131
+ char escape_mode; // Escape_Mode
132
+ char mode; // Mode
133
+ char class_cache; // YesNo
134
+ char time_format; // TimeFormat
135
+ char bigdec_as_num; // YesNo
136
+ char bigdec_load; // BigLoad
137
+ char compat_bigdec; // boolean (0 or 1)
138
+ char to_hash; // YesNo
139
+ char to_json; // YesNo
140
+ char as_json; // YesNo
141
+ char raw_json; // YesNo
142
+ char nilnil; // YesNo
143
+ char empty_string; // YesNo
144
+ char allow_gc; // allow GC during parse
145
+ char quirks_mode; // allow single JSON values instead of documents
146
+ char allow_invalid; // YesNo - allow invalid unicode
147
+ char create_ok; // YesNo allow create_id
148
+ char allow_nan; // YesNo for parsing only
149
+ char trace; // YesNo
150
+ char safe; // YesNo
151
+ char sec_prec_set; // boolean (0 or 1)
152
+ char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
153
+ char cache_keys; // YesNo
154
+ char cache_str; // string short than or equal to this are cache
155
+ int64_t int_range_min; // dump numbers below as string
156
+ int64_t int_range_max; // dump numbers above as string
157
+ size_t max_integer_digits; // 0 = unlimited; max decimal digits for parsed integers
158
+ const char *create_id; // 0 or string
159
+ size_t create_id_len; // length of create_id
160
+ int sec_prec; // second precision when dumping time
161
+ char float_prec; // float precision, linked to float_fmt
162
+ char float_fmt[7]; // float format for dumping, if empty use Ruby
163
+ VALUE hash_class; // class to use in place of Hash on load
164
+ VALUE array_class; // class to use in place of Array on load
162
165
  struct _dumpOpts dump_opts;
163
166
  struct _rxClass str_rx;
164
167
  VALUE *ignore; // Qnil terminated array of classes or NULL
@@ -262,6 +265,8 @@ extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
262
265
  extern void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts);
263
266
  extern char *oj_longlong_to_string(long long num, bool negative, char *buf);
264
267
 
268
+ extern StrWriter oj_str_writer_unwrap(VALUE writer);
269
+
265
270
  extern void oj_str_writer_push_key(StrWriter sw, const char *key);
266
271
  extern void oj_str_writer_push_object(StrWriter sw, const char *key);
267
272
  extern void oj_str_writer_push_array(StrWriter sw, const char *key);
@@ -311,9 +316,7 @@ extern VALUE oj_ascii_only_sym;
311
316
  extern VALUE oj_create_additions_sym;
312
317
  extern VALUE oj_decimal_class_sym;
313
318
  extern VALUE oj_hash_class_sym;
314
- extern VALUE oj_in_sym;
315
319
  extern VALUE oj_indent_sym;
316
- extern VALUE oj_nanosecond_sym;
317
320
  extern VALUE oj_max_nesting_sym;
318
321
  extern VALUE oj_object_class_sym;
319
322
  extern VALUE oj_object_nl_sym;
@@ -331,10 +334,10 @@ extern ID oj_array_append_id;
331
334
  extern ID oj_array_end_id;
332
335
  extern ID oj_array_start_id;
333
336
  extern ID oj_as_json_id;
334
- extern ID oj_at_id;
335
337
  extern ID oj_begin_id;
336
338
  extern ID oj_bigdecimal_id;
337
339
  extern ID oj_end_id;
340
+ extern ID oj_eofq_id;
338
341
  extern ID oj_error_id;
339
342
  extern ID oj_exclude_end_id;
340
343
  extern ID oj_file_id;
@@ -349,6 +352,7 @@ extern ID oj_json_create_id;
349
352
  extern ID oj_length_id;
350
353
  extern ID oj_new_id;
351
354
  extern ID oj_parse_id;
355
+ extern ID oj_plus_id;
352
356
  extern ID oj_pos_id;
353
357
  extern ID oj_read_id;
354
358
  extern ID oj_readpartial_id;
data/ext/oj/parse.c CHANGED
@@ -15,12 +15,9 @@
15
15
  #include "mem.h"
16
16
  #include "oj.h"
17
17
  #include "rxclass.h"
18
+ #include "simd.h"
18
19
  #include "val_stack.h"
19
20
 
20
- #ifdef OJ_USE_SSE4_2
21
- #include <nmmintrin.h>
22
- #endif
23
-
24
21
  // Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
25
22
  #define OJ_INFINITY (1.0 / 0.0)
26
23
 
@@ -183,32 +180,189 @@ static void unicode_to_chars(ParseInfo pi, Buf buf, uint32_t code) {
183
180
  }
184
181
  }
185
182
 
183
+ static const unsigned char end_of_scan_string[] = {
184
+ // Filled 1 at the positions of '\0', '\\', and '"'
185
+ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
186
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192
+ };
186
193
  static inline const char *scan_string_noSIMD(const char *str, const char *end) {
187
- for (; '"' != *str; str++) {
188
- if (end <= str || '\0' == *str || '\\' == *str) {
194
+ for (; str < end; str++) {
195
+ if (end_of_scan_string[(unsigned char)*str]) {
189
196
  break;
190
197
  }
191
198
  }
192
199
  return str;
193
200
  }
194
201
 
195
- #ifdef OJ_USE_SSE4_2
196
- static inline const char *scan_string_SIMD(const char *str, const char *end) {
197
- static const char chars[16] = "\x00\\\"";
198
- const __m128i terminate = _mm_loadu_si128((const __m128i *)&chars[0]);
199
- const char *_end = (const char *)(end - 16);
202
+ #ifdef HAVE_SIMD_NEON
203
+
204
+ static inline const char *string_scan_neon(const char *str, const char *end) {
205
+ const uint8x16_t null_char = vdupq_n_u8(0);
206
+ const uint8x16_t backslash = vdupq_n_u8('\\');
207
+ const uint8x16_t quote = vdupq_n_u8('"');
208
+
209
+ while (str + sizeof(uint8x16_t) <= end) {
210
+ uint8x16_t chunk = vld1q_u8((const uint8_t *)str);
211
+ uint8x16_t tmp = vorrq_u8(vorrq_u8(vceqq_u8(chunk, null_char), vceqq_u8(chunk, backslash)),
212
+ vceqq_u8(chunk, quote));
213
+ const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(tmp), 4);
214
+ uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(res), 0);
215
+ if (mask != 0) {
216
+ mask &= 0x8888888888888888ull;
217
+ return str + (OJ_CTZ64(mask) >> 2);
218
+ }
219
+ str += sizeof(uint8x16_t);
220
+ }
221
+
222
+ return scan_string_noSIMD(str, end);
223
+ }
224
+
225
+ #endif
226
+
227
+ #ifdef HAVE_SIMD_SSE4_2
228
+ // Optimized SIMD string scanner using SSE4.2 instructions
229
+ // Uses prefetching and processes multiple chunks in parallel to reduce latency
230
+ // Note: OJ_TARGET_SSE42 attribute allows this to compile even without global -msse4.2
231
+ static OJ_TARGET_SSE42 const char *scan_string_SSE42(const char *str, const char *end) {
232
+ static const char chars[16] = "\x00\\\"";
233
+ const __m128i terminate = _mm_loadu_si128((const __m128i *)&chars[0]);
234
+ const char *safe_end_64 = end - 64;
235
+ const char *safe_end_16 = end - 16;
236
+
237
+ // Process 64 bytes at a time with parallel SIMD operations
238
+ // This reduces pipeline stalls and improves instruction-level parallelism
239
+ while (str <= safe_end_64) {
240
+ // Prefetch next cache line for better memory throughput
241
+ OJ_PREFETCH(str + 64);
242
+
243
+ // Load and compare 4 chunks in parallel
244
+ const __m128i chunk0 = _mm_loadu_si128((const __m128i *)(str));
245
+ const __m128i chunk1 = _mm_loadu_si128((const __m128i *)(str + 16));
246
+ const __m128i chunk2 = _mm_loadu_si128((const __m128i *)(str + 32));
247
+ const __m128i chunk3 = _mm_loadu_si128((const __m128i *)(str + 48));
248
+
249
+ const int r0 = _mm_cmpestri(terminate,
250
+ 3,
251
+ chunk0,
252
+ 16,
253
+ _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
254
+ if (OJ_UNLIKELY(r0 != 16))
255
+ return str + r0;
256
+
257
+ const int r1 = _mm_cmpestri(terminate,
258
+ 3,
259
+ chunk1,
260
+ 16,
261
+ _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
262
+ if (OJ_UNLIKELY(r1 != 16))
263
+ return str + 16 + r1;
264
+
265
+ const int r2 = _mm_cmpestri(terminate,
266
+ 3,
267
+ chunk2,
268
+ 16,
269
+ _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
270
+ if (OJ_UNLIKELY(r2 != 16))
271
+ return str + 32 + r2;
272
+
273
+ const int r3 = _mm_cmpestri(terminate,
274
+ 3,
275
+ chunk3,
276
+ 16,
277
+ _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
278
+ if (OJ_UNLIKELY(r3 != 16))
279
+ return str + 48 + r3;
280
+
281
+ str += 64;
282
+ }
200
283
 
201
- for (; str <= _end; str += 16) {
284
+ // Handle remaining 16-byte chunks
285
+ for (; str <= safe_end_16; str += 16) {
202
286
  const __m128i string = _mm_loadu_si128((const __m128i *)str);
203
287
  const int r = _mm_cmpestri(terminate,
204
- 3,
205
- string,
206
- 16,
207
- _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
208
- if (r != 16) {
209
- str = (char *)(str + r);
210
- return str;
211
- }
288
+ 3,
289
+ string,
290
+ 16,
291
+ _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
292
+ if (r != 16)
293
+ return str + r;
294
+ }
295
+
296
+ return scan_string_noSIMD(str, end);
297
+ }
298
+ #endif
299
+
300
+ #ifdef HAVE_SIMD_SSE2
301
+ // Optimized SSE2 string scanner (fallback for older x86_64 CPUs)
302
+ // Uses SSE2 instructions with prefetching and parallel processing
303
+ // Note: OJ_TARGET_SSE2 attribute allows this to compile even without global -msse2
304
+ static OJ_TARGET_SSE2 const char *scan_string_SSE2(const char *str, const char *end) {
305
+ const char *safe_end_64 = end - 64;
306
+ const char *safe_end_16 = end - 16;
307
+
308
+ // Create comparison vectors for our three special characters
309
+ const __m128i null_char = _mm_setzero_si128();
310
+ const __m128i backslash = _mm_set1_epi8('\\');
311
+ const __m128i quote = _mm_set1_epi8('"');
312
+
313
+ // Process 64 bytes at a time for better throughput
314
+ while (str <= safe_end_64) {
315
+ OJ_PREFETCH(str + 64);
316
+
317
+ // Load 4 chunks
318
+ const __m128i chunk0 = _mm_loadu_si128((const __m128i *)(str));
319
+ const __m128i chunk1 = _mm_loadu_si128((const __m128i *)(str + 16));
320
+ const __m128i chunk2 = _mm_loadu_si128((const __m128i *)(str + 32));
321
+ const __m128i chunk3 = _mm_loadu_si128((const __m128i *)(str + 48));
322
+
323
+ // Compare all chunks (allows CPU to parallelize)
324
+ const __m128i cmp0 = _mm_or_si128(
325
+ _mm_or_si128(_mm_cmpeq_epi8(chunk0, null_char), _mm_cmpeq_epi8(chunk0, backslash)),
326
+ _mm_cmpeq_epi8(chunk0, quote));
327
+ const __m128i cmp1 = _mm_or_si128(
328
+ _mm_or_si128(_mm_cmpeq_epi8(chunk1, null_char), _mm_cmpeq_epi8(chunk1, backslash)),
329
+ _mm_cmpeq_epi8(chunk1, quote));
330
+ const __m128i cmp2 = _mm_or_si128(
331
+ _mm_or_si128(_mm_cmpeq_epi8(chunk2, null_char), _mm_cmpeq_epi8(chunk2, backslash)),
332
+ _mm_cmpeq_epi8(chunk2, quote));
333
+ const __m128i cmp3 = _mm_or_si128(
334
+ _mm_or_si128(_mm_cmpeq_epi8(chunk3, null_char), _mm_cmpeq_epi8(chunk3, backslash)),
335
+ _mm_cmpeq_epi8(chunk3, quote));
336
+
337
+ // Convert to masks
338
+ int mask0 = _mm_movemask_epi8(cmp0);
339
+ if (OJ_UNLIKELY(mask0 != 0))
340
+ return str + OJ_CTZ(mask0);
341
+
342
+ int mask1 = _mm_movemask_epi8(cmp1);
343
+ if (OJ_UNLIKELY(mask1 != 0))
344
+ return str + 16 + OJ_CTZ(mask1);
345
+
346
+ int mask2 = _mm_movemask_epi8(cmp2);
347
+ if (OJ_UNLIKELY(mask2 != 0))
348
+ return str + 32 + OJ_CTZ(mask2);
349
+
350
+ int mask3 = _mm_movemask_epi8(cmp3);
351
+ if (OJ_UNLIKELY(mask3 != 0))
352
+ return str + 48 + OJ_CTZ(mask3);
353
+
354
+ str += 64;
355
+ }
356
+
357
+ // Handle remaining 16-byte chunks
358
+ for (; str <= safe_end_16; str += 16) {
359
+ const __m128i chunk = _mm_loadu_si128((const __m128i *)str);
360
+ const __m128i matches = _mm_or_si128(
361
+ _mm_or_si128(_mm_cmpeq_epi8(chunk, null_char), _mm_cmpeq_epi8(chunk, backslash)),
362
+ _mm_cmpeq_epi8(chunk, quote));
363
+ int mask = _mm_movemask_epi8(matches);
364
+ if (mask != 0)
365
+ return str + OJ_CTZ(mask);
212
366
  }
213
367
 
214
368
  return scan_string_noSIMD(str, end);
@@ -218,16 +372,29 @@ static inline const char *scan_string_SIMD(const char *str, const char *end) {
218
372
  static const char *(*scan_func)(const char *str, const char *end) = scan_string_noSIMD;
219
373
 
220
374
  void oj_scanner_init(void) {
221
- #ifdef OJ_USE_SSE4_2
222
- scan_func = scan_string_SIMD;
375
+ // Use runtime CPU detection to select the best SIMD implementation
376
+ // This ensures we don't crash on CPUs that don't support SSE4.2
377
+ SIMD_Implementation impl = oj_get_simd_implementation();
378
+
379
+ switch (impl) {
380
+ #ifdef HAVE_SIMD_SSE4_2
381
+ case SIMD_SSE42: scan_func = scan_string_SSE42; break;
382
+ #endif
383
+ #ifdef HAVE_SIMD_SSE2
384
+ case SIMD_SSE2: scan_func = scan_string_SSE2; break;
385
+ #endif
386
+ #ifdef HAVE_SIMD_NEON
387
+ case SIMD_NEON: scan_func = string_scan_neon; break;
223
388
  #endif
389
+ default: scan_func = scan_string_noSIMD; break;
390
+ }
224
391
  }
225
392
 
226
393
  // entered at /
227
394
  static void read_escaped_str(ParseInfo pi, const char *start) {
228
395
  struct _buf buf;
229
396
  const char *s;
230
- int cnt = (int)(pi->cur - start);
397
+ size_t cnt = pi->cur - start;
231
398
  uint32_t code;
232
399
  Val parent = stack_peek(&pi->stack);
233
400
 
@@ -444,7 +611,7 @@ static void read_num(ParseInfo pi) {
444
611
  ni.bigdec_load = pi->options.compat_bigdec;
445
612
  } else {
446
613
  ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load ||
447
- RubyDec == pi->options.bigdec_load);
614
+ RubyDec == pi->options.bigdec_load);
448
615
  ni.bigdec_load = pi->options.bigdec_load;
449
616
  }
450
617
 
@@ -502,7 +669,7 @@ static void read_num(ParseInfo pi) {
502
669
  // A trailing . is not a valid decimal but if encountered allow it
503
670
  // except when mimicking the JSON gem or in strict mode.
504
671
  if (StrictMode == pi->options.mode || CompatMode == pi->options.mode) {
505
- int pos = (int)(pi->cur - ni.str);
672
+ size_t pos = pi->cur - ni.str;
506
673
 
507
674
  if (1 == pos || (2 == pos && ni.neg)) {
508
675
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number");
@@ -681,7 +848,7 @@ void oj_parse2(ParseInfo pi) {
681
848
  pi->cur = pi->json;
682
849
  err_init(&pi->err);
683
850
  while (1) {
684
- if (0 < pi->max_depth && pi->max_depth <= pi->stack.tail - pi->stack.head - 1) {
851
+ if (RB_UNLIKELY(0 < pi->max_depth && pi->max_depth <= pi->stack.tail - pi->stack.head - 1)) {
685
852
  VALUE err_clas = oj_get_json_err_class("NestingError");
686
853
 
687
854
  oj_set_error_at(pi, err_clas, __FILE__, __LINE__, "Too deeply nested.");
@@ -689,18 +856,20 @@ void oj_parse2(ParseInfo pi) {
689
856
  return;
690
857
  }
691
858
  next_non_white(pi);
692
- if (!first && '\0' != *pi->cur) {
693
- oj_set_error_at(pi,
694
- oj_parse_error_class,
695
- __FILE__,
696
- __LINE__,
697
- "unexpected characters after the JSON document");
698
- }
699
-
700
- // If no tokens are consumed (i.e. empty string), throw a parse error
701
- // this is the behavior of JSON.parse in both Ruby and JS.
702
- if (No == pi->options.empty_string && 1 == first && '\0' == *pi->cur) {
703
- oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "unexpected character");
859
+ if (first) {
860
+ // If no tokens are consumed (i.e. empty string), throw a parse error
861
+ // this is the behavior of JSON.parse in both Ruby and JS.
862
+ if (RB_UNLIKELY('\0' == *pi->cur && No == pi->options.empty_string)) {
863
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "unexpected character");
864
+ }
865
+ } else {
866
+ if (RB_UNLIKELY('\0' != *pi->cur)) {
867
+ oj_set_error_at(pi,
868
+ oj_parse_error_class,
869
+ __FILE__,
870
+ __LINE__,
871
+ "unexpected characters after the JSON document");
872
+ }
704
873
  }
705
874
 
706
875
  switch (*pi->cur++) {
@@ -761,7 +930,7 @@ void oj_parse2(ParseInfo pi) {
761
930
  case '\0': pi->cur--; return;
762
931
  default: oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "unexpected character"); return;
763
932
  }
764
- if (err_has(&pi->err)) {
933
+ if (RB_UNLIKELY(err_has(&pi->err))) {
765
934
  return;
766
935
  }
767
936
  if (stack_empty(&pi->stack)) {
@@ -802,6 +971,20 @@ static long double exp_plus[] = {
802
971
  1.0e39, 1.0e40, 1.0e41, 1.0e42, 1.0e43, 1.0e44, 1.0e45, 1.0e46, 1.0e47, 1.0e48, 1.0e49,
803
972
  };
804
973
 
974
+ static void validate_integer_size(size_t limit, NumInfo ni) {
975
+ size_t digit_count = ni->len - (ni->neg ? 1 : 0);
976
+
977
+ if (digit_count > limit) {
978
+ oj_set_error_at(ni->pi,
979
+ (Qnil != ni->pi->err_class) ? ni->pi->err_class : oj_parse_error_class,
980
+ __FILE__,
981
+ __LINE__,
982
+ "integer exceeds :max_integer_digits (%lu > %lu)",
983
+ (unsigned long)digit_count,
984
+ (unsigned long)limit);
985
+ }
986
+ }
987
+
805
988
  VALUE
806
989
  oj_num_as_value(NumInfo ni) {
807
990
  VALUE rnum = Qnil;
@@ -815,6 +998,12 @@ oj_num_as_value(NumInfo ni) {
815
998
  } else if (ni->nan) {
816
999
  rnum = rb_float_new(0.0 / 0.0);
817
1000
  } else if (1 == ni->div && 0 == ni->exp && !ni->has_exp) { // fixnum
1001
+ size_t limit = (NULL != ni->pi) ? ni->pi->options.max_integer_digits : 0;
1002
+
1003
+ if (0 < limit) {
1004
+ validate_integer_size(limit, ni);
1005
+ }
1006
+
818
1007
  if (ni->big) {
819
1008
  if (256 > ni->len) {
820
1009
  char buf[256];
@@ -875,7 +1064,11 @@ oj_num_as_value(NumInfo ni) {
875
1064
  double d = strtod(ni->str, &end);
876
1065
 
877
1066
  if ((long)ni->len != (long)(end - ni->str)) {
878
- rb_raise(ni->pi->err_class, "Invalid float");
1067
+ if (Qnil == ni->pi->err_class) {
1068
+ rb_raise(oj_parse_error_class, "Invalid float");
1069
+ } else {
1070
+ rb_raise(ni->pi->err_class, "Invalid float");
1071
+ }
879
1072
  }
880
1073
  rnum = rb_float_new(d);
881
1074
  }
@@ -1020,11 +1213,19 @@ oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yie
1020
1213
  buf = OJ_R_ALLOC_N(char, len + 1);
1021
1214
  pi->json = buf;
1022
1215
  pi->end = buf + len;
1023
- if (0 >= (cnt = read(fd, (char *)pi->json, len)) || cnt != (ssize_t)len) {
1024
- if (0 != buf) {
1025
- OJ_R_FREE(buf);
1216
+ {
1217
+ size_t total = 0;
1218
+
1219
+ while (total < len) {
1220
+ cnt = read(fd, (char *)pi->json + total, len - total);
1221
+ if (cnt <= 0) {
1222
+ if (0 != buf) {
1223
+ OJ_R_FREE(buf);
1224
+ }
1225
+ rb_raise(rb_eIOError, "failed to read from IO Object.");
1226
+ }
1227
+ total += cnt;
1026
1228
  }
1027
- rb_raise(rb_eIOError, "failed to read from IO Object.");
1028
1229
  }
1029
1230
  ((char *)pi->json)[len] = '\0';
1030
1231
  /* skip UTF-8 BOM if present */
@@ -1117,12 +1318,12 @@ CLEANUP:
1117
1318
  // The json gem requires the error message be UTF-8 encoded. In
1118
1319
  // additional the complete JSON source must be returned. There
1119
1320
  // does not seem to be a size limit.
1120
- VALUE msg = oj_encode(rb_str_new2(pi->err.msg));
1321
+ VALUE msg = rb_utf8_str_new_cstr(pi->err.msg);
1121
1322
  VALUE args[1];
1122
1323
 
1123
1324
  if (NULL != pi->json) {
1124
- msg = rb_str_append(msg, oj_encode(rb_str_new2(" in '")));
1125
- msg = rb_str_append(msg, oj_encode(rb_str_new2(pi->json)));
1325
+ msg = rb_str_append(msg, rb_utf8_str_new_cstr(" in '"));
1326
+ msg = rb_str_append(msg, rb_utf8_str_new_cstr(pi->json));
1126
1327
  }
1127
1328
  args[0] = msg;
1128
1329
  if (pi->err.clas == oj_parse_error_class) {