json 2.10.2 → 2.13.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/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,228 @@ 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
+ static inline unsigned char search_escape_basic_neon(search_state *search)
308
+ {
309
+ if (RB_UNLIKELY(search->has_matches)) {
310
+ // There are more matches if search->matches_mask > 0.
311
+ if (search->matches_mask > 0) {
312
+ return neon_next_match(search);
313
+ } else {
314
+ // neon_next_match will only advance search->ptr up to the last matching character.
315
+ // Skip over any characters in the last chunk that occur after the last match.
316
+ search->has_matches = false;
317
+ search->ptr = search->chunk_end;
318
+ }
319
+ }
320
+
321
+ /*
322
+ * The code below implements an SIMD-based algorithm to determine if N bytes at a time
323
+ * need to be escaped.
324
+ *
325
+ * Assume the ptr = "Te\sting!" (the double quotes are included in the string)
326
+ *
327
+ * The explanation will be limited to the first 8 bytes of the string for simplicity. However
328
+ * the vector insructions may work on larger vectors.
329
+ *
330
+ * First, we load three constants 'lower_bound', 'backslash' and 'dblquote" in vector registers.
331
+ *
332
+ * lower_bound: [20 20 20 20 20 20 20 20]
333
+ * backslash: [5C 5C 5C 5C 5C 5C 5C 5C]
334
+ * dblquote: [22 22 22 22 22 22 22 22]
335
+ *
336
+ * Next we load the first chunk of the ptr:
337
+ * [22 54 65 5C 73 74 69 6E] (" T e \ s t i n)
338
+ *
339
+ * First we check if any byte in chunk is less than 32 (0x20). This returns the following vector
340
+ * as no bytes are less than 32 (0x20):
341
+ * [0 0 0 0 0 0 0 0]
342
+ *
343
+ * Next, we check if any byte in chunk is equal to a backslash:
344
+ * [0 0 0 FF 0 0 0 0]
345
+ *
346
+ * Finally we check if any byte in chunk is equal to a double quote:
347
+ * [FF 0 0 0 0 0 0 0]
348
+ *
349
+ * Now we have three vectors where each byte indicates if the corresponding byte in chunk
350
+ * needs to be escaped. We combine these vectors with a series of logical OR instructions.
351
+ * This is the needs_escape vector and it is equal to:
352
+ * [FF 0 0 FF 0 0 0 0]
353
+ *
354
+ * Next we compute the bitwise AND between each byte and 0x1 and compute the horizontal sum of
355
+ * the values in the vector. This computes how many bytes need to be escaped within this chunk.
356
+ *
357
+ * Finally we compute a mask that indicates which bytes need to be escaped. If the mask is 0 then,
358
+ * no bytes need to be escaped and we can continue to the next chunk. If the mask is not 0 then we
359
+ * have at least one byte that needs to be escaped.
360
+ */
361
+
362
+ if (string_scan_simd_neon(&search->ptr, search->end, &search->matches_mask)) {
363
+ search->has_matches = true;
364
+ search->chunk_base = search->ptr;
365
+ search->chunk_end = search->ptr + sizeof(uint8x16_t);
366
+ return neon_next_match(search);
367
+ }
368
+
369
+ // There are fewer than 16 bytes left.
370
+ unsigned long remaining = (search->end - search->ptr);
371
+ if (remaining >= SIMD_MINIMUM_THRESHOLD) {
372
+ char *s = copy_remaining_bytes(search, sizeof(uint8x16_t), remaining);
373
+
374
+ uint64_t mask = compute_chunk_mask_neon(s);
375
+
376
+ if (!mask) {
377
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
378
+ // search->cursor to search->ptr.
379
+ fbuffer_consumed(search->buffer, remaining);
380
+ search->ptr = search->end;
381
+ search->cursor = search->end;
382
+ return 0;
383
+ }
384
+
385
+ search->matches_mask = mask;
386
+ search->has_matches = true;
387
+ search->chunk_end = search->end;
388
+ search->chunk_base = search->ptr;
389
+ return neon_next_match(search);
390
+ }
391
+
392
+ if (search->ptr < search->end) {
393
+ return search_escape_basic(search);
394
+ }
395
+
396
+ search_flush(search);
397
+ return 0;
398
+ }
399
+ #endif /* HAVE_SIMD_NEON */
400
+
401
+ #ifdef HAVE_SIMD_SSE2
402
+
403
+ static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
404
+ {
405
+ int mask = search->matches_mask;
406
+ int index = trailing_zeros(mask);
407
+
408
+ // It is assumed escape_UTF8_char_basic will only ever increase search->ptr by at most one character.
409
+ // If we want to use a similar approach for full escaping we'll need to ensure:
410
+ // search->chunk_base + index >= search->ptr
411
+ // However, since we know escape_UTF8_char_basic only increases search->ptr by one, if the next match
412
+ // is one byte after the previous match then:
413
+ // search->chunk_base + index == search->ptr
414
+ search->ptr = search->chunk_base + index;
415
+ mask &= mask - 1;
416
+ search->matches_mask = mask;
417
+ search_flush(search);
418
+ return 1;
419
+ }
420
+
421
+ #if defined(__clang__) || defined(__GNUC__)
422
+ #define TARGET_SSE2 __attribute__((target("sse2")))
423
+ #else
424
+ #define TARGET_SSE2
425
+ #endif
426
+
427
+ static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(search_state *search)
428
+ {
429
+ if (RB_UNLIKELY(search->has_matches)) {
430
+ // There are more matches if search->matches_mask > 0.
431
+ if (search->matches_mask > 0) {
432
+ return sse2_next_match(search);
433
+ } else {
434
+ // sse2_next_match will only advance search->ptr up to the last matching character.
435
+ // Skip over any characters in the last chunk that occur after the last match.
436
+ search->has_matches = false;
437
+ if (RB_UNLIKELY(search->chunk_base + sizeof(__m128i) >= search->end)) {
438
+ search->ptr = search->end;
439
+ } else {
440
+ search->ptr = search->chunk_base + sizeof(__m128i);
441
+ }
442
+ }
443
+ }
444
+
445
+ if (string_scan_simd_sse2(&search->ptr, search->end, &search->matches_mask)) {
446
+ search->has_matches = true;
447
+ search->chunk_base = search->ptr;
448
+ search->chunk_end = search->ptr + sizeof(__m128i);
449
+ return sse2_next_match(search);
450
+ }
451
+
452
+ // There are fewer than 16 bytes left.
453
+ unsigned long remaining = (search->end - search->ptr);
454
+ if (remaining >= SIMD_MINIMUM_THRESHOLD) {
455
+ char *s = copy_remaining_bytes(search, sizeof(__m128i), remaining);
456
+
457
+ int needs_escape_mask = compute_chunk_mask_sse2(s);
458
+
459
+ if (needs_escape_mask == 0) {
460
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
461
+ // search->cursor to search->ptr.
462
+ fbuffer_consumed(search->buffer, remaining);
463
+ search->ptr = search->end;
464
+ search->cursor = search->end;
465
+ return 0;
466
+ }
467
+
468
+ search->has_matches = true;
469
+ search->matches_mask = needs_escape_mask;
470
+ search->chunk_base = search->ptr;
471
+ return sse2_next_match(search);
472
+ }
473
+
474
+ if (search->ptr < search->end) {
475
+ return search_escape_basic(search);
476
+ }
477
+
478
+ search_flush(search);
479
+ return 0;
480
+ }
481
+
482
+ #endif /* HAVE_SIMD_SSE2 */
483
+
484
+ #endif /* HAVE_SIMD */
485
+
229
486
  static const unsigned char script_safe_escape_table[256] = {
230
487
  // ASCII Control Characters
231
488
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
@@ -329,7 +586,8 @@ static inline unsigned char search_ascii_only_escape(search_state *search, const
329
586
  return 0;
330
587
  }
331
588
 
332
- static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_len) {
589
+ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_len)
590
+ {
333
591
  const unsigned char ch = (unsigned char)*search->ptr;
334
592
  switch (ch_len) {
335
593
  case 1: {
@@ -359,7 +617,7 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
359
617
 
360
618
  uint32_t wchar = 0;
361
619
 
362
- switch(ch_len) {
620
+ switch (ch_len) {
363
621
  case 2:
364
622
  wchar = ch & 0x1F;
365
623
  break;
@@ -519,7 +777,8 @@ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
519
777
  * _state_ is a JSON::State object, that can also be used to configure the
520
778
  * produced JSON string output further.
521
779
  */
522
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
780
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self)
781
+ {
523
782
  rb_check_arity(argc, 0, 1);
524
783
  VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
525
784
  return cState_partial_generate(Vstate, self, generate_json_array, Qfalse);
@@ -581,7 +840,8 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
581
840
  *
582
841
  * Extends _modul_ with the String::Extend module.
583
842
  */
584
- static VALUE mString_included_s(VALUE self, VALUE modul) {
843
+ static VALUE mString_included_s(VALUE self, VALUE modul)
844
+ {
585
845
  VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
586
846
  rb_call_super(1, &modul);
587
847
  return result;
@@ -788,6 +1048,21 @@ struct hash_foreach_arg {
788
1048
  int iter;
789
1049
  };
790
1050
 
1051
+ static VALUE
1052
+ convert_string_subclass(VALUE key)
1053
+ {
1054
+ VALUE key_to_s = rb_funcall(key, i_to_s, 0);
1055
+
1056
+ if (RB_UNLIKELY(!RB_TYPE_P(key_to_s, T_STRING))) {
1057
+ VALUE cname = rb_obj_class(key);
1058
+ rb_raise(rb_eTypeError,
1059
+ "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
1060
+ cname, "String", cname, "to_s", rb_obj_class(key_to_s));
1061
+ }
1062
+
1063
+ return key_to_s;
1064
+ }
1065
+
791
1066
  static int
792
1067
  json_object_i(VALUE key, VALUE val, VALUE _arg)
793
1068
  {
@@ -801,22 +1076,22 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
801
1076
  int j;
802
1077
 
803
1078
  if (arg->iter > 0) fbuffer_append_char(buffer, ',');
804
- if (RB_UNLIKELY(state->object_nl)) {
805
- fbuffer_append_str(buffer, state->object_nl);
1079
+ if (RB_UNLIKELY(data->state->object_nl)) {
1080
+ fbuffer_append_str(buffer, data->state->object_nl);
806
1081
  }
807
- if (RB_UNLIKELY(state->indent)) {
1082
+ if (RB_UNLIKELY(data->state->indent)) {
808
1083
  for (j = 0; j < depth; j++) {
809
- fbuffer_append_str(buffer, state->indent);
1084
+ fbuffer_append_str(buffer, data->state->indent);
810
1085
  }
811
1086
  }
812
1087
 
813
1088
  VALUE key_to_s;
814
- switch(rb_type(key)) {
1089
+ switch (rb_type(key)) {
815
1090
  case T_STRING:
816
1091
  if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
817
1092
  key_to_s = key;
818
1093
  } else {
819
- key_to_s = rb_funcall(key, i_to_s, 0);
1094
+ key_to_s = convert_string_subclass(key);
820
1095
  }
821
1096
  break;
822
1097
  case T_SYMBOL:
@@ -828,21 +1103,22 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
828
1103
  }
829
1104
 
830
1105
  if (RB_LIKELY(RBASIC_CLASS(key_to_s) == rb_cString)) {
831
- generate_json_string(buffer, data, state, key_to_s);
1106
+ generate_json_string(buffer, data, key_to_s);
832
1107
  } else {
833
- generate_json(buffer, data, state, key_to_s);
1108
+ generate_json(buffer, data, key_to_s);
834
1109
  }
835
- if (RB_UNLIKELY(state->space_before)) fbuffer_append_str(buffer, state->space_before);
1110
+ if (RB_UNLIKELY(state->space_before)) fbuffer_append_str(buffer, data->state->space_before);
836
1111
  fbuffer_append_char(buffer, ':');
837
- if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, state->space);
838
- generate_json(buffer, data, state, val);
1112
+ if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, data->state->space);
1113
+ generate_json(buffer, data, val);
839
1114
 
840
1115
  arg->iter++;
841
1116
  return ST_CONTINUE;
842
1117
  }
843
1118
 
844
- static inline long increase_depth(JSON_Generator_State *state)
1119
+ static inline long increase_depth(struct generate_json_data *data)
845
1120
  {
1121
+ JSON_Generator_State *state = data->state;
846
1122
  long depth = ++state->depth;
847
1123
  if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
848
1124
  rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
@@ -850,14 +1126,14 @@ static inline long increase_depth(JSON_Generator_State *state)
850
1126
  return depth;
851
1127
  }
852
1128
 
853
- static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1129
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
854
1130
  {
855
1131
  int j;
856
- long depth = increase_depth(state);
1132
+ long depth = increase_depth(data);
857
1133
 
858
1134
  if (RHASH_SIZE(obj) == 0) {
859
1135
  fbuffer_append(buffer, "{}", 2);
860
- --state->depth;
1136
+ --data->state->depth;
861
1137
  return;
862
1138
  }
863
1139
 
@@ -869,49 +1145,49 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
869
1145
  };
870
1146
  rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
871
1147
 
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)) {
1148
+ depth = --data->state->depth;
1149
+ if (RB_UNLIKELY(data->state->object_nl)) {
1150
+ fbuffer_append_str(buffer, data->state->object_nl);
1151
+ if (RB_UNLIKELY(data->state->indent)) {
876
1152
  for (j = 0; j < depth; j++) {
877
- fbuffer_append_str(buffer, state->indent);
1153
+ fbuffer_append_str(buffer, data->state->indent);
878
1154
  }
879
1155
  }
880
1156
  }
881
1157
  fbuffer_append_char(buffer, '}');
882
1158
  }
883
1159
 
884
- static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1160
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
885
1161
  {
886
1162
  int i, j;
887
- long depth = increase_depth(state);
1163
+ long depth = increase_depth(data);
888
1164
 
889
1165
  if (RARRAY_LEN(obj) == 0) {
890
1166
  fbuffer_append(buffer, "[]", 2);
891
- --state->depth;
1167
+ --data->state->depth;
892
1168
  return;
893
1169
  }
894
1170
 
895
1171
  fbuffer_append_char(buffer, '[');
896
- if (RB_UNLIKELY(state->array_nl)) fbuffer_append_str(buffer, state->array_nl);
897
- for(i = 0; i < RARRAY_LEN(obj); i++) {
1172
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1173
+ for (i = 0; i < RARRAY_LEN(obj); i++) {
898
1174
  if (i > 0) {
899
1175
  fbuffer_append_char(buffer, ',');
900
- if (RB_UNLIKELY(state->array_nl)) fbuffer_append_str(buffer, state->array_nl);
1176
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
901
1177
  }
902
- if (RB_UNLIKELY(state->indent)) {
1178
+ if (RB_UNLIKELY(data->state->indent)) {
903
1179
  for (j = 0; j < depth; j++) {
904
- fbuffer_append_str(buffer, state->indent);
1180
+ fbuffer_append_str(buffer, data->state->indent);
905
1181
  }
906
1182
  }
907
- generate_json(buffer, data, state, RARRAY_AREF(obj, i));
1183
+ generate_json(buffer, data, RARRAY_AREF(obj, i));
908
1184
  }
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)) {
1185
+ data->state->depth = --depth;
1186
+ if (RB_UNLIKELY(data->state->array_nl)) {
1187
+ fbuffer_append_str(buffer, data->state->array_nl);
1188
+ if (RB_UNLIKELY(data->state->indent)) {
913
1189
  for (j = 0; j < depth; j++) {
914
- fbuffer_append_str(buffer, state->indent);
1190
+ fbuffer_append_str(buffer, data->state->indent);
915
1191
  }
916
1192
  }
917
1193
  }
@@ -960,7 +1236,7 @@ static inline VALUE ensure_valid_encoding(VALUE str)
960
1236
  return str;
961
1237
  }
962
1238
 
963
- static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1239
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
964
1240
  {
965
1241
  obj = ensure_valid_encoding(obj);
966
1242
 
@@ -973,12 +1249,18 @@ static void generate_json_string(FBuffer *buffer, struct generate_json_data *dat
973
1249
  search.cursor = search.ptr;
974
1250
  search.end = search.ptr + len;
975
1251
 
976
- switch(rb_enc_str_coderange(obj)) {
1252
+ #ifdef HAVE_SIMD
1253
+ search.matches_mask = 0;
1254
+ search.has_matches = false;
1255
+ search.chunk_base = NULL;
1256
+ #endif /* HAVE_SIMD */
1257
+
1258
+ switch (rb_enc_str_coderange(obj)) {
977
1259
  case ENC_CODERANGE_7BIT:
978
1260
  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)) {
1261
+ if (RB_UNLIKELY(data->state->ascii_only)) {
1262
+ convert_UTF8_to_ASCII_only_JSON(&search, data->state->script_safe ? script_safe_escape_table : ascii_only_escape_table);
1263
+ } else if (RB_UNLIKELY(data->state->script_safe)) {
982
1264
  convert_UTF8_to_script_safe_JSON(&search);
983
1265
  } else {
984
1266
  convert_UTF8_to_JSON(&search);
@@ -991,7 +1273,7 @@ static void generate_json_string(FBuffer *buffer, struct generate_json_data *dat
991
1273
  fbuffer_append_char(buffer, '"');
992
1274
  }
993
1275
 
994
- static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1276
+ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
995
1277
  {
996
1278
  VALUE tmp;
997
1279
  if (rb_respond_to(obj, i_to_json)) {
@@ -1001,100 +1283,116 @@ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *d
1001
1283
  } else {
1002
1284
  tmp = rb_funcall(obj, i_to_s, 0);
1003
1285
  Check_Type(tmp, T_STRING);
1004
- generate_json_string(buffer, data, state, tmp);
1286
+ generate_json_string(buffer, data, tmp);
1005
1287
  }
1006
1288
  }
1007
1289
 
1008
- static inline void generate_json_symbol(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1290
+ static inline void generate_json_symbol(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1009
1291
  {
1010
- if (state->strict) {
1011
- generate_json_string(buffer, data, state, rb_sym2str(obj));
1292
+ if (data->state->strict) {
1293
+ generate_json_string(buffer, data, rb_sym2str(obj));
1012
1294
  } else {
1013
- generate_json_fallback(buffer, data, state, obj);
1295
+ generate_json_fallback(buffer, data, obj);
1014
1296
  }
1015
1297
  }
1016
1298
 
1017
- static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1299
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1018
1300
  {
1019
1301
  fbuffer_append(buffer, "null", 4);
1020
1302
  }
1021
1303
 
1022
- static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1304
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1023
1305
  {
1024
1306
  fbuffer_append(buffer, "false", 5);
1025
1307
  }
1026
1308
 
1027
- static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1309
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1028
1310
  {
1029
1311
  fbuffer_append(buffer, "true", 4);
1030
1312
  }
1031
1313
 
1032
- static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1314
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1033
1315
  {
1034
1316
  fbuffer_append_long(buffer, FIX2LONG(obj));
1035
1317
  }
1036
1318
 
1037
- static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1319
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1038
1320
  {
1039
1321
  VALUE tmp = rb_funcall(obj, i_to_s, 0);
1040
1322
  fbuffer_append_str(buffer, tmp);
1041
1323
  }
1042
1324
 
1043
1325
  #ifdef RUBY_INTEGER_UNIFICATION
1044
- static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1326
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1045
1327
  {
1046
1328
  if (FIXNUM_P(obj))
1047
- generate_json_fixnum(buffer, data, state, obj);
1329
+ generate_json_fixnum(buffer, data, obj);
1048
1330
  else
1049
- generate_json_bignum(buffer, data, state, obj);
1331
+ generate_json_bignum(buffer, data, obj);
1050
1332
  }
1051
1333
  #endif
1052
1334
 
1053
- static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1335
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1054
1336
  {
1055
1337
  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);
1338
+ char allow_nan = data->state->allow_nan;
1339
+ if (isinf(value) || isnan(value)) {
1340
+ /* for NaN and Infinity values we either raise an error or rely on Float#to_s. */
1341
+ if (!allow_nan) {
1342
+ if (data->state->strict && data->state->as_json) {
1343
+ VALUE casted_obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1061
1344
  if (casted_obj != obj) {
1062
- increase_depth(state);
1063
- generate_json(buffer, data, state, casted_obj);
1064
- state->depth--;
1345
+ increase_depth(data);
1346
+ generate_json(buffer, data, casted_obj);
1347
+ data->state->depth--;
1065
1348
  return;
1066
1349
  }
1067
1350
  }
1068
1351
  raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
1069
1352
  }
1353
+
1354
+ VALUE tmp = rb_funcall(obj, i_to_s, 0);
1355
+ fbuffer_append_str(buffer, tmp);
1356
+ return;
1070
1357
  }
1071
- fbuffer_append_str(buffer, rb_funcall(obj, i_to_s, 0));
1358
+
1359
+ /* This implementation writes directly into the buffer. We reserve
1360
+ * the 28 characters that fpconv_dtoa states as its maximum.
1361
+ */
1362
+ fbuffer_inc_capa(buffer, 28);
1363
+ char* d = buffer->ptr + buffer->len;
1364
+ int len = fpconv_dtoa(value, d);
1365
+
1366
+ /* fpconv_dtoa converts a float to its shortest string representation,
1367
+ * but it adds a ".0" if this is a plain integer.
1368
+ */
1369
+ fbuffer_consumed(buffer, len);
1072
1370
  }
1073
1371
 
1074
- static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1372
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1075
1373
  {
1076
1374
  VALUE fragment = RSTRUCT_GET(obj, 0);
1077
1375
  Check_Type(fragment, T_STRING);
1078
1376
  fbuffer_append_str(buffer, fragment);
1079
1377
  }
1080
1378
 
1081
- static void generate_json(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
1379
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1082
1380
  {
1083
1381
  bool as_json_called = false;
1084
1382
  start:
1085
1383
  if (obj == Qnil) {
1086
- generate_json_null(buffer, data, state, obj);
1384
+ generate_json_null(buffer, data, obj);
1087
1385
  } else if (obj == Qfalse) {
1088
- generate_json_false(buffer, data, state, obj);
1386
+ generate_json_false(buffer, data, obj);
1089
1387
  } else if (obj == Qtrue) {
1090
- generate_json_true(buffer, data, state, obj);
1388
+ generate_json_true(buffer, data, obj);
1091
1389
  } else if (RB_SPECIAL_CONST_P(obj)) {
1092
1390
  if (RB_FIXNUM_P(obj)) {
1093
- generate_json_fixnum(buffer, data, state, obj);
1391
+ generate_json_fixnum(buffer, data, obj);
1094
1392
  } else if (RB_FLONUM_P(obj)) {
1095
- generate_json_float(buffer, data, state, obj);
1393
+ generate_json_float(buffer, data, obj);
1096
1394
  } else if (RB_STATIC_SYM_P(obj)) {
1097
- generate_json_symbol(buffer, data, state, obj);
1395
+ generate_json_symbol(buffer, data, obj);
1098
1396
  } else {
1099
1397
  goto general;
1100
1398
  }
@@ -1102,43 +1400,43 @@ start:
1102
1400
  VALUE klass = RBASIC_CLASS(obj);
1103
1401
  switch (RB_BUILTIN_TYPE(obj)) {
1104
1402
  case T_BIGNUM:
1105
- generate_json_bignum(buffer, data, state, obj);
1403
+ generate_json_bignum(buffer, data, obj);
1106
1404
  break;
1107
1405
  case T_HASH:
1108
1406
  if (klass != rb_cHash) goto general;
1109
- generate_json_object(buffer, data, state, obj);
1407
+ generate_json_object(buffer, data, obj);
1110
1408
  break;
1111
1409
  case T_ARRAY:
1112
1410
  if (klass != rb_cArray) goto general;
1113
- generate_json_array(buffer, data, state, obj);
1411
+ generate_json_array(buffer, data, obj);
1114
1412
  break;
1115
1413
  case T_STRING:
1116
1414
  if (klass != rb_cString) goto general;
1117
- generate_json_string(buffer, data, state, obj);
1415
+ generate_json_string(buffer, data, obj);
1118
1416
  break;
1119
1417
  case T_SYMBOL:
1120
- generate_json_symbol(buffer, data, state, obj);
1418
+ generate_json_symbol(buffer, data, obj);
1121
1419
  break;
1122
1420
  case T_FLOAT:
1123
1421
  if (klass != rb_cFloat) goto general;
1124
- generate_json_float(buffer, data, state, obj);
1422
+ generate_json_float(buffer, data, obj);
1125
1423
  break;
1126
1424
  case T_STRUCT:
1127
1425
  if (klass != cFragment) goto general;
1128
- generate_json_fragment(buffer, data, state, obj);
1426
+ generate_json_fragment(buffer, data, obj);
1129
1427
  break;
1130
1428
  default:
1131
1429
  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);
1430
+ if (data->state->strict) {
1431
+ if (RTEST(data->state->as_json) && !as_json_called) {
1432
+ obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1135
1433
  as_json_called = true;
1136
1434
  goto start;
1137
1435
  } else {
1138
1436
  raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
1139
1437
  }
1140
1438
  } else {
1141
- generate_json_fallback(buffer, data, state, obj);
1439
+ generate_json_fallback(buffer, data, obj);
1142
1440
  }
1143
1441
  }
1144
1442
  }
@@ -1148,7 +1446,7 @@ static VALUE generate_json_try(VALUE d)
1148
1446
  {
1149
1447
  struct generate_json_data *data = (struct generate_json_data *)d;
1150
1448
 
1151
- data->func(data->buffer, data, data->state, data->obj);
1449
+ data->func(data->buffer, data, data->obj);
1152
1450
 
1153
1451
  return Qnil;
1154
1452
  }
@@ -1609,15 +1907,30 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
1609
1907
  return Qnil;
1610
1908
  }
1611
1909
 
1910
+ struct configure_state_data {
1911
+ JSON_Generator_State *state;
1912
+ VALUE vstate; // Ruby object that owns the state, or Qfalse if stack-allocated
1913
+ };
1914
+
1915
+ static inline void state_write_value(struct configure_state_data *data, VALUE *field, VALUE value)
1916
+ {
1917
+ if (RTEST(data->vstate)) {
1918
+ RB_OBJ_WRITE(data->vstate, field, value);
1919
+ } else {
1920
+ *field = value;
1921
+ }
1922
+ }
1923
+
1612
1924
  static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1613
1925
  {
1614
- JSON_Generator_State *state = (JSON_Generator_State *)_arg;
1926
+ struct configure_state_data *data = (struct configure_state_data *)_arg;
1927
+ JSON_Generator_State *state = data->state;
1615
1928
 
1616
- if (key == sym_indent) { state->indent = string_config(val); }
1617
- else if (key == sym_space) { state->space = string_config(val); }
1618
- else if (key == sym_space_before) { state->space_before = string_config(val); }
1619
- else if (key == sym_object_nl) { state->object_nl = string_config(val); }
1620
- else if (key == sym_array_nl) { state->array_nl = string_config(val); }
1929
+ if (key == sym_indent) { state_write_value(data, &state->indent, string_config(val)); }
1930
+ else if (key == sym_space) { state_write_value(data, &state->space, string_config(val)); }
1931
+ else if (key == sym_space_before) { state_write_value(data, &state->space_before, string_config(val)); }
1932
+ else if (key == sym_object_nl) { state_write_value(data, &state->object_nl, string_config(val)); }
1933
+ else if (key == sym_array_nl) { state_write_value(data, &state->array_nl, string_config(val)); }
1621
1934
  else if (key == sym_max_nesting) { state->max_nesting = long_config(val); }
1622
1935
  else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); }
1623
1936
  else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); }
@@ -1626,11 +1939,14 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1626
1939
  else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
1627
1940
  else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
1628
1941
  else if (key == sym_strict) { state->strict = RTEST(val); }
1629
- else if (key == sym_as_json) { state->as_json = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse; }
1942
+ else if (key == sym_as_json) {
1943
+ VALUE proc = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse;
1944
+ state_write_value(data, &state->as_json, proc);
1945
+ }
1630
1946
  return ST_CONTINUE;
1631
1947
  }
1632
1948
 
1633
- static void configure_state(JSON_Generator_State *state, VALUE config)
1949
+ static void configure_state(JSON_Generator_State *state, VALUE vstate, VALUE config)
1634
1950
  {
1635
1951
  if (!RTEST(config)) return;
1636
1952
 
@@ -1638,15 +1954,20 @@ static void configure_state(JSON_Generator_State *state, VALUE config)
1638
1954
 
1639
1955
  if (!RHASH_SIZE(config)) return;
1640
1956
 
1957
+ struct configure_state_data data = {
1958
+ .state = state,
1959
+ .vstate = vstate
1960
+ };
1961
+
1641
1962
  // We assume in most cases few keys are set so it's faster to go over
1642
1963
  // the provided keys than to check all possible keys.
1643
- rb_hash_foreach(config, configure_state_i, (VALUE)state);
1964
+ rb_hash_foreach(config, configure_state_i, (VALUE)&data);
1644
1965
  }
1645
1966
 
1646
1967
  static VALUE cState_configure(VALUE self, VALUE opts)
1647
1968
  {
1648
1969
  GET_STATE(self);
1649
- configure_state(state, opts);
1970
+ configure_state(state, self, opts);
1650
1971
  return self;
1651
1972
  }
1652
1973
 
@@ -1654,7 +1975,7 @@ static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
1654
1975
  {
1655
1976
  JSON_Generator_State state = {0};
1656
1977
  state_init(&state);
1657
- configure_state(&state, opts);
1978
+ configure_state(&state, Qfalse, opts);
1658
1979
 
1659
1980
  char stack_buffer[FBUFFER_STACK_SIZE];
1660
1981
  FBuffer buffer = {
@@ -1819,4 +2140,23 @@ void Init_generator(void)
1819
2140
  binary_encindex = rb_ascii8bit_encindex();
1820
2141
 
1821
2142
  rb_require("json/ext/generator/state");
2143
+
2144
+
2145
+ switch (find_simd_implementation()) {
2146
+ #ifdef HAVE_SIMD
2147
+ #ifdef HAVE_SIMD_NEON
2148
+ case SIMD_NEON:
2149
+ search_escape_basic_impl = search_escape_basic_neon;
2150
+ break;
2151
+ #endif /* HAVE_SIMD_NEON */
2152
+ #ifdef HAVE_SIMD_SSE2
2153
+ case SIMD_SSE2:
2154
+ search_escape_basic_impl = search_escape_basic_sse2;
2155
+ break;
2156
+ #endif /* HAVE_SIMD_SSE2 */
2157
+ #endif /* HAVE_SIMD */
2158
+ default:
2159
+ search_escape_basic_impl = search_escape_basic;
2160
+ break;
2161
+ }
1822
2162
  }