-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.c
More file actions
2155 lines (1884 loc) · 71.3 KB
/
Copy pathgenerator.c
File metadata and controls
2155 lines (1884 loc) · 71.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ruby.h"
#include "../fbuffer/fbuffer.h"
#include <math.h>
#include <ctype.h>
#include "extconf.h"
#include "simd.h"
/* ruby api and some helpers */
typedef struct JSON_Generator_StateStruct {
VALUE indent;
VALUE space;
VALUE space_before;
VALUE object_nl;
VALUE array_nl;
long max_nesting;
long depth;
long buffer_initial_length;
bool allow_nan;
bool ascii_only;
bool script_safe;
bool strict;
} JSON_Generator_State;
#ifndef RB_UNLIKELY
#define RB_UNLIKELY(cond) (cond)
#endif
static VALUE mJSON, cState, mString_Extend, eGeneratorError, eNestingError, Encoding_UTF_8;
static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
static ID sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict;
static void (*convert_UTF8_to_JSON_impl)(FBuffer *, VALUE, const unsigned char escape_table[256]);
#ifdef ENABLE_SIMD
static void (*convert_UTF8_to_JSON_simd_kernel)(FBuffer *out_buffer, const char * ptr, unsigned long len, unsigned long *_beg, unsigned long *_pos, const char *hexdig, char scratch[12], const unsigned char escape_table[256]);
#endif
#define GET_STATE_TO(self, state) \
TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
#define GET_STATE(self) \
JSON_Generator_State *state; \
GET_STATE_TO(self, state)
struct generate_json_data;
typedef void (*generator_func)(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
struct generate_json_data {
FBuffer *buffer;
VALUE vstate;
JSON_Generator_State *state;
VALUE obj;
generator_func func;
};
static VALUE cState_from_state_s(VALUE self, VALUE opts);
static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func, VALUE io);
static void generate_json(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
#ifdef RUBY_INTEGER_UNIFICATION
static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
#endif
static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj);
static int usascii_encindex, utf8_encindex, binary_encindex;
#ifdef RBIMPL_ATTR_NORETURN
RBIMPL_ATTR_NORETURN()
#endif
static void raise_generator_error_str(VALUE invalid_object, VALUE str)
{
VALUE exc = rb_exc_new_str(eGeneratorError, str);
rb_ivar_set(exc, rb_intern("@invalid_object"), invalid_object);
rb_exc_raise(exc);
}
#ifdef RBIMPL_ATTR_NORETURN
RBIMPL_ATTR_NORETURN()
#endif
#ifdef RBIMPL_ATTR_FORMAT
RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
#endif
static void raise_generator_error(VALUE invalid_object, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
VALUE str = rb_vsprintf(fmt, args);
va_end(args);
raise_generator_error_str(invalid_object, str);
}
// 0 - single byte char that don't need to be escaped.
// (x | 8) - char that needs to be escaped.
static const unsigned char CHAR_LENGTH_MASK = 7;
static const unsigned char escape_table[256] = {
// ASCII Control Characters
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
// ASCII Characters
0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static const unsigned char ascii_only_escape_table[256] = {
// ASCII Control Characters
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
// ASCII Characters
0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// Continuation byte
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
// First byte of a 2-byte code point
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
// First byte of a 3-byte code point
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
//First byte of a 4+ byte code point
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
};
static const unsigned char script_safe_escape_table[256] = {
// ASCII Control Characters
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
// ASCII Characters
0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, // '"' and '/'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// Continuation byte
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
// First byte of a 2-byte code point
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
// First byte of a 3-byte code point
3, 3,11, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE2 is the start of \u2028 and \u2029
//First byte of a 4+ byte code point
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
};
/* Converts in_string to a JSON string (without the wrapping '"'
* characters) in FBuffer out_buffer.
*
* Character are JSON-escaped according to:
*
* - Always: ASCII control characters (0x00-0x1F), dquote, and
* backslash.
*
* - If out_ascii_only: non-ASCII characters (>0x7F)
*
* - If script_safe: forwardslash (/), line separator (U+2028), and
* paragraph separator (U+2029)
*
* Everything else (should be UTF-8) is just passed through and
* appended to the result.
*/
#define FLUSH_POS(bytes) if (pos > beg) { fbuffer_append(out_buffer, &ptr[beg], pos - beg); } pos += bytes; beg = pos;
#define PROCESS_BYTE if (RB_UNLIKELY(ch_len)) { \
switch (ch_len) { \
case 9: { \
FLUSH_POS(1); \
switch (ch) { \
case '"': fbuffer_append(out_buffer, "\\\"", 2); break; \
case '\\': fbuffer_append(out_buffer, "\\\\", 2); break; \
case '/': fbuffer_append(out_buffer, "\\/", 2); break; \
case '\b': fbuffer_append(out_buffer, "\\b", 2); break; \
case '\f': fbuffer_append(out_buffer, "\\f", 2); break; \
case '\n': fbuffer_append(out_buffer, "\\n", 2); break; \
case '\r': fbuffer_append(out_buffer, "\\r", 2); break; \
case '\t': fbuffer_append(out_buffer, "\\t", 2); break; \
default: { \
scratch[2] = '0'; \
scratch[3] = '0'; \
scratch[4] = hexdig[(ch >> 4) & 0xf]; \
scratch[5] = hexdig[ch & 0xf]; \
fbuffer_append(out_buffer, scratch, 6); \
break; \
} \
} \
break; \
} \
case 11: { \
unsigned char b2 = ptr[pos + 1]; \
if (RB_UNLIKELY(b2 == 0x80)) { \
unsigned char b3 = ptr[pos + 2]; \
if (b3 == 0xA8) { \
FLUSH_POS(3); \
fbuffer_append(out_buffer, "\\u2028", 6); \
break; \
} else if (b3 == 0xA9) { \
FLUSH_POS(3); \
fbuffer_append(out_buffer, "\\u2029", 6); \
break; \
} \
} \
ch_len = 3; \
} \
default: \
pos += ch_len; \
break; \
} \
} else { \
pos++; \
}
#ifdef ENABLE_SIMD
static void convert_UTF8_to_JSON_simd(FBuffer *out_buffer, VALUE str, const unsigned char escape_table[256])
{
const char *hexdig = "0123456789abcdef";
char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
const char *ptr = RSTRING_PTR(str);
unsigned long len = RSTRING_LEN(str);
unsigned long beg = 0, pos = 0;
convert_UTF8_to_JSON_simd_kernel(out_buffer, ptr, len, &beg, &pos, hexdig, scratch, escape_table);
while (pos < len) {
unsigned char ch = ptr[pos];
unsigned char ch_len = escape_table[ch];
PROCESS_BYTE;
}
if (beg < len) {
fbuffer_append(out_buffer, &ptr[beg], len - beg);
}
RB_GC_GUARD(str);
}
#endif
#ifdef HAVE_SIMD_NEON
void convert_UTF8_to_JSON_simd_kernel_neon(FBuffer *out_buffer, const char * ptr, unsigned long len, unsigned long *_beg, unsigned long *_pos, const char *hexdig, char scratch[12], const unsigned char escape_table[256]) {
unsigned long beg = *_beg, pos = *_pos;
const uint8x16_t lower_bound = vdupq_n_u8(' ');
const uint8x16_t backslash = vdupq_n_u8('\\');
const uint8x16_t dblquote = vdupq_n_u8('\"');
if (escape_table == script_safe_escape_table) {
/*
* This works almost exactly the same as what is described above. The difference in this case comes after we know
* there is a byte to be escaped. In the previous case, all bytes were handled the same way. In this case, however,
* some bytes need to be handled differently.
*
* Since we know each byte in chunk can only match a single case, we logical AND each of the has_backslash,
* has_dblquote, and has_forward_slash with a different bit (0x1, 0x2 and 0x4 respectively) and combine
* the results with a logical OR.
*
* Now we loop over the result vector and switch on the particular pattern we just created. If we find a
* case we don't know, we simply lookup the byte in the script_safe_escape_table to determine the correct
* action.
*/
const uint8x16_t upper_bound = vdupq_n_u8('~');
const uint8x16_t forward_slash = vdupq_n_u8('/');
while (pos+16 < len) {
uint8x16_t chunk = vld1q_u8((const uint8_t*)&ptr[pos]);
uint8x16_t too_low = vcltq_u8(chunk, lower_bound);
uint8x16_t too_high = vcgtq_u8(chunk, upper_bound);
uint8x16_t has_backslash = vceqq_u8(chunk, backslash);
uint8x16_t has_dblquote = vceqq_u8(chunk, dblquote);
uint8x16_t has_forward_slash = vceqq_u8(chunk, forward_slash);
uint8x16_t needs_escape = vorrq_u8(too_low, too_high);
uint8x16_t has_escaped_char = vorrq_u8(has_forward_slash, vorrq_u8(has_backslash, has_dblquote));
needs_escape = vorrq_u8(needs_escape, has_escaped_char);
if (vmaxvq_u8(needs_escape) == 0) {
pos += 16;
continue;
}
uint8x16_t tmp = vandq_u8(too_low, vdupq_n_u8(0x1));
tmp = vorrq_u8(tmp, vandq_u8(has_backslash, vdupq_n_u8(0x2)));
tmp = vorrq_u8(tmp, vandq_u8(has_dblquote, vdupq_n_u8(0x4)));
tmp = vorrq_u8(tmp, vandq_u8(has_forward_slash, vdupq_n_u8(0x8)));
uint8_t arr[16];
vst1q_u8(arr, tmp);
for (int i = 0; i < 16; ) {
unsigned long start = pos;
unsigned char ch = ptr[pos];
unsigned char ch_len = arr[i];
switch(ch_len) {
case 0x1:
case 0x2:
case 0x4:
case 0x8:
ch_len = 9;
break;
default:
ch_len = script_safe_escape_table[ch];
}
// This must remain in sync with the array `escape_table`.
if (RB_UNLIKELY(ch_len)) {
PROCESS_BYTE;
} else {
pos++;
}
i += (pos - start);
}
}
} else {
/*
* The code below implements an SIMD-based algorithm to determine if N bytes at a time
* need to be escaped.
*
* Assume the ptr = "Te\sting!" (the double quotes are included in the string)
*
* The explanination will be limited to the first 8 bytes of the string for simplicity. However
* the vector insructions may work on larger vectors.
*
* First, we load three constants 'lower_bound', 'backslash' and 'dblquote" in vector registers.
*
* lower_bound: [20 20 20 20 20 20 20 20]
* backslash: [5C 5C 5C 5C 5C 5C 5C 5C]
* dblquote: [22 22 22 22 22 22 22 22]
*
* Next we load the first chunk of the ptr:
* [22 54 65 5C 73 74 69 6E] (" T e \ s t i n)
*
* First we check if any byte in chunk is less than 32 (0x20). This returns the following vector
* as no bytes are less than 32 (0x20):
* [0 0 0 0 0 0 0 0]
*
* Next, we check if any byte in chunk is equal to a backslash:
* [0 0 0 FF 0 0 0 0]
*
* Finally we check if any byte in chunk is equal to a double quote:
* [FF 0 0 0 0 0 0 0]
*
* Now we have three vectors where each byte indicates if the corresponding byte in chunk
* needs to be escaped. We combine these vectors with a series of logical OR instructions.
* This is the needs_escape vector and it is equal to:
* [FF 0 0 FF 0 0 0 0]
*
* For ARM Neon specifically, we check if the maximum number in the vector is 0. The maximum of
* the needs_escape vector is FF. Therefore, we know there is at least one byte that needs to be
* escaped.
*
* If the maximum of the needs_escape vector is 0, none of the bytes need to be escaped and
* we advance pos by the width of the vector.
*
* To determine how to escape characters, we look at each value in the needs_escape vector and take
* the appropriate action.
*/
while (pos+16 < len) {
uint8x16_t chunk = vld1q_u8((const uint8_t*)&ptr[pos]);
uint8x16_t too_low = vcltq_u8(chunk, lower_bound);
uint8x16_t has_backslash = vceqq_u8(chunk, backslash);
uint8x16_t has_dblquote = vceqq_u8(chunk, dblquote);
uint8x16_t needs_escape = vorrq_u8(too_low, vorrq_u8(has_backslash, has_dblquote));
if (vmaxvq_u8(needs_escape) == 0) {
pos += 16;
continue;
}
/*
* TODO Consider making another type simd_vec_mask. The reason being on x86 we can use _mm_movemask_epi8
* to get a mask rather than storing the vector to memory.
*
* We would need another function like simd_vec_mask_position_set(mask, pos) which returns true
* if the bit/byte (implementation defined) at position 'pos' is non-zero.
*/
uint8_t arr[16];
vst1q_u8(arr, needs_escape);
for (int i = 0; i < 16; i++) {
unsigned char ch = ptr[pos];
unsigned char ch_len = arr[i];
// This must remain in sync with the array `escape_table`.
if (RB_UNLIKELY(ch_len)) {
ch_len = 9;
PROCESS_BYTE;
} else {
pos++;
}
}
}
}
*_beg = beg;
*_pos = pos;
}
#endif /* HAVE_SIMD_NEON */
#ifdef HAVE_SIMD_X86_64
#ifdef HAVE_TYPE___M128I
#ifdef __GNUC__
#pragma GCC push_options
#pragma GCC target ("sse4")
#endif /* __GNUC__ */
#define _mm_cmpge_epu8(a, b) _mm_cmpeq_epi8(_mm_max_epu8(a, b), a)
#define _mm_cmple_epu8(a, b) _mm_cmpge_epu8(b, a)
#define _mm_cmpgt_epu8(a, b) _mm_xor_si128(_mm_cmple_epu8(a, b), _mm_set1_epi8(-1))
#define _mm_cmplt_epu8(a, b) _mm_cmpgt_epu8(b, a)
#ifdef __clang__
__attribute__((target("sse4.2")))
#endif /* __clang__ */
void convert_UTF8_to_JSON_simd_kernel_sse42(FBuffer *out_buffer, const char * ptr, unsigned long len, unsigned long *_beg, unsigned long *_pos, const char *hexdig, char scratch[12], const unsigned char escape_table[256]) {
unsigned long beg = *_beg, pos = *_pos;
if (escape_table == script_safe_escape_table) {
/*
* Again, this is basically a straight port of the ARM Neon version.
*/
const __m128i lower_bound = _mm_set1_epi8(' ');
const __m128i upper_bound = _mm_set1_epi8('~');
const __m128i backslash = _mm_set1_epi8('\\');
const __m128i dblquote = _mm_set1_epi8('\"');
const __m128i forward_slash = _mm_set1_epi8('/');
while (pos+16 < len) {
__m128i chunk = _mm_loadu_si128((__m128i const*)&ptr[pos]);
__m128i too_low = _mm_cmplt_epu8(chunk, lower_bound);
__m128i too_high = _mm_cmpgt_epu8(chunk, upper_bound);
__m128i has_backslash = _mm_cmpeq_epi8(chunk, backslash);
__m128i has_dblquote = _mm_cmpeq_epi8(chunk, dblquote);
__m128i has_forward_slash = _mm_cmpeq_epi8(chunk, forward_slash);
__m128i needs_escape = _mm_or_si128(too_low, too_high);
__m128i has_escaped_char = _mm_or_si128(has_forward_slash, _mm_or_si128(has_backslash, has_dblquote));
needs_escape = _mm_or_si128(needs_escape, has_escaped_char);
int needs_escape_mask = _mm_movemask_epi8(needs_escape);
if (needs_escape_mask == 0) {
pos += 16;
continue;
}
__m128i tmp = _mm_and_si128(too_low, _mm_set1_epi8(0x1));
tmp = _mm_or_si128(tmp, _mm_and_si128(has_backslash, _mm_set1_epi8(0x2)));
tmp = _mm_or_si128(tmp, _mm_and_si128(has_dblquote, _mm_set1_epi8(0x4)));
tmp = _mm_or_si128(tmp, _mm_and_si128(has_forward_slash, _mm_set1_epi8(0x8)));
uint8_t arr[16];
_mm_storeu_si128((__m128i *) arr, tmp);
for (int i = 0; i < 16; ) {
unsigned long start = pos;
unsigned char ch = ptr[pos];
unsigned char ch_len = arr[i];
switch(ch_len) {
case 0x1:
case 0x2:
case 0x4:
case 0x8:
ch_len = 9;
break;
default:
ch_len = script_safe_escape_table[ch];
}
// This must remain in sync with the array `escape_table`.
if (RB_UNLIKELY(ch_len)) {
PROCESS_BYTE;
} else {
pos++;
}
i += (pos - start);
}
}
} else {
/*
* This is a straight port of the ARM Neon implementation to SSE4. This is
* likely not optimal for this instruction set. There is likely table lookup,
* shuffle, gather, blend, etc. instructions that may perform significantly
* better than what is implemented here.
*/
const __m128i lower_bound = _mm_set1_epi8(' ');
const __m128i backslash = _mm_set1_epi8('\\');
const __m128i dblquote = _mm_set1_epi8('\"');
while (pos+16 < len) {
__m128i chunk = _mm_loadu_si128((__m128i const*)&ptr[pos]);
__m128i too_low = _mm_cmplt_epu8(chunk, lower_bound);
__m128i has_backslash = _mm_cmpeq_epi8(chunk, backslash);
__m128i has_dblquote = _mm_cmpeq_epi8(chunk, dblquote);
__m128i needs_escape = _mm_or_si128(too_low, _mm_or_si128(has_backslash, has_dblquote));
int needs_escape_mask = _mm_movemask_epi8(needs_escape);
if (needs_escape_mask == 0) {
pos += 16;
continue;
}
for (int i = 0; i < 16; i++) {
int bit = needs_escape_mask & (1 << i);
unsigned char ch = ptr[pos];
unsigned char ch_len = 0;
// This must remain in sync with the array `escape_table`.
if (RB_UNLIKELY(bit)) {
ch_len = 9;
PROCESS_BYTE;
} else {
pos++;
}
}
}
}
*_beg = beg;
*_pos = pos;
}
#ifdef __GNUC__
#pragma GCC pop_options
#endif /* __GNUC__ */
#endif /* HAVE_TYPE___M128I */
#ifdef HAVE_TYPE___M256I
#ifdef __GNUC__
#pragma GCC push_options
#pragma GCC target ("avx2")
#endif /* __GNUC__ */
#define _mm256_cmpge_epu8(a, b) _mm256_cmpeq_epi8(_mm256_max_epu8(a, b), a)
#define _mm256_cmple_epu8(a, b) _mm256_cmpge_epu8(b, a)
#define _mm256_cmpgt_epu8(a, b) _mm256_xor_si256(_mm256_cmple_epu8(a, b), _mm256_set1_epi8(-1))
#define _mm256_cmplt_epu8(a, b) _mm256_cmpgt_epu8(b, a)
#ifdef __clang__
__attribute__((target("avx2")))
#endif /* __clang__ */
void convert_UTF8_to_JSON_simd_kernel_avx2(FBuffer *out_buffer, const char * ptr, unsigned long len, unsigned long *_beg, unsigned long *_pos, const char *hexdig, char scratch[12], const unsigned char escape_table[256]) {
unsigned long beg = *_beg, pos = *_pos;
const __m256i lower_bound = _mm256_set1_epi8(' ');
const __m256i backslash = _mm256_set1_epi8('\\');
const __m256i dblquote = _mm256_set1_epi8('\"');
if (escape_table == script_safe_escape_table) {
/*
* Again, this is basically a straight port of the ARM Neon version.
*/
const __m256i upper_bound = _mm256_set1_epi8('~');
const __m256i forward_slash = _mm256_set1_epi8('/');
while (pos+32 < len) {
__m256i chunk = _mm256_loadu_si256((__m256i const*)&ptr[pos]);
__m256i too_low = _mm256_cmplt_epu8(chunk, lower_bound);
__m256i too_high = _mm256_cmpgt_epu8(chunk, upper_bound);
__m256i has_backslash = _mm256_cmpeq_epi8(chunk, backslash);
__m256i has_dblquote = _mm256_cmpeq_epi8(chunk, dblquote);
__m256i has_forward_slash = _mm256_cmpeq_epi8(chunk, forward_slash);
__m256i needs_escape = _mm256_or_si256(too_low, too_high);
__m256i has_escaped_char = _mm256_or_si256(has_forward_slash, _mm256_or_si256(has_backslash, has_dblquote));
needs_escape = _mm256_or_si256(needs_escape, has_escaped_char);
int needs_escape_mask = _mm256_movemask_epi8(needs_escape);
if (needs_escape_mask == 0) {
pos += 32;
continue;
}
__m256i tmp = _mm256_and_si256(too_low, _mm256_set1_epi8(0x1));
tmp = _mm256_or_si256(tmp, _mm256_and_si256(has_backslash, _mm256_set1_epi8(0x2)));
tmp = _mm256_or_si256(tmp, _mm256_and_si256(has_dblquote, _mm256_set1_epi8(0x4)));
tmp = _mm256_or_si256(tmp, _mm256_and_si256(has_forward_slash, _mm256_set1_epi8(0x8)));
uint8_t arr[32];
_mm256_storeu_si256((__m256i *) arr, tmp);
for (int i = 0; i < 32; ) {
unsigned long start = pos;
unsigned char ch = ptr[pos];
unsigned char ch_len = arr[i];
switch(ch_len) {
case 0x1:
case 0x2:
case 0x4:
case 0x8:
ch_len = 9;
break;
default:
ch_len = script_safe_escape_table[ch];
}
// This must remain in sync with the array `escape_table`.
if (RB_UNLIKELY(ch_len)) {
PROCESS_BYTE;
} else {
pos++;
}
i += (pos - start);
}
}
} else {
/*
* This is a straight port of the ARM Neon implementation to SSE4. This is
* likely not optimal for this instruction set. There is likely table lookup,
* shuffle, gather, blend, etc. instructions that may perform significantly
* better than what is implemented here.
*/
while (pos+32 < len) {
__m256i chunk = _mm256_loadu_si256((__m256i const*)&ptr[pos]);
__m256i too_low = _mm256_cmplt_epu8(chunk, lower_bound);
__m256i has_backslash = _mm256_cmpeq_epi8(chunk, backslash);
__m256i has_dblquote = _mm256_cmpeq_epi8(chunk, dblquote);
__m256i needs_escape = _mm256_or_si256(too_low, _mm256_or_si256(has_backslash, has_dblquote));
int needs_escape_mask = _mm256_movemask_epi8(needs_escape);
if (needs_escape_mask == 0) {
pos += 32;
continue;
}
for (int i = 0; i < 32; i++) {
int bit = needs_escape_mask & (1 << i);
unsigned char ch = ptr[pos];
unsigned char ch_len = 0;
// This must remain in sync with the array `escape_table`.
if (RB_UNLIKELY(bit)) {
ch_len = 9;
PROCESS_BYTE;
} else {
pos++;
}
}
}
}
*_beg = beg;
*_pos = pos;
}
#ifdef __GNUC__
#pragma GCC pop_options
#endif /* __GNUC__ */
#endif /* HAVE_TYPE___M256I */
#endif /* x86_64 support */
static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE str, const unsigned char escape_table[256])
{
const char *hexdig = "0123456789abcdef";
char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
const char *ptr = RSTRING_PTR(str);
unsigned long len = RSTRING_LEN(str);
unsigned long beg = 0, pos = 0;
while (pos < len) {
unsigned char ch = ptr[pos];
unsigned char ch_len = escape_table[ch];
/* JSON encoding */
PROCESS_BYTE;
}
if (beg < len) {
fbuffer_append(out_buffer, &ptr[beg], len - beg);
}
RB_GC_GUARD(str);
}
#undef PROCESS_BYTE
static void convert_UTF8_to_ASCII_only_JSON(FBuffer *out_buffer, VALUE str, const unsigned char escape_table[256])
{
const char *hexdig = "0123456789abcdef";
char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
const char *ptr = RSTRING_PTR(str);
unsigned long len = RSTRING_LEN(str);
unsigned long beg = 0, pos = 0;
while (pos < len) {
unsigned char ch = ptr[pos];
unsigned char ch_len = escape_table[ch];
if (RB_UNLIKELY(ch_len)) {
switch (ch_len) {
case 9: {
FLUSH_POS(1);
switch (ch) {
case '"': fbuffer_append(out_buffer, "\\\"", 2); break;
case '\\': fbuffer_append(out_buffer, "\\\\", 2); break;
case '/': fbuffer_append(out_buffer, "\\/", 2); break;
case '\b': fbuffer_append(out_buffer, "\\b", 2); break;
case '\f': fbuffer_append(out_buffer, "\\f", 2); break;
case '\n': fbuffer_append(out_buffer, "\\n", 2); break;
case '\r': fbuffer_append(out_buffer, "\\r", 2); break;
case '\t': fbuffer_append(out_buffer, "\\t", 2); break;
default: {
scratch[2] = '0';
scratch[3] = '0';
scratch[4] = hexdig[(ch >> 4) & 0xf];
scratch[5] = hexdig[ch & 0xf];
fbuffer_append(out_buffer, scratch, 6);
break;
}
}
break;
}
default: {
uint32_t wchar = 0;
ch_len = ch_len & CHAR_LENGTH_MASK;
switch(ch_len) {
case 2:
wchar = ptr[pos] & 0x1F;
break;
case 3:
wchar = ptr[pos] & 0x0F;
break;
case 4:
wchar = ptr[pos] & 0x07;
break;
}
for (short i = 1; i < ch_len; i++) {
wchar = (wchar << 6) | (ptr[pos+i] & 0x3F);
}
FLUSH_POS(ch_len);
if (wchar <= 0xFFFF) {
scratch[2] = hexdig[wchar >> 12];
scratch[3] = hexdig[(wchar >> 8) & 0xf];
scratch[4] = hexdig[(wchar >> 4) & 0xf];
scratch[5] = hexdig[wchar & 0xf];
fbuffer_append(out_buffer, scratch, 6);
} else {
uint16_t hi, lo;
wchar -= 0x10000;
hi = 0xD800 + (uint16_t)(wchar >> 10);
lo = 0xDC00 + (uint16_t)(wchar & 0x3FF);
scratch[2] = hexdig[hi >> 12];
scratch[3] = hexdig[(hi >> 8) & 0xf];
scratch[4] = hexdig[(hi >> 4) & 0xf];
scratch[5] = hexdig[hi & 0xf];
scratch[8] = hexdig[lo >> 12];
scratch[9] = hexdig[(lo >> 8) & 0xf];
scratch[10] = hexdig[(lo >> 4) & 0xf];
scratch[11] = hexdig[lo & 0xf];
fbuffer_append(out_buffer, scratch, 12);
}
break;
}
}
} else {
pos++;
}
}
if (beg < len) {
fbuffer_append(out_buffer, &ptr[beg], len - beg);
}
RB_GC_GUARD(str);
}
#undef FLUSH_POS
/*
* Document-module: JSON::Ext::Generator
*
* This is the JSON generator implemented as a C extension. It can be
* configured to be used by setting
*
* JSON.generator = JSON::Ext::Generator
*
* with the method generator= in JSON.
*
*/
/* Explanation of the following: that's the only way to not pollute
* standard library's docs with GeneratorMethods::<ClassName> which
* are uninformative and take a large place in a list of classes
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Array
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Bignum
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::FalseClass
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Fixnum
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Float
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Hash
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Integer
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::NilClass
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::Object
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::String
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::String::Extend
* :nodoc:
*/
/*
* Document-module: JSON::Ext::Generator::GeneratorMethods::TrueClass
* :nodoc:
*/
/*
* call-seq: to_json(state = nil)
*
* Returns a JSON string containing a JSON object, that is generated from
* this Hash instance.
* _state_ is a JSON::State object, that can also be used to configure the
* produced JSON string output further.
*/
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
return cState_partial_generate(Vstate, self, generate_json_object, Qfalse);
}
/*
* call-seq: to_json(state = nil)
*
* Returns a JSON string containing a JSON array, that is generated from
* this Array instance.
* _state_ is a JSON::State object, that can also be used to configure the
* produced JSON string output further.
*/
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
rb_check_arity(argc, 0, 1);
VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
return cState_partial_generate(Vstate, self, generate_json_array, Qfalse);
}
#ifdef RUBY_INTEGER_UNIFICATION
/*
* call-seq: to_json(*)
*
* Returns a JSON string representation for this Integer number.
*/
static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
return cState_partial_generate(Vstate, self, generate_json_integer, Qfalse);
}
#else
/*
* call-seq: to_json(*)
*
* Returns a JSON string representation for this Integer number.
*/
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
return cState_partial_generate(Vstate, self, generate_json_fixnum, Qfalse);
}
/*
* call-seq: to_json(*)
*
* Returns a JSON string representation for this Integer number.
*/
static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
return cState_partial_generate(Vstate, self, generate_json_bignum, Qfalse);
}
#endif
/*
* call-seq: to_json(*)
*
* Returns a JSON string representation for this Float number.
*/
static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
return cState_partial_generate(Vstate, self, generate_json_float, Qfalse);
}
/*
* call-seq: String.included(modul)
*
* Extends _modul_ with the String::Extend module.
*/
static VALUE mString_included_s(VALUE self, VALUE modul) {
VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
rb_call_super(1, &modul);
return result;
}
/*
* call-seq: to_json(*)
*
* This string should be encoded with UTF-8 A call to this method
* returns a JSON string encoded with UTF16 big endian characters as
* \u????.
*/
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);