json 2.11.3 → 2.13.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1919e2040a180b81eba1f475c511ace075b32015997b7d58098f93103941f8b2
4
- data.tar.gz: d958784bea1136d935835d3e602fae96f97d25208cafec8a68db03619e6d34d0
3
+ metadata.gz: 41dcbe399cb5dd00e62d93c87f31b356674c1feb9430306902009ebf8f56bd9a
4
+ data.tar.gz: f398e819143dc90c162474b5c97241d7a8c8c8209d5a5512c1b081d72a29192a
5
5
  SHA512:
6
- metadata.gz: 742da3e909b2b6d8c1c9de5833b11be0f80e3b50f5296973b57f03cd45ae584162ac33bcbeb5b99fa767714b8531fef71b6d7ff559da40c3b04e75026ba3158f
7
- data.tar.gz: e55ae407cc5b0da66922a41119b000da925391b58ea9da154a058b15f034334fae9e9c813cda12f4db75936c50e281df00acf8a4053808923d89d5efaa6927af
6
+ metadata.gz: ff828416dfb1f4a6ffcb51e02827d948b6c566ce008799cc9a02afb538b8d9a32ff1d6eee5c4e0e609757d1e9cfe1332ea359049a3fd820b199bdc7931a2573e
7
+ data.tar.gz: e5ea4c5bd447d2ed5c37b144219cd0cbed1474a5c7976f63b938996687cfc7f422e4e147582e6fee0a2d3f4ba09937496c23f08c42e2a525744a9246bcfcb5df
data/CHANGES.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # Changes
2
2
 
3
+ ### Unreleased
4
+
5
+ ### 2025-05-23 (2.13.0)
6
+
7
+ * Add new `allow_duplicate_key` parsing options. By default a warning is now emitted when a duplicated key is encountered.
8
+ In `json 3.0` an error will be raised.
9
+ * Optimize parsing further using SIMD to scan strings.
10
+
11
+ ### 2025-05-23 (2.12.2)
12
+
13
+ * Fix compiler optimization level.
14
+
15
+ ### 2025-05-23 (2.12.1)
16
+
17
+ * Fix a potential crash in large negative floating point number generation.
18
+ * Fix for JSON.pretty_generate to use passed state object's generate instead of state class as the required parameters aren't available.
19
+
20
+ ### 2025-05-12 (2.12.0)
21
+
22
+ * Improve floating point generation to not use scientific notation as much.
23
+ * Include line and column in parser errors. Both in the message and as exception attributes.
24
+ * Handle non-string hash keys with broken `to_s` implementations.
25
+ * `JSON.generate` now uses SSE2 (x86) or NEON (arm64) instructions when available to escape strings.
26
+
3
27
  ### 2025-04-25 (2.11.3)
4
28
 
5
29
  * Fix a regression in `JSON.pretty_generate` that could cause indentation to be off once some `#to_json` has been called.
data/README.md CHANGED
@@ -233,6 +233,19 @@ the `pp` library's `pp` methods.
233
233
 
234
234
  ## Development
235
235
 
236
+ ### Prerequisites
237
+
238
+ 1. Clone the repository
239
+ 2. Install dependencies with `bundle install`
240
+
241
+ ### Testing
242
+
243
+ The full test suite can be run with:
244
+
245
+ ```bash
246
+ bundle exec rake test
247
+ ```
248
+
236
249
  ### Release
237
250
 
238
251
  Update the `lib/json/version.rb` file.
@@ -36,6 +36,12 @@ typedef unsigned char _Bool;
36
36
  # define MAYBE_UNUSED(x) x
37
37
  #endif
38
38
 
39
+ #ifdef RUBY_DEBUG
40
+ #ifndef JSON_DEBUG
41
+ #define JSON_DEBUG RUBY_DEBUG
42
+ #endif
43
+ #endif
44
+
39
45
  enum fbuffer_type {
40
46
  FBUFFER_HEAP_ALLOCATED = 0,
41
47
  FBUFFER_STACK_ALLOCATED = 1,
@@ -46,6 +52,9 @@ typedef struct FBufferStruct {
46
52
  unsigned long initial_length;
47
53
  unsigned long len;
48
54
  unsigned long capa;
55
+ #ifdef JSON_DEBUG
56
+ unsigned long requested;
57
+ #endif
49
58
  char *ptr;
50
59
  VALUE io;
51
60
  } FBuffer;
@@ -74,6 +83,20 @@ static void fbuffer_stack_init(FBuffer *fb, unsigned long initial_length, char *
74
83
  fb->ptr = stack_buffer;
75
84
  fb->capa = stack_buffer_size;
76
85
  }
86
+ #ifdef JSON_DEBUG
87
+ fb->requested = 0;
88
+ #endif
89
+ }
90
+
91
+ static inline void fbuffer_consumed(FBuffer *fb, unsigned long consumed)
92
+ {
93
+ #ifdef JSON_DEBUG
94
+ if (consumed > fb->requested) {
95
+ rb_bug("fbuffer: Out of bound write");
96
+ }
97
+ fb->requested = 0;
98
+ #endif
99
+ fb->len += consumed;
77
100
  }
78
101
 
79
102
  static void fbuffer_free(FBuffer *fb)
@@ -137,6 +160,10 @@ static void fbuffer_do_inc_capa(FBuffer *fb, unsigned long requested)
137
160
 
138
161
  static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
139
162
  {
163
+ #ifdef JSON_DEBUG
164
+ fb->requested = requested;
165
+ #endif
166
+
140
167
  if (RB_UNLIKELY(requested > fb->capa - fb->len)) {
141
168
  fbuffer_do_inc_capa(fb, requested);
142
169
  }
@@ -147,15 +174,22 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
147
174
  if (len > 0) {
148
175
  fbuffer_inc_capa(fb, len);
149
176
  MEMCPY(fb->ptr + fb->len, newstr, char, len);
150
- fb->len += len;
177
+ fbuffer_consumed(fb, len);
151
178
  }
152
179
  }
153
180
 
154
181
  /* Appends a character into a buffer. The buffer needs to have sufficient capacity, via fbuffer_inc_capa(...). */
155
182
  static inline void fbuffer_append_reserved_char(FBuffer *fb, char chr)
156
183
  {
184
+ #ifdef JSON_DEBUG
185
+ if (fb->requested < 1) {
186
+ rb_bug("fbuffer: unreserved write");
187
+ }
188
+ fb->requested--;
189
+ #endif
190
+
157
191
  fb->ptr[fb->len] = chr;
158
- fb->len += 1;
192
+ fb->len++;
159
193
  }
160
194
 
161
195
  static void fbuffer_append_str(FBuffer *fb, VALUE str)
@@ -172,7 +206,7 @@ static inline void fbuffer_append_char(FBuffer *fb, char newchr)
172
206
  {
173
207
  fbuffer_inc_capa(fb, 1);
174
208
  *(fb->ptr + fb->len) = newchr;
175
- fb->len++;
209
+ fbuffer_consumed(fb, 1);
176
210
  }
177
211
 
178
212
  static inline char *fbuffer_cursor(FBuffer *fb)
@@ -182,7 +216,7 @@ static inline char *fbuffer_cursor(FBuffer *fb)
182
216
 
183
217
  static inline void fbuffer_advance_to(FBuffer *fb, char *end)
184
218
  {
185
- fb->len = end - fb->ptr;
219
+ fbuffer_consumed(fb, (end - fb->ptr) - fb->len);
186
220
  }
187
221
 
188
222
  /*
@@ -6,5 +6,11 @@ if RUBY_ENGINE == 'truffleruby'
6
6
  else
7
7
  append_cflags("-std=c99")
8
8
  $defs << "-DJSON_GENERATOR"
9
+ $defs << "-DJSON_DEBUG" if ENV["JSON_DEBUG"]
10
+
11
+ if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
12
+ load __dir__ + "/../simd/conf.rb"
13
+ end
14
+
9
15
  create_makefile 'json/ext/generator'
10
16
  end
@@ -5,6 +5,8 @@
5
5
  #include <math.h>
6
6
  #include <ctype.h>
7
7
 
8
+ #include "../simd/simd.h"
9
+
8
10
  /* ruby api and some helpers */
9
11
 
10
12
  typedef struct JSON_Generator_StateStruct {
@@ -109,12 +111,40 @@ typedef struct _search_state {
109
111
  const char *end;
110
112
  const char *cursor;
111
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 */
112
128
  } search_state;
113
129
 
114
- static inline void search_flush(search_state *search)
115
- {
116
- fbuffer_append(search->buffer, search->cursor, search->ptr - search->cursor);
117
- 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
+ }
118
148
  }
119
149
 
120
150
  static const unsigned char escape_table_basic[256] = {
@@ -130,6 +160,8 @@ static const unsigned char escape_table_basic[256] = {
130
160
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131
161
  };
132
162
 
163
+ static unsigned char (*search_escape_basic_impl)(search_state *);
164
+
133
165
  static inline unsigned char search_escape_basic(search_state *search)
134
166
  {
135
167
  while (search->ptr < search->end) {
@@ -144,7 +176,8 @@ static inline unsigned char search_escape_basic(search_state *search)
144
176
  return 0;
145
177
  }
146
178
 
147
- static inline void escape_UTF8_char_basic(search_state *search) {
179
+ static inline FORCE_INLINE void escape_UTF8_char_basic(search_state *search)
180
+ {
148
181
  const unsigned char ch = (unsigned char)*search->ptr;
149
182
  switch (ch) {
150
183
  case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
@@ -186,12 +219,13 @@ static inline void escape_UTF8_char_basic(search_state *search) {
186
219
  */
187
220
  static inline void convert_UTF8_to_JSON(search_state *search)
188
221
  {
189
- while (search_escape_basic(search)) {
222
+ while (search_escape_basic_impl(search)) {
190
223
  escape_UTF8_char_basic(search);
191
224
  }
192
225
  }
193
226
 
194
- 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
+ {
195
229
  const unsigned char ch = (unsigned char)*search->ptr;
196
230
  switch (ch_len) {
197
231
  case 1: {
@@ -227,6 +261,228 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
227
261
  search->cursor = (search->ptr += ch_len);
228
262
  }
229
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
+
230
486
  static const unsigned char script_safe_escape_table[256] = {
231
487
  // ASCII Control Characters
232
488
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
@@ -330,7 +586,8 @@ static inline unsigned char search_ascii_only_escape(search_state *search, const
330
586
  return 0;
331
587
  }
332
588
 
333
- 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
+ {
334
591
  const unsigned char ch = (unsigned char)*search->ptr;
335
592
  switch (ch_len) {
336
593
  case 1: {
@@ -360,7 +617,7 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
360
617
 
361
618
  uint32_t wchar = 0;
362
619
 
363
- switch(ch_len) {
620
+ switch (ch_len) {
364
621
  case 2:
365
622
  wchar = ch & 0x1F;
366
623
  break;
@@ -520,7 +777,8 @@ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
520
777
  * _state_ is a JSON::State object, that can also be used to configure the
521
778
  * produced JSON string output further.
522
779
  */
523
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
780
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self)
781
+ {
524
782
  rb_check_arity(argc, 0, 1);
525
783
  VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
526
784
  return cState_partial_generate(Vstate, self, generate_json_array, Qfalse);
@@ -582,7 +840,8 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
582
840
  *
583
841
  * Extends _modul_ with the String::Extend module.
584
842
  */
585
- static VALUE mString_included_s(VALUE self, VALUE modul) {
843
+ static VALUE mString_included_s(VALUE self, VALUE modul)
844
+ {
586
845
  VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
587
846
  rb_call_super(1, &modul);
588
847
  return result;
@@ -789,6 +1048,21 @@ struct hash_foreach_arg {
789
1048
  int iter;
790
1049
  };
791
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
+
792
1066
  static int
793
1067
  json_object_i(VALUE key, VALUE val, VALUE _arg)
794
1068
  {
@@ -812,12 +1086,12 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
812
1086
  }
813
1087
 
814
1088
  VALUE key_to_s;
815
- switch(rb_type(key)) {
1089
+ switch (rb_type(key)) {
816
1090
  case T_STRING:
817
1091
  if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
818
1092
  key_to_s = key;
819
1093
  } else {
820
- key_to_s = rb_funcall(key, i_to_s, 0);
1094
+ key_to_s = convert_string_subclass(key);
821
1095
  }
822
1096
  break;
823
1097
  case T_SYMBOL:
@@ -896,7 +1170,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
896
1170
 
897
1171
  fbuffer_append_char(buffer, '[');
898
1172
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
899
- for(i = 0; i < RARRAY_LEN(obj); i++) {
1173
+ for (i = 0; i < RARRAY_LEN(obj); i++) {
900
1174
  if (i > 0) {
901
1175
  fbuffer_append_char(buffer, ',');
902
1176
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
@@ -975,7 +1249,13 @@ static void generate_json_string(FBuffer *buffer, struct generate_json_data *dat
975
1249
  search.cursor = search.ptr;
976
1250
  search.end = search.ptr + len;
977
1251
 
978
- 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)) {
979
1259
  case ENC_CODERANGE_7BIT:
980
1260
  case ENC_CODERANGE_VALID:
981
1261
  if (RB_UNLIKELY(data->state->ascii_only)) {
@@ -1077,17 +1357,16 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
1077
1357
  }
1078
1358
 
1079
1359
  /* This implementation writes directly into the buffer. We reserve
1080
- * the 24 characters that fpconv_dtoa states as its maximum, plus
1081
- * 2 more characters for the potential ".0" suffix.
1360
+ * the 28 characters that fpconv_dtoa states as its maximum.
1082
1361
  */
1083
- fbuffer_inc_capa(buffer, 26);
1362
+ fbuffer_inc_capa(buffer, 28);
1084
1363
  char* d = buffer->ptr + buffer->len;
1085
1364
  int len = fpconv_dtoa(value, d);
1086
1365
 
1087
1366
  /* fpconv_dtoa converts a float to its shortest string representation,
1088
1367
  * but it adds a ".0" if this is a plain integer.
1089
1368
  */
1090
- buffer->len += len;
1369
+ fbuffer_consumed(buffer, len);
1091
1370
  }
1092
1371
 
1093
1372
  static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
@@ -1838,4 +2117,23 @@ void Init_generator(void)
1838
2117
  binary_encindex = rb_ascii8bit_encindex();
1839
2118
 
1840
2119
  rb_require("json/ext/generator/state");
2120
+
2121
+
2122
+ switch (find_simd_implementation()) {
2123
+ #ifdef HAVE_SIMD
2124
+ #ifdef HAVE_SIMD_NEON
2125
+ case SIMD_NEON:
2126
+ search_escape_basic_impl = search_escape_basic_neon;
2127
+ break;
2128
+ #endif /* HAVE_SIMD_NEON */
2129
+ #ifdef HAVE_SIMD_SSE2
2130
+ case SIMD_SSE2:
2131
+ search_escape_basic_impl = search_escape_basic_sse2;
2132
+ break;
2133
+ #endif /* HAVE_SIMD_SSE2 */
2134
+ #endif /* HAVE_SIMD */
2135
+ default:
2136
+ search_escape_basic_impl = search_escape_basic;
2137
+ break;
2138
+ }
1841
2139
  }
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
  require 'mkmf'
3
3
 
4
- have_func("rb_enc_interned_str", "ruby.h") # RUBY_VERSION >= 3.0
4
+ have_func("rb_enc_interned_str", "ruby/encoding.h") # RUBY_VERSION >= 3.0
5
5
  have_func("rb_hash_new_capa", "ruby.h") # RUBY_VERSION >= 3.2
6
6
  have_func("rb_hash_bulk_insert", "ruby.h") # Missing on TruffleRuby
7
7
  have_func("strnlen", "string.h") # Missing on Solaris 10
8
8
 
9
9
  append_cflags("-std=c99")
10
10
 
11
+ if enable_config('parser-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
12
+ load __dir__ + "/../simd/conf.rb"
13
+ end
14
+
11
15
  create_makefile 'json/ext/parser'