json 2.10.2 → 2.12.2

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.
@@ -1,9 +1,12 @@
1
1
  #include "ruby.h"
2
2
  #include "../fbuffer/fbuffer.h"
3
+ #include "../vendor/fpconv.c"
3
4
 
4
5
  #include <math.h>
5
6
  #include <ctype.h>
6
7
 
8
+ #include "simd.h"
9
+
7
10
  /* ruby api and some helpers */
8
11
 
9
12
  typedef struct JSON_Generator_StateStruct {
@@ -44,7 +47,7 @@ static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_n
44
47
 
45
48
  struct generate_json_data;
46
49
 
47
- typedef void (*generator_func)(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
50
+ typedef void (*generator_func)(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
48
51
 
49
52
  struct generate_json_data {
50
53
  FBuffer *buffer;
@@ -56,20 +59,20 @@ struct generate_json_data {
56
59
 
57
60
  static VALUE cState_from_state_s(VALUE self, VALUE opts);
58
61
  static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func, VALUE io);
59
- static void generate_json(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
60
- static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
61
- static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
62
- static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
63
- static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
64
- static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
65
- static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
62
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
63
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
64
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
65
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
66
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
67
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
68
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
66
69
  #ifdef RUBY_INTEGER_UNIFICATION
67
- static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
70
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
68
71
  #endif
69
- static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
70
- static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
71
- static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
72
- static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
72
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
73
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
74
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
75
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
73
76
 
74
77
  static int usascii_encindex, utf8_encindex, binary_encindex;
75
78
 
@@ -108,12 +111,40 @@ typedef struct _search_state {
108
111
  const char *end;
109
112
  const char *cursor;
110
113
  FBuffer *buffer;
114
+
115
+ #ifdef HAVE_SIMD
116
+ const char *chunk_base;
117
+ const char *chunk_end;
118
+ bool has_matches;
119
+
120
+ #if defined(HAVE_SIMD_NEON)
121
+ uint64_t matches_mask;
122
+ #elif defined(HAVE_SIMD_SSE2)
123
+ int matches_mask;
124
+ #else
125
+ #error "Unknown SIMD Implementation."
126
+ #endif /* HAVE_SIMD_NEON */
127
+ #endif /* HAVE_SIMD */
111
128
  } search_state;
112
129
 
113
- static inline void search_flush(search_state *search)
114
- {
115
- fbuffer_append(search->buffer, search->cursor, search->ptr - search->cursor);
116
- search->cursor = search->ptr;
130
+ #if (defined(__GNUC__ ) || defined(__clang__))
131
+ #define FORCE_INLINE __attribute__((always_inline))
132
+ #else
133
+ #define FORCE_INLINE
134
+ #endif
135
+
136
+ static inline FORCE_INLINE void search_flush(search_state *search)
137
+ {
138
+ // Do not remove this conditional without profiling, specifically escape-heavy text.
139
+ // escape_UTF8_char_basic will advance search->ptr and search->cursor (effectively a search_flush).
140
+ // For back-to-back characters that need to be escaped, specifcally for the SIMD code paths, this method
141
+ // will be called just before calling escape_UTF8_char_basic. There will be no characers to append for the
142
+ // consecutive characters that need to be escaped. While the fbuffer_append is a no-op if
143
+ // nothing needs to be flushed, we can save a few memory references with this conditional.
144
+ if (search->ptr > search->cursor) {
145
+ fbuffer_append(search->buffer, search->cursor, search->ptr - search->cursor);
146
+ search->cursor = search->ptr;
147
+ }
117
148
  }
118
149
 
119
150
  static const unsigned char escape_table_basic[256] = {
@@ -129,6 +160,8 @@ static const unsigned char escape_table_basic[256] = {
129
160
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130
161
  };
131
162
 
163
+ static unsigned char (*search_escape_basic_impl)(search_state *);
164
+
132
165
  static inline unsigned char search_escape_basic(search_state *search)
133
166
  {
134
167
  while (search->ptr < search->end) {
@@ -143,7 +176,8 @@ static inline unsigned char search_escape_basic(search_state *search)
143
176
  return 0;
144
177
  }
145
178
 
146
- static inline void escape_UTF8_char_basic(search_state *search) {
179
+ static inline FORCE_INLINE void escape_UTF8_char_basic(search_state *search)
180
+ {
147
181
  const unsigned char ch = (unsigned char)*search->ptr;
148
182
  switch (ch) {
149
183
  case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
@@ -185,12 +219,13 @@ static inline void escape_UTF8_char_basic(search_state *search) {
185
219
  */
186
220
  static inline void convert_UTF8_to_JSON(search_state *search)
187
221
  {
188
- while (search_escape_basic(search)) {
222
+ while (search_escape_basic_impl(search)) {
189
223
  escape_UTF8_char_basic(search);
190
224
  }
191
225
  }
192
226
 
193
- static inline void escape_UTF8_char(search_state *search, unsigned char ch_len) {
227
+ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
228
+ {
194
229
  const unsigned char ch = (unsigned char)*search->ptr;
195
230
  switch (ch_len) {
196
231
  case 1: {
@@ -226,6 +261,280 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
226
261
  search->cursor = (search->ptr += ch_len);
227
262
  }
228
263
 
264
+ #ifdef HAVE_SIMD
265
+
266
+ static inline FORCE_INLINE char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
267
+ {
268
+ // Flush the buffer so everything up until the last 'len' characters are unflushed.
269
+ search_flush(search);
270
+
271
+ FBuffer *buf = search->buffer;
272
+ fbuffer_inc_capa(buf, vec_len);
273
+
274
+ char *s = (buf->ptr + buf->len);
275
+
276
+ // Pad the buffer with dummy characters that won't need escaping.
277
+ // This seem wateful at first sight, but memset of vector length is very fast.
278
+ memset(s, 'X', vec_len);
279
+
280
+ // Optimistically copy the remaining 'len' characters to the output FBuffer. If there are no characters
281
+ // to escape, then everything ends up in the correct spot. Otherwise it was convenient temporary storage.
282
+ MEMCPY(s, search->ptr, char, len);
283
+
284
+ return s;
285
+ }
286
+
287
+ #ifdef HAVE_SIMD_NEON
288
+
289
+ static inline FORCE_INLINE unsigned char neon_next_match(search_state *search)
290
+ {
291
+ uint64_t mask = search->matches_mask;
292
+ uint32_t index = trailing_zeros64(mask) >> 2;
293
+
294
+ // It is assumed escape_UTF8_char_basic will only ever increase search->ptr by at most one character.
295
+ // If we want to use a similar approach for full escaping we'll need to ensure:
296
+ // search->chunk_base + index >= search->ptr
297
+ // However, since we know escape_UTF8_char_basic only increases search->ptr by one, if the next match
298
+ // is one byte after the previous match then:
299
+ // search->chunk_base + index == search->ptr
300
+ search->ptr = search->chunk_base + index;
301
+ mask &= mask - 1;
302
+ search->matches_mask = mask;
303
+ search_flush(search);
304
+ return 1;
305
+ }
306
+
307
+ // See: https://community.arm.com/arm-community-blogs/b/servers-and-cloud-computing-blog/posts/porting-x86-vector-bitmask-optimizations-to-arm-neon
308
+ static inline FORCE_INLINE uint64_t neon_match_mask(uint8x16_t matches)
309
+ {
310
+ const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(matches), 4);
311
+ const uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(res), 0);
312
+ return mask & 0x8888888888888888ull;
313
+ }
314
+
315
+ static inline FORCE_INLINE uint64_t neon_rules_update(const char *ptr)
316
+ {
317
+ uint8x16_t chunk = vld1q_u8((const unsigned char *)ptr);
318
+
319
+ // Trick: c < 32 || c == 34 can be factored as c ^ 2 < 33
320
+ // https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
321
+ const uint8x16_t too_low_or_dbl_quote = vcltq_u8(veorq_u8(chunk, vdupq_n_u8(2)), vdupq_n_u8(33));
322
+
323
+ uint8x16_t has_backslash = vceqq_u8(chunk, vdupq_n_u8('\\'));
324
+ uint8x16_t needs_escape = vorrq_u8(too_low_or_dbl_quote, has_backslash);
325
+
326
+ return neon_match_mask(needs_escape);
327
+ }
328
+
329
+ static inline unsigned char search_escape_basic_neon(search_state *search)
330
+ {
331
+ if (RB_UNLIKELY(search->has_matches)) {
332
+ // There are more matches if search->matches_mask > 0.
333
+ if (search->matches_mask > 0) {
334
+ return neon_next_match(search);
335
+ } else {
336
+ // neon_next_match will only advance search->ptr up to the last matching character.
337
+ // Skip over any characters in the last chunk that occur after the last match.
338
+ search->has_matches = false;
339
+ search->ptr = search->chunk_end;
340
+ }
341
+ }
342
+
343
+ /*
344
+ * The code below implements an SIMD-based algorithm to determine if N bytes at a time
345
+ * need to be escaped.
346
+ *
347
+ * Assume the ptr = "Te\sting!" (the double quotes are included in the string)
348
+ *
349
+ * The explanation will be limited to the first 8 bytes of the string for simplicity. However
350
+ * the vector insructions may work on larger vectors.
351
+ *
352
+ * First, we load three constants 'lower_bound', 'backslash' and 'dblquote" in vector registers.
353
+ *
354
+ * lower_bound: [20 20 20 20 20 20 20 20]
355
+ * backslash: [5C 5C 5C 5C 5C 5C 5C 5C]
356
+ * dblquote: [22 22 22 22 22 22 22 22]
357
+ *
358
+ * Next we load the first chunk of the ptr:
359
+ * [22 54 65 5C 73 74 69 6E] (" T e \ s t i n)
360
+ *
361
+ * First we check if any byte in chunk is less than 32 (0x20). This returns the following vector
362
+ * as no bytes are less than 32 (0x20):
363
+ * [0 0 0 0 0 0 0 0]
364
+ *
365
+ * Next, we check if any byte in chunk is equal to a backslash:
366
+ * [0 0 0 FF 0 0 0 0]
367
+ *
368
+ * Finally we check if any byte in chunk is equal to a double quote:
369
+ * [FF 0 0 0 0 0 0 0]
370
+ *
371
+ * Now we have three vectors where each byte indicates if the corresponding byte in chunk
372
+ * needs to be escaped. We combine these vectors with a series of logical OR instructions.
373
+ * This is the needs_escape vector and it is equal to:
374
+ * [FF 0 0 FF 0 0 0 0]
375
+ *
376
+ * Next we compute the bitwise AND between each byte and 0x1 and compute the horizontal sum of
377
+ * the values in the vector. This computes how many bytes need to be escaped within this chunk.
378
+ *
379
+ * Finally we compute a mask that indicates which bytes need to be escaped. If the mask is 0 then,
380
+ * no bytes need to be escaped and we can continue to the next chunk. If the mask is not 0 then we
381
+ * have at least one byte that needs to be escaped.
382
+ */
383
+ while (search->ptr + sizeof(uint8x16_t) <= search->end) {
384
+ uint64_t mask = neon_rules_update(search->ptr);
385
+
386
+ if (!mask) {
387
+ search->ptr += sizeof(uint8x16_t);
388
+ continue;
389
+ }
390
+ search->matches_mask = mask;
391
+ search->has_matches = true;
392
+ search->chunk_base = search->ptr;
393
+ search->chunk_end = search->ptr + sizeof(uint8x16_t);
394
+ return neon_next_match(search);
395
+ }
396
+
397
+ // There are fewer than 16 bytes left.
398
+ unsigned long remaining = (search->end - search->ptr);
399
+ if (remaining >= SIMD_MINIMUM_THRESHOLD) {
400
+ char *s = copy_remaining_bytes(search, sizeof(uint8x16_t), remaining);
401
+
402
+ uint64_t mask = neon_rules_update(s);
403
+
404
+ if (!mask) {
405
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
406
+ // search->cursor to search->ptr.
407
+ fbuffer_consumed(search->buffer, remaining);
408
+ search->ptr = search->end;
409
+ search->cursor = search->end;
410
+ return 0;
411
+ }
412
+
413
+ search->matches_mask = mask;
414
+ search->has_matches = true;
415
+ search->chunk_end = search->end;
416
+ search->chunk_base = search->ptr;
417
+ return neon_next_match(search);
418
+ }
419
+
420
+ if (search->ptr < search->end) {
421
+ return search_escape_basic(search);
422
+ }
423
+
424
+ search_flush(search);
425
+ return 0;
426
+ }
427
+ #endif /* HAVE_SIMD_NEON */
428
+
429
+ #ifdef HAVE_SIMD_SSE2
430
+
431
+ #define _mm_cmpge_epu8(a, b) _mm_cmpeq_epi8(_mm_max_epu8(a, b), a)
432
+ #define _mm_cmple_epu8(a, b) _mm_cmpge_epu8(b, a)
433
+ #define _mm_cmpgt_epu8(a, b) _mm_xor_si128(_mm_cmple_epu8(a, b), _mm_set1_epi8(-1))
434
+ #define _mm_cmplt_epu8(a, b) _mm_cmpgt_epu8(b, a)
435
+
436
+ static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
437
+ {
438
+ int mask = search->matches_mask;
439
+ int index = trailing_zeros(mask);
440
+
441
+ // It is assumed escape_UTF8_char_basic will only ever increase search->ptr by at most one character.
442
+ // If we want to use a similar approach for full escaping we'll need to ensure:
443
+ // search->chunk_base + index >= search->ptr
444
+ // However, since we know escape_UTF8_char_basic only increases search->ptr by one, if the next match
445
+ // is one byte after the previous match then:
446
+ // search->chunk_base + index == search->ptr
447
+ search->ptr = search->chunk_base + index;
448
+ mask &= mask - 1;
449
+ search->matches_mask = mask;
450
+ search_flush(search);
451
+ return 1;
452
+ }
453
+
454
+ #if defined(__clang__) || defined(__GNUC__)
455
+ #define TARGET_SSE2 __attribute__((target("sse2")))
456
+ #else
457
+ #define TARGET_SSE2
458
+ #endif
459
+
460
+ static inline TARGET_SSE2 FORCE_INLINE int sse2_update(const char *ptr)
461
+ {
462
+ __m128i chunk = _mm_loadu_si128((__m128i const*)ptr);
463
+
464
+ // Trick: c < 32 || c == 34 can be factored as c ^ 2 < 33
465
+ // https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
466
+ __m128i too_low_or_dbl_quote = _mm_cmplt_epu8(_mm_xor_si128(chunk, _mm_set1_epi8(2)), _mm_set1_epi8(33));
467
+ __m128i has_backslash = _mm_cmpeq_epi8(chunk, _mm_set1_epi8('\\'));
468
+ __m128i needs_escape = _mm_or_si128(too_low_or_dbl_quote, has_backslash);
469
+ return _mm_movemask_epi8(needs_escape);
470
+ }
471
+
472
+ static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(search_state *search)
473
+ {
474
+ if (RB_UNLIKELY(search->has_matches)) {
475
+ // There are more matches if search->matches_mask > 0.
476
+ if (search->matches_mask > 0) {
477
+ return sse2_next_match(search);
478
+ } else {
479
+ // sse2_next_match will only advance search->ptr up to the last matching character.
480
+ // Skip over any characters in the last chunk that occur after the last match.
481
+ search->has_matches = false;
482
+ if (RB_UNLIKELY(search->chunk_base + sizeof(__m128i) >= search->end)) {
483
+ search->ptr = search->end;
484
+ } else {
485
+ search->ptr = search->chunk_base + sizeof(__m128i);
486
+ }
487
+ }
488
+ }
489
+
490
+ while (search->ptr + sizeof(__m128i) <= search->end) {
491
+ int needs_escape_mask = sse2_update(search->ptr);
492
+
493
+ if (needs_escape_mask == 0) {
494
+ search->ptr += sizeof(__m128i);
495
+ continue;
496
+ }
497
+
498
+ search->has_matches = true;
499
+ search->matches_mask = needs_escape_mask;
500
+ search->chunk_base = search->ptr;
501
+ return sse2_next_match(search);
502
+ }
503
+
504
+ // There are fewer than 16 bytes left.
505
+ unsigned long remaining = (search->end - search->ptr);
506
+ if (remaining >= SIMD_MINIMUM_THRESHOLD) {
507
+ char *s = copy_remaining_bytes(search, sizeof(__m128i), remaining);
508
+
509
+ int needs_escape_mask = sse2_update(s);
510
+
511
+ if (needs_escape_mask == 0) {
512
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
513
+ // search->cursor to search->ptr.
514
+ fbuffer_consumed(search->buffer, remaining);
515
+ search->ptr = search->end;
516
+ search->cursor = search->end;
517
+ return 0;
518
+ }
519
+
520
+ search->has_matches = true;
521
+ search->matches_mask = needs_escape_mask;
522
+ search->chunk_base = search->ptr;
523
+ return sse2_next_match(search);
524
+ }
525
+
526
+ if (search->ptr < search->end) {
527
+ return search_escape_basic(search);
528
+ }
529
+
530
+ search_flush(search);
531
+ return 0;
532
+ }
533
+
534
+ #endif /* HAVE_SIMD_SSE2 */
535
+
536
+ #endif /* HAVE_SIMD */
537
+
229
538
  static const unsigned char script_safe_escape_table[256] = {
230
539
  // ASCII Control Characters
231
540
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
@@ -788,6 +1097,21 @@ struct hash_foreach_arg {
788
1097
  int iter;
789
1098
  };
790
1099
 
1100
+ static VALUE
1101
+ convert_string_subclass(VALUE key)
1102
+ {
1103
+ VALUE key_to_s = rb_funcall(key, i_to_s, 0);
1104
+
1105
+ if (RB_UNLIKELY(!RB_TYPE_P(key_to_s, T_STRING))) {
1106
+ VALUE cname = rb_obj_class(key);
1107
+ rb_raise(rb_eTypeError,
1108
+ "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
1109
+ cname, "String", cname, "to_s", rb_obj_class(key_to_s));
1110
+ }
1111
+
1112
+ return key_to_s;
1113
+ }
1114
+
791
1115
  static int
792
1116
  json_object_i(VALUE key, VALUE val, VALUE _arg)
793
1117
  {
@@ -801,12 +1125,12 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
801
1125
  int j;
802
1126
 
803
1127
  if (arg->iter > 0) fbuffer_append_char(buffer, ',');
804
- if (RB_UNLIKELY(state->object_nl)) {
805
- fbuffer_append_str(buffer, state->object_nl);
1128
+ if (RB_UNLIKELY(data->state->object_nl)) {
1129
+ fbuffer_append_str(buffer, data->state->object_nl);
806
1130
  }
807
- if (RB_UNLIKELY(state->indent)) {
1131
+ if (RB_UNLIKELY(data->state->indent)) {
808
1132
  for (j = 0; j < depth; j++) {
809
- fbuffer_append_str(buffer, state->indent);
1133
+ fbuffer_append_str(buffer, data->state->indent);
810
1134
  }
811
1135
  }
812
1136
 
@@ -816,7 +1140,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
816
1140
  if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
817
1141
  key_to_s = key;
818
1142
  } else {
819
- key_to_s = rb_funcall(key, i_to_s, 0);
1143
+ key_to_s = convert_string_subclass(key);
820
1144
  }
821
1145
  break;
822
1146
  case T_SYMBOL:
@@ -828,21 +1152,22 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
828
1152
  }
829
1153
 
830
1154
  if (RB_LIKELY(RBASIC_CLASS(key_to_s) == rb_cString)) {
831
- generate_json_string(buffer, data, state, key_to_s);
1155
+ generate_json_string(buffer, data, key_to_s);
832
1156
  } else {
833
- generate_json(buffer, data, state, key_to_s);
1157
+ generate_json(buffer, data, key_to_s);
834
1158
  }
835
- if (RB_UNLIKELY(state->space_before)) fbuffer_append_str(buffer, state->space_before);
1159
+ if (RB_UNLIKELY(state->space_before)) fbuffer_append_str(buffer, data->state->space_before);
836
1160
  fbuffer_append_char(buffer, ':');
837
- if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, state->space);
838
- generate_json(buffer, data, state, val);
1161
+ if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, data->state->space);
1162
+ generate_json(buffer, data, val);
839
1163
 
840
1164
  arg->iter++;
841
1165
  return ST_CONTINUE;
842
1166
  }
843
1167
 
844
- static inline long increase_depth(JSON_Generator_State *state)
1168
+ static inline long increase_depth(struct generate_json_data *data)
845
1169
  {
1170
+ JSON_Generator_State *state = data->state;
846
1171
  long depth = ++state->depth;
847
1172
  if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
848
1173
  rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
@@ -850,14 +1175,14 @@ static inline long increase_depth(JSON_Generator_State *state)
850
1175
  return depth;
851
1176
  }
852
1177
 
853
- static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1178
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
854
1179
  {
855
1180
  int j;
856
- long depth = increase_depth(state);
1181
+ long depth = increase_depth(data);
857
1182
 
858
1183
  if (RHASH_SIZE(obj) == 0) {
859
1184
  fbuffer_append(buffer, "{}", 2);
860
- --state->depth;
1185
+ --data->state->depth;
861
1186
  return;
862
1187
  }
863
1188
 
@@ -869,49 +1194,49 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
869
1194
  };
870
1195
  rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
871
1196
 
872
- depth = --state->depth;
873
- if (RB_UNLIKELY(state->object_nl)) {
874
- fbuffer_append_str(buffer, state->object_nl);
875
- if (RB_UNLIKELY(state->indent)) {
1197
+ depth = --data->state->depth;
1198
+ if (RB_UNLIKELY(data->state->object_nl)) {
1199
+ fbuffer_append_str(buffer, data->state->object_nl);
1200
+ if (RB_UNLIKELY(data->state->indent)) {
876
1201
  for (j = 0; j < depth; j++) {
877
- fbuffer_append_str(buffer, state->indent);
1202
+ fbuffer_append_str(buffer, data->state->indent);
878
1203
  }
879
1204
  }
880
1205
  }
881
1206
  fbuffer_append_char(buffer, '}');
882
1207
  }
883
1208
 
884
- static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1209
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
885
1210
  {
886
1211
  int i, j;
887
- long depth = increase_depth(state);
1212
+ long depth = increase_depth(data);
888
1213
 
889
1214
  if (RARRAY_LEN(obj) == 0) {
890
1215
  fbuffer_append(buffer, "[]", 2);
891
- --state->depth;
1216
+ --data->state->depth;
892
1217
  return;
893
1218
  }
894
1219
 
895
1220
  fbuffer_append_char(buffer, '[');
896
- if (RB_UNLIKELY(state->array_nl)) fbuffer_append_str(buffer, state->array_nl);
1221
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
897
1222
  for(i = 0; i < RARRAY_LEN(obj); i++) {
898
1223
  if (i > 0) {
899
1224
  fbuffer_append_char(buffer, ',');
900
- if (RB_UNLIKELY(state->array_nl)) fbuffer_append_str(buffer, state->array_nl);
1225
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
901
1226
  }
902
- if (RB_UNLIKELY(state->indent)) {
1227
+ if (RB_UNLIKELY(data->state->indent)) {
903
1228
  for (j = 0; j < depth; j++) {
904
- fbuffer_append_str(buffer, state->indent);
1229
+ fbuffer_append_str(buffer, data->state->indent);
905
1230
  }
906
1231
  }
907
- generate_json(buffer, data, state, RARRAY_AREF(obj, i));
1232
+ generate_json(buffer, data, RARRAY_AREF(obj, i));
908
1233
  }
909
- state->depth = --depth;
910
- if (RB_UNLIKELY(state->array_nl)) {
911
- fbuffer_append_str(buffer, state->array_nl);
912
- if (RB_UNLIKELY(state->indent)) {
1234
+ data->state->depth = --depth;
1235
+ if (RB_UNLIKELY(data->state->array_nl)) {
1236
+ fbuffer_append_str(buffer, data->state->array_nl);
1237
+ if (RB_UNLIKELY(data->state->indent)) {
913
1238
  for (j = 0; j < depth; j++) {
914
- fbuffer_append_str(buffer, state->indent);
1239
+ fbuffer_append_str(buffer, data->state->indent);
915
1240
  }
916
1241
  }
917
1242
  }
@@ -960,7 +1285,7 @@ static inline VALUE ensure_valid_encoding(VALUE str)
960
1285
  return str;
961
1286
  }
962
1287
 
963
- static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1288
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
964
1289
  {
965
1290
  obj = ensure_valid_encoding(obj);
966
1291
 
@@ -973,12 +1298,18 @@ static void generate_json_string(FBuffer *buffer, struct generate_json_data *dat
973
1298
  search.cursor = search.ptr;
974
1299
  search.end = search.ptr + len;
975
1300
 
1301
+ #ifdef HAVE_SIMD
1302
+ search.matches_mask = 0;
1303
+ search.has_matches = false;
1304
+ search.chunk_base = NULL;
1305
+ #endif /* HAVE_SIMD */
1306
+
976
1307
  switch(rb_enc_str_coderange(obj)) {
977
1308
  case ENC_CODERANGE_7BIT:
978
1309
  case ENC_CODERANGE_VALID:
979
- if (RB_UNLIKELY(state->ascii_only)) {
980
- convert_UTF8_to_ASCII_only_JSON(&search, state->script_safe ? script_safe_escape_table : ascii_only_escape_table);
981
- } else if (RB_UNLIKELY(state->script_safe)) {
1310
+ if (RB_UNLIKELY(data->state->ascii_only)) {
1311
+ convert_UTF8_to_ASCII_only_JSON(&search, data->state->script_safe ? script_safe_escape_table : ascii_only_escape_table);
1312
+ } else if (RB_UNLIKELY(data->state->script_safe)) {
982
1313
  convert_UTF8_to_script_safe_JSON(&search);
983
1314
  } else {
984
1315
  convert_UTF8_to_JSON(&search);
@@ -991,7 +1322,7 @@ static void generate_json_string(FBuffer *buffer, struct generate_json_data *dat
991
1322
  fbuffer_append_char(buffer, '"');
992
1323
  }
993
1324
 
994
- static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1325
+ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
995
1326
  {
996
1327
  VALUE tmp;
997
1328
  if (rb_respond_to(obj, i_to_json)) {
@@ -1001,100 +1332,116 @@ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *d
1001
1332
  } else {
1002
1333
  tmp = rb_funcall(obj, i_to_s, 0);
1003
1334
  Check_Type(tmp, T_STRING);
1004
- generate_json_string(buffer, data, state, tmp);
1335
+ generate_json_string(buffer, data, tmp);
1005
1336
  }
1006
1337
  }
1007
1338
 
1008
- static inline void generate_json_symbol(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1339
+ static inline void generate_json_symbol(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1009
1340
  {
1010
- if (state->strict) {
1011
- generate_json_string(buffer, data, state, rb_sym2str(obj));
1341
+ if (data->state->strict) {
1342
+ generate_json_string(buffer, data, rb_sym2str(obj));
1012
1343
  } else {
1013
- generate_json_fallback(buffer, data, state, obj);
1344
+ generate_json_fallback(buffer, data, obj);
1014
1345
  }
1015
1346
  }
1016
1347
 
1017
- static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1348
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1018
1349
  {
1019
1350
  fbuffer_append(buffer, "null", 4);
1020
1351
  }
1021
1352
 
1022
- static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1353
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1023
1354
  {
1024
1355
  fbuffer_append(buffer, "false", 5);
1025
1356
  }
1026
1357
 
1027
- static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1358
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1028
1359
  {
1029
1360
  fbuffer_append(buffer, "true", 4);
1030
1361
  }
1031
1362
 
1032
- static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1363
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1033
1364
  {
1034
1365
  fbuffer_append_long(buffer, FIX2LONG(obj));
1035
1366
  }
1036
1367
 
1037
- static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1368
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1038
1369
  {
1039
1370
  VALUE tmp = rb_funcall(obj, i_to_s, 0);
1040
1371
  fbuffer_append_str(buffer, tmp);
1041
1372
  }
1042
1373
 
1043
1374
  #ifdef RUBY_INTEGER_UNIFICATION
1044
- static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1375
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1045
1376
  {
1046
1377
  if (FIXNUM_P(obj))
1047
- generate_json_fixnum(buffer, data, state, obj);
1378
+ generate_json_fixnum(buffer, data, obj);
1048
1379
  else
1049
- generate_json_bignum(buffer, data, state, obj);
1380
+ generate_json_bignum(buffer, data, obj);
1050
1381
  }
1051
1382
  #endif
1052
1383
 
1053
- static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1384
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1054
1385
  {
1055
1386
  double value = RFLOAT_VALUE(obj);
1056
- char allow_nan = state->allow_nan;
1057
- if (!allow_nan) {
1058
- if (isinf(value) || isnan(value)) {
1059
- if (state->strict && state->as_json) {
1060
- VALUE casted_obj = rb_proc_call_with_block(state->as_json, 1, &obj, Qnil);
1387
+ char allow_nan = data->state->allow_nan;
1388
+ if (isinf(value) || isnan(value)) {
1389
+ /* for NaN and Infinity values we either raise an error or rely on Float#to_s. */
1390
+ if (!allow_nan) {
1391
+ if (data->state->strict && data->state->as_json) {
1392
+ VALUE casted_obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1061
1393
  if (casted_obj != obj) {
1062
- increase_depth(state);
1063
- generate_json(buffer, data, state, casted_obj);
1064
- state->depth--;
1394
+ increase_depth(data);
1395
+ generate_json(buffer, data, casted_obj);
1396
+ data->state->depth--;
1065
1397
  return;
1066
1398
  }
1067
1399
  }
1068
1400
  raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
1069
1401
  }
1402
+
1403
+ VALUE tmp = rb_funcall(obj, i_to_s, 0);
1404
+ fbuffer_append_str(buffer, tmp);
1405
+ return;
1070
1406
  }
1071
- fbuffer_append_str(buffer, rb_funcall(obj, i_to_s, 0));
1407
+
1408
+ /* This implementation writes directly into the buffer. We reserve
1409
+ * the 28 characters that fpconv_dtoa states as its maximum.
1410
+ */
1411
+ fbuffer_inc_capa(buffer, 28);
1412
+ char* d = buffer->ptr + buffer->len;
1413
+ int len = fpconv_dtoa(value, d);
1414
+
1415
+ /* fpconv_dtoa converts a float to its shortest string representation,
1416
+ * but it adds a ".0" if this is a plain integer.
1417
+ */
1418
+ fbuffer_consumed(buffer, len);
1072
1419
  }
1073
1420
 
1074
- static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1421
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1075
1422
  {
1076
1423
  VALUE fragment = RSTRUCT_GET(obj, 0);
1077
1424
  Check_Type(fragment, T_STRING);
1078
1425
  fbuffer_append_str(buffer, fragment);
1079
1426
  }
1080
1427
 
1081
- static void generate_json(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1428
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1082
1429
  {
1083
1430
  bool as_json_called = false;
1084
1431
  start:
1085
1432
  if (obj == Qnil) {
1086
- generate_json_null(buffer, data, state, obj);
1433
+ generate_json_null(buffer, data, obj);
1087
1434
  } else if (obj == Qfalse) {
1088
- generate_json_false(buffer, data, state, obj);
1435
+ generate_json_false(buffer, data, obj);
1089
1436
  } else if (obj == Qtrue) {
1090
- generate_json_true(buffer, data, state, obj);
1437
+ generate_json_true(buffer, data, obj);
1091
1438
  } else if (RB_SPECIAL_CONST_P(obj)) {
1092
1439
  if (RB_FIXNUM_P(obj)) {
1093
- generate_json_fixnum(buffer, data, state, obj);
1440
+ generate_json_fixnum(buffer, data, obj);
1094
1441
  } else if (RB_FLONUM_P(obj)) {
1095
- generate_json_float(buffer, data, state, obj);
1442
+ generate_json_float(buffer, data, obj);
1096
1443
  } else if (RB_STATIC_SYM_P(obj)) {
1097
- generate_json_symbol(buffer, data, state, obj);
1444
+ generate_json_symbol(buffer, data, obj);
1098
1445
  } else {
1099
1446
  goto general;
1100
1447
  }
@@ -1102,43 +1449,43 @@ start:
1102
1449
  VALUE klass = RBASIC_CLASS(obj);
1103
1450
  switch (RB_BUILTIN_TYPE(obj)) {
1104
1451
  case T_BIGNUM:
1105
- generate_json_bignum(buffer, data, state, obj);
1452
+ generate_json_bignum(buffer, data, obj);
1106
1453
  break;
1107
1454
  case T_HASH:
1108
1455
  if (klass != rb_cHash) goto general;
1109
- generate_json_object(buffer, data, state, obj);
1456
+ generate_json_object(buffer, data, obj);
1110
1457
  break;
1111
1458
  case T_ARRAY:
1112
1459
  if (klass != rb_cArray) goto general;
1113
- generate_json_array(buffer, data, state, obj);
1460
+ generate_json_array(buffer, data, obj);
1114
1461
  break;
1115
1462
  case T_STRING:
1116
1463
  if (klass != rb_cString) goto general;
1117
- generate_json_string(buffer, data, state, obj);
1464
+ generate_json_string(buffer, data, obj);
1118
1465
  break;
1119
1466
  case T_SYMBOL:
1120
- generate_json_symbol(buffer, data, state, obj);
1467
+ generate_json_symbol(buffer, data, obj);
1121
1468
  break;
1122
1469
  case T_FLOAT:
1123
1470
  if (klass != rb_cFloat) goto general;
1124
- generate_json_float(buffer, data, state, obj);
1471
+ generate_json_float(buffer, data, obj);
1125
1472
  break;
1126
1473
  case T_STRUCT:
1127
1474
  if (klass != cFragment) goto general;
1128
- generate_json_fragment(buffer, data, state, obj);
1475
+ generate_json_fragment(buffer, data, obj);
1129
1476
  break;
1130
1477
  default:
1131
1478
  general:
1132
- if (state->strict) {
1133
- if (RTEST(state->as_json) && !as_json_called) {
1134
- obj = rb_proc_call_with_block(state->as_json, 1, &obj, Qnil);
1479
+ if (data->state->strict) {
1480
+ if (RTEST(data->state->as_json) && !as_json_called) {
1481
+ obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1135
1482
  as_json_called = true;
1136
1483
  goto start;
1137
1484
  } else {
1138
1485
  raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
1139
1486
  }
1140
1487
  } else {
1141
- generate_json_fallback(buffer, data, state, obj);
1488
+ generate_json_fallback(buffer, data, obj);
1142
1489
  }
1143
1490
  }
1144
1491
  }
@@ -1148,7 +1495,7 @@ static VALUE generate_json_try(VALUE d)
1148
1495
  {
1149
1496
  struct generate_json_data *data = (struct generate_json_data *)d;
1150
1497
 
1151
- data->func(data->buffer, data, data->state, data->obj);
1498
+ data->func(data->buffer, data, data->obj);
1152
1499
 
1153
1500
  return Qnil;
1154
1501
  }
@@ -1819,4 +2166,23 @@ void Init_generator(void)
1819
2166
  binary_encindex = rb_ascii8bit_encindex();
1820
2167
 
1821
2168
  rb_require("json/ext/generator/state");
2169
+
2170
+
2171
+ switch(find_simd_implementation()) {
2172
+ #ifdef HAVE_SIMD
2173
+ #ifdef HAVE_SIMD_NEON
2174
+ case SIMD_NEON:
2175
+ search_escape_basic_impl = search_escape_basic_neon;
2176
+ break;
2177
+ #endif /* HAVE_SIMD_NEON */
2178
+ #ifdef HAVE_SIMD_SSE2
2179
+ case SIMD_SSE2:
2180
+ search_escape_basic_impl = search_escape_basic_sse2;
2181
+ break;
2182
+ #endif /* HAVE_SIMD_SSE2 */
2183
+ #endif /* HAVE_SIMD */
2184
+ default:
2185
+ search_escape_basic_impl = search_escape_basic;
2186
+ break;
2187
+ }
1822
2188
  }