json 2.12.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e71f977a9d4c1316007814d62236fd185f5aaade7a79f3e5d48a9ffde32f520
4
- data.tar.gz: f1be8ac3136a6dcf48aa15c7ec08fa4dfcedb6f89b1b6ad8944727708a16e074
3
+ metadata.gz: 71499860706a6f27871853ec88fe26d5bd6a53f85fba2e9764b9ef0aadc170d9
4
+ data.tar.gz: c07cd26190c4f36864490465890c162e1b9ce0ae1f262069e51c94d7cf74b117
5
5
  SHA512:
6
- metadata.gz: 23f2d490dfb7ea60b189f8227787fde0c53844f62c8e9023ba1d413a72b46b7a3b77836d1a6050dd0a2fa925370bd260da0a52d738bd1231c81ad1ef4a17adda
7
- data.tar.gz: 22326ad3f75f99e20c7f1ad3cc0f519ffc56b7c85c94aa124a2ea47c8d0c86f604307fe504f216b347651d3c82df83623798dbdbabc45be78a1e4721cc7b8cbe
6
+ metadata.gz: f1c626d30c67e99c56d9f411b7944b6263261676567c02f6d57ba7566087743b26835d3c6c4c1636a0dc76cc2b8c4e2739a1130f286cd069406c38c66138df97
7
+ data.tar.gz: eb4fd62079fc730962359e4128f7abd36ca04c17b580506c3e8e3ebdd0e8e0c54c3cf75d1ae899dc53e13697bfa0c11c6c04277d701cfc83fa9ee611785ac85e
data/CHANGES.md CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### 2025-07-28 (2.13.2)
6
+
7
+ * Improve duplicate key warning and errors to include the key name and point to the right caller.
8
+
9
+ ### 2025-07-24 (2.13.1)
10
+
11
+ * Fix support for older compilers without `__builtin_cpu_supports`.
12
+
13
+ ### 2025-07-17 (2.13.0)
14
+
15
+ * Add new `allow_duplicate_key` parsing options. By default a warning is now emitted when a duplicated key is encountered.
16
+ In `json 3.0` an error will be raised.
17
+ * Optimize parsing further using SIMD to scan strings.
18
+
19
+ ### 2025-05-23 (2.12.2)
20
+
21
+ * Fix compiler optimization level.
22
+
23
+ ### 2025-05-23 (2.12.1)
24
+
25
+ * Fix a potential crash in large negative floating point number generation.
26
+ * Fix for JSON.pretty_generate to use passed state object's generate instead of state class as the required parameters aren't available.
27
+
5
28
  ### 2025-05-12 (2.12.0)
6
29
 
7
30
  * Improve floating point generation to not use scientific notation as much.
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,33 +6,10 @@ 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"]
9
10
 
10
11
  if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
11
- if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
12
- # Try to compile a small program using NEON instructions
13
- if have_header('arm_neon.h')
14
- have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
15
- #include <arm_neon.h>
16
- int main() {
17
- uint8x16_t test = vdupq_n_u8(32);
18
- return 0;
19
- }
20
- SRC
21
- $defs.push("-DJSON_ENABLE_SIMD")
22
- end
23
- end
24
-
25
- if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC')
26
- #include <x86intrin.h>
27
- int main() {
28
- __m128i test = _mm_set1_epi8(32);
29
- return 0;
30
- }
31
- SRC
32
- $defs.push("-DJSON_ENABLE_SIMD")
33
- end
34
-
35
- have_header('cpuid.h')
12
+ load __dir__ + "/../simd/conf.rb"
36
13
  end
37
14
 
38
15
  create_makefile 'json/ext/generator'
@@ -5,7 +5,7 @@
5
5
  #include <math.h>
6
6
  #include <ctype.h>
7
7
 
8
- #include "simd.h"
8
+ #include "../simd/simd.h"
9
9
 
10
10
  /* ruby api and some helpers */
11
11
 
@@ -304,28 +304,6 @@ static inline FORCE_INLINE unsigned char neon_next_match(search_state *search)
304
304
  return 1;
305
305
  }
306
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
307
  static inline unsigned char search_escape_basic_neon(search_state *search)
330
308
  {
331
309
  if (RB_UNLIKELY(search->has_matches)) {
@@ -333,7 +311,7 @@ static inline unsigned char search_escape_basic_neon(search_state *search)
333
311
  if (search->matches_mask > 0) {
334
312
  return neon_next_match(search);
335
313
  } else {
336
- // neon_next_match will only advance search->ptr up to the last matching character.
314
+ // neon_next_match will only advance search->ptr up to the last matching character.
337
315
  // Skip over any characters in the last chunk that occur after the last match.
338
316
  search->has_matches = false;
339
317
  search->ptr = search->chunk_end;
@@ -342,69 +320,63 @@ static inline unsigned char search_escape_basic_neon(search_state *search)
342
320
 
343
321
  /*
344
322
  * The code below implements an SIMD-based algorithm to determine if N bytes at a time
345
- * need to be escaped.
346
- *
323
+ * need to be escaped.
324
+ *
347
325
  * Assume the ptr = "Te\sting!" (the double quotes are included in the string)
348
- *
326
+ *
349
327
  * The explanation will be limited to the first 8 bytes of the string for simplicity. However
350
328
  * the vector insructions may work on larger vectors.
351
- *
329
+ *
352
330
  * 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:
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:
359
337
  * [22 54 65 5C 73 74 69 6E] (" T e \ s t i n)
360
- *
338
+ *
361
339
  * First we check if any byte in chunk is less than 32 (0x20). This returns the following vector
362
340
  * as no bytes are less than 32 (0x20):
363
341
  * [0 0 0 0 0 0 0 0]
364
- *
342
+ *
365
343
  * Next, we check if any byte in chunk is equal to a backslash:
366
344
  * [0 0 0 FF 0 0 0 0]
367
- *
345
+ *
368
346
  * Finally we check if any byte in chunk is equal to a double quote:
369
- * [FF 0 0 0 0 0 0 0]
370
- *
347
+ * [FF 0 0 0 0 0 0 0]
348
+ *
371
349
  * Now we have three vectors where each byte indicates if the corresponding byte in chunk
372
350
  * needs to be escaped. We combine these vectors with a series of logical OR instructions.
373
351
  * This is the needs_escape vector and it is equal to:
374
- * [FF 0 0 FF 0 0 0 0]
375
- *
352
+ * [FF 0 0 FF 0 0 0 0]
353
+ *
376
354
  * Next we compute the bitwise AND between each byte and 0x1 and compute the horizontal sum of
377
355
  * the values in the vector. This computes how many bytes need to be escaped within this chunk.
378
- *
356
+ *
379
357
  * Finally we compute a mask that indicates which bytes need to be escaped. If the mask is 0 then,
380
358
  * no bytes need to be escaped and we can continue to the next chunk. If the mask is not 0 then we
381
359
  * have at least one byte that needs to be escaped.
382
360
  */
383
- while (search->ptr + sizeof(uint8x16_t) <= search->end) {
384
- uint64_t mask = neon_rules_update(search->ptr);
385
361
 
386
- if (!mask) {
387
- search->ptr += sizeof(uint8x16_t);
388
- continue;
389
- }
390
- search->matches_mask = mask;
362
+ if (string_scan_simd_neon(&search->ptr, search->end, &search->matches_mask)) {
391
363
  search->has_matches = true;
392
364
  search->chunk_base = search->ptr;
393
365
  search->chunk_end = search->ptr + sizeof(uint8x16_t);
394
366
  return neon_next_match(search);
395
367
  }
396
368
 
397
- // There are fewer than 16 bytes left.
369
+ // There are fewer than 16 bytes left.
398
370
  unsigned long remaining = (search->end - search->ptr);
399
371
  if (remaining >= SIMD_MINIMUM_THRESHOLD) {
400
372
  char *s = copy_remaining_bytes(search, sizeof(uint8x16_t), remaining);
401
373
 
402
- uint64_t mask = neon_rules_update(s);
374
+ uint64_t mask = compute_chunk_mask_neon(s);
403
375
 
404
376
  if (!mask) {
405
- // Nothing to escape, ensure search_flush doesn't do anything by setting
377
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
406
378
  // search->cursor to search->ptr.
407
- search->buffer->len += remaining;
379
+ fbuffer_consumed(search->buffer, remaining);
408
380
  search->ptr = search->end;
409
381
  search->cursor = search->end;
410
382
  return 0;
@@ -428,11 +400,6 @@ static inline unsigned char search_escape_basic_neon(search_state *search)
428
400
 
429
401
  #ifdef HAVE_SIMD_SSE2
430
402
 
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
403
  static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
437
404
  {
438
405
  int mask = search->matches_mask;
@@ -457,18 +424,6 @@ static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
457
424
  #define TARGET_SSE2
458
425
  #endif
459
426
 
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
427
  static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(search_state *search)
473
428
  {
474
429
  if (RB_UNLIKELY(search->has_matches)) {
@@ -476,7 +431,7 @@ static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(se
476
431
  if (search->matches_mask > 0) {
477
432
  return sse2_next_match(search);
478
433
  } else {
479
- // sse2_next_match will only advance search->ptr up to the last matching character.
434
+ // sse2_next_match will only advance search->ptr up to the last matching character.
480
435
  // Skip over any characters in the last chunk that occur after the last match.
481
436
  search->has_matches = false;
482
437
  if (RB_UNLIKELY(search->chunk_base + sizeof(__m128i) >= search->end)) {
@@ -487,31 +442,24 @@ static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(se
487
442
  }
488
443
  }
489
444
 
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
-
445
+ if (string_scan_simd_sse2(&search->ptr, search->end, &search->matches_mask)) {
498
446
  search->has_matches = true;
499
- search->matches_mask = needs_escape_mask;
500
447
  search->chunk_base = search->ptr;
448
+ search->chunk_end = search->ptr + sizeof(__m128i);
501
449
  return sse2_next_match(search);
502
450
  }
503
451
 
504
- // There are fewer than 16 bytes left.
452
+ // There are fewer than 16 bytes left.
505
453
  unsigned long remaining = (search->end - search->ptr);
506
454
  if (remaining >= SIMD_MINIMUM_THRESHOLD) {
507
455
  char *s = copy_remaining_bytes(search, sizeof(__m128i), remaining);
508
456
 
509
- int needs_escape_mask = sse2_update(s);
457
+ int needs_escape_mask = compute_chunk_mask_sse2(s);
510
458
 
511
459
  if (needs_escape_mask == 0) {
512
- // Nothing to escape, ensure search_flush doesn't do anything by setting
460
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
513
461
  // search->cursor to search->ptr.
514
- search->buffer->len += remaining;
462
+ fbuffer_consumed(search->buffer, remaining);
515
463
  search->ptr = search->end;
516
464
  search->cursor = search->end;
517
465
  return 0;
@@ -638,7 +586,8 @@ static inline unsigned char search_ascii_only_escape(search_state *search, const
638
586
  return 0;
639
587
  }
640
588
 
641
- 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
+ {
642
591
  const unsigned char ch = (unsigned char)*search->ptr;
643
592
  switch (ch_len) {
644
593
  case 1: {
@@ -668,7 +617,7 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
668
617
 
669
618
  uint32_t wchar = 0;
670
619
 
671
- switch(ch_len) {
620
+ switch (ch_len) {
672
621
  case 2:
673
622
  wchar = ch & 0x1F;
674
623
  break;
@@ -828,7 +777,8 @@ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
828
777
  * _state_ is a JSON::State object, that can also be used to configure the
829
778
  * produced JSON string output further.
830
779
  */
831
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
780
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self)
781
+ {
832
782
  rb_check_arity(argc, 0, 1);
833
783
  VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
834
784
  return cState_partial_generate(Vstate, self, generate_json_array, Qfalse);
@@ -890,7 +840,8 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
890
840
  *
891
841
  * Extends _modul_ with the String::Extend module.
892
842
  */
893
- static VALUE mString_included_s(VALUE self, VALUE modul) {
843
+ static VALUE mString_included_s(VALUE self, VALUE modul)
844
+ {
894
845
  VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
895
846
  rb_call_super(1, &modul);
896
847
  return result;
@@ -1135,7 +1086,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1135
1086
  }
1136
1087
 
1137
1088
  VALUE key_to_s;
1138
- switch(rb_type(key)) {
1089
+ switch (rb_type(key)) {
1139
1090
  case T_STRING:
1140
1091
  if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
1141
1092
  key_to_s = key;
@@ -1219,7 +1170,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
1219
1170
 
1220
1171
  fbuffer_append_char(buffer, '[');
1221
1172
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1222
- for(i = 0; i < RARRAY_LEN(obj); i++) {
1173
+ for (i = 0; i < RARRAY_LEN(obj); i++) {
1223
1174
  if (i > 0) {
1224
1175
  fbuffer_append_char(buffer, ',');
1225
1176
  if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
@@ -1304,7 +1255,7 @@ static void generate_json_string(FBuffer *buffer, struct generate_json_data *dat
1304
1255
  search.chunk_base = NULL;
1305
1256
  #endif /* HAVE_SIMD */
1306
1257
 
1307
- switch(rb_enc_str_coderange(obj)) {
1258
+ switch (rb_enc_str_coderange(obj)) {
1308
1259
  case ENC_CODERANGE_7BIT:
1309
1260
  case ENC_CODERANGE_VALID:
1310
1261
  if (RB_UNLIKELY(data->state->ascii_only)) {
@@ -1406,17 +1357,16 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
1406
1357
  }
1407
1358
 
1408
1359
  /* This implementation writes directly into the buffer. We reserve
1409
- * the 24 characters that fpconv_dtoa states as its maximum, plus
1410
- * 2 more characters for the potential ".0" suffix.
1360
+ * the 28 characters that fpconv_dtoa states as its maximum.
1411
1361
  */
1412
- fbuffer_inc_capa(buffer, 26);
1362
+ fbuffer_inc_capa(buffer, 28);
1413
1363
  char* d = buffer->ptr + buffer->len;
1414
1364
  int len = fpconv_dtoa(value, d);
1415
1365
 
1416
1366
  /* fpconv_dtoa converts a float to its shortest string representation,
1417
1367
  * but it adds a ".0" if this is a plain integer.
1418
1368
  */
1419
- buffer->len += len;
1369
+ fbuffer_consumed(buffer, len);
1420
1370
  }
1421
1371
 
1422
1372
  static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
@@ -1957,15 +1907,30 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
1957
1907
  return Qnil;
1958
1908
  }
1959
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
+
1960
1924
  static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1961
1925
  {
1962
- 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;
1963
1928
 
1964
- if (key == sym_indent) { state->indent = string_config(val); }
1965
- else if (key == sym_space) { state->space = string_config(val); }
1966
- else if (key == sym_space_before) { state->space_before = string_config(val); }
1967
- else if (key == sym_object_nl) { state->object_nl = string_config(val); }
1968
- 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)); }
1969
1934
  else if (key == sym_max_nesting) { state->max_nesting = long_config(val); }
1970
1935
  else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); }
1971
1936
  else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); }
@@ -1974,11 +1939,14 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1974
1939
  else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
1975
1940
  else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
1976
1941
  else if (key == sym_strict) { state->strict = RTEST(val); }
1977
- 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
+ }
1978
1946
  return ST_CONTINUE;
1979
1947
  }
1980
1948
 
1981
- static void configure_state(JSON_Generator_State *state, VALUE config)
1949
+ static void configure_state(JSON_Generator_State *state, VALUE vstate, VALUE config)
1982
1950
  {
1983
1951
  if (!RTEST(config)) return;
1984
1952
 
@@ -1986,15 +1954,20 @@ static void configure_state(JSON_Generator_State *state, VALUE config)
1986
1954
 
1987
1955
  if (!RHASH_SIZE(config)) return;
1988
1956
 
1957
+ struct configure_state_data data = {
1958
+ .state = state,
1959
+ .vstate = vstate
1960
+ };
1961
+
1989
1962
  // We assume in most cases few keys are set so it's faster to go over
1990
1963
  // the provided keys than to check all possible keys.
1991
- rb_hash_foreach(config, configure_state_i, (VALUE)state);
1964
+ rb_hash_foreach(config, configure_state_i, (VALUE)&data);
1992
1965
  }
1993
1966
 
1994
1967
  static VALUE cState_configure(VALUE self, VALUE opts)
1995
1968
  {
1996
1969
  GET_STATE(self);
1997
- configure_state(state, opts);
1970
+ configure_state(state, self, opts);
1998
1971
  return self;
1999
1972
  }
2000
1973
 
@@ -2002,7 +1975,7 @@ static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
2002
1975
  {
2003
1976
  JSON_Generator_State state = {0};
2004
1977
  state_init(&state);
2005
- configure_state(&state, opts);
1978
+ configure_state(&state, Qfalse, opts);
2006
1979
 
2007
1980
  char stack_buffer[FBUFFER_STACK_SIZE];
2008
1981
  FBuffer buffer = {
@@ -2169,7 +2142,7 @@ void Init_generator(void)
2169
2142
  rb_require("json/ext/generator/state");
2170
2143
 
2171
2144
 
2172
- switch(find_simd_implementation()) {
2145
+ switch (find_simd_implementation()) {
2173
2146
  #ifdef HAVE_SIMD
2174
2147
  #ifdef HAVE_SIMD_NEON
2175
2148
  case SIMD_NEON:
@@ -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'