oj 3.16.3 → 3.17.4
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 +4 -4
- data/CHANGELOG.md +99 -0
- data/README.md +0 -16
- data/ext/oj/compat.c +3 -3
- data/ext/oj/custom.c +17 -11
- data/ext/oj/dump.c +619 -88
- data/ext/oj/dump.h +9 -2
- data/ext/oj/dump_compat.c +16 -9
- data/ext/oj/dump_leaf.c +1 -1
- data/ext/oj/dump_object.c +52 -27
- data/ext/oj/dump_strict.c +20 -14
- data/ext/oj/extconf.rb +11 -9
- data/ext/oj/fast.c +90 -65
- data/ext/oj/intern.c +1 -1
- data/ext/oj/mimic_json.c +14 -8
- data/ext/oj/object.c +18 -12
- data/ext/oj/oj.c +426 -11
- data/ext/oj/oj.h +60 -52
- data/ext/oj/parse.c +252 -48
- data/ext/oj/parse.h +0 -1
- data/ext/oj/parser.c +179 -47
- data/ext/oj/parser.h +4 -2
- data/ext/oj/rails.c +57 -36
- data/ext/oj/reader.c +17 -5
- data/ext/oj/rxclass.c +17 -1
- data/ext/oj/rxclass.h +2 -1
- data/ext/oj/safe.c +230 -0
- data/ext/oj/safe.h +79 -0
- data/ext/oj/saj.c +55 -20
- data/ext/oj/scp.c +3 -6
- data/ext/oj/simd.h +219 -0
- data/ext/oj/sparse.c +15 -3
- data/ext/oj/stream_writer.c +19 -14
- data/ext/oj/strict.c +2 -4
- data/ext/oj/string_writer.c +26 -14
- data/ext/oj/usual.c +37 -35
- data/ext/oj/wab.c +5 -4
- data/lib/oj/mimic.rb +1 -5
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/version.rb +1 -1
- data/pages/Encoding.md +1 -1
- metadata +22 -100
- data/test/_test_active.rb +0 -75
- data/test/_test_active_mimic.rb +0 -95
- data/test/_test_mimic_rails.rb +0 -123
- data/test/activerecord/result_test.rb +0 -31
- data/test/activesupport6/abstract_unit.rb +0 -44
- data/test/activesupport6/decoding_test.rb +0 -133
- data/test/activesupport6/encoding_test.rb +0 -507
- data/test/activesupport6/encoding_test_cases.rb +0 -98
- data/test/activesupport6/test_common.rb +0 -17
- data/test/activesupport6/test_helper.rb +0 -163
- data/test/activesupport6/time_zone_test_helpers.rb +0 -39
- data/test/activesupport7/abstract_unit.rb +0 -49
- data/test/activesupport7/decoding_test.rb +0 -125
- data/test/activesupport7/encoding_test.rb +0 -486
- data/test/activesupport7/encoding_test_cases.rb +0 -104
- data/test/activesupport7/time_zone_test_helpers.rb +0 -47
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -14
- data/test/helper.rb +0 -39
- data/test/isolated/shared.rb +0 -309
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/json_gem/json_addition_test.rb +0 -216
- data/test/json_gem/json_common_interface_test.rb +0 -155
- data/test/json_gem/json_encoding_test.rb +0 -107
- data/test/json_gem/json_ext_parser_test.rb +0 -21
- data/test/json_gem/json_fixtures_test.rb +0 -36
- data/test/json_gem/json_generator_test.rb +0 -413
- data/test/json_gem/json_generic_object_test.rb +0 -90
- data/test/json_gem/json_parser_test.rb +0 -477
- data/test/json_gem/json_string_matching_test.rb +0 -42
- data/test/json_gem/test_helper.rb +0 -30
- data/test/mem.rb +0 -34
- data/test/perf.rb +0 -102
- data/test/perf_compat.rb +0 -128
- data/test/perf_dump.rb +0 -50
- data/test/perf_fast.rb +0 -162
- data/test/perf_file.rb +0 -62
- data/test/perf_object.rb +0 -134
- data/test/perf_once.rb +0 -59
- data/test/perf_parser.rb +0 -183
- data/test/perf_saj.rb +0 -101
- data/test/perf_scp.rb +0 -140
- data/test/perf_simple.rb +0 -289
- data/test/perf_strict.rb +0 -137
- data/test/perf_wab.rb +0 -129
- data/test/prec.rb +0 -23
- data/test/sample/change.rb +0 -13
- data/test/sample/dir.rb +0 -18
- data/test/sample/doc.rb +0 -35
- data/test/sample/file.rb +0 -47
- data/test/sample/group.rb +0 -15
- data/test/sample/hasprops.rb +0 -15
- data/test/sample/layer.rb +0 -11
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -9
- data/test/sample/shape.rb +0 -34
- data/test/sample/text.rb +0 -19
- data/test/sample.rb +0 -54
- data/test/sample_json.rb +0 -37
- data/test/test_compat.rb +0 -567
- data/test/test_custom.rb +0 -555
- data/test/test_debian.rb +0 -50
- data/test/test_fast.rb +0 -526
- data/test/test_file.rb +0 -250
- data/test/test_gc.rb +0 -60
- data/test/test_generate.rb +0 -21
- data/test/test_hash.rb +0 -39
- data/test/test_integer_range.rb +0 -72
- data/test/test_null.rb +0 -376
- data/test/test_object.rb +0 -1030
- data/test/test_parser.rb +0 -11
- data/test/test_parser_debug.rb +0 -27
- data/test/test_parser_saj.rb +0 -337
- data/test/test_parser_usual.rb +0 -227
- data/test/test_rails.rb +0 -35
- data/test/test_saj.rb +0 -188
- data/test/test_scp.rb +0 -431
- data/test/test_strict.rb +0 -441
- data/test/test_various.rb +0 -801
- data/test/test_wab.rb +0 -311
- data/test/test_writer.rb +0 -380
- data/test/tests.rb +0 -33
- data/test/tests_mimic.rb +0 -23
- data/test/tests_mimic_addition.rb +0 -16
data/ext/oj/oj.h
CHANGED
|
@@ -104,61 +104,64 @@ typedef enum {
|
|
|
104
104
|
} StreamWriterType;
|
|
105
105
|
|
|
106
106
|
typedef struct _dumpOpts {
|
|
107
|
-
bool
|
|
108
|
-
char
|
|
109
|
-
char
|
|
110
|
-
char
|
|
111
|
-
char
|
|
112
|
-
char
|
|
113
|
-
uint8_t
|
|
114
|
-
uint8_t
|
|
115
|
-
uint8_t
|
|
116
|
-
uint8_t
|
|
117
|
-
uint8_t
|
|
118
|
-
char
|
|
119
|
-
bool
|
|
120
|
-
bool
|
|
121
|
-
int
|
|
107
|
+
bool use;
|
|
108
|
+
char indent_str[16];
|
|
109
|
+
char before_sep[16];
|
|
110
|
+
char after_sep[16];
|
|
111
|
+
char hash_nl[16];
|
|
112
|
+
char array_nl[16];
|
|
113
|
+
uint8_t indent_size;
|
|
114
|
+
uint8_t before_size;
|
|
115
|
+
uint8_t after_size;
|
|
116
|
+
uint8_t hash_size;
|
|
117
|
+
uint8_t array_size;
|
|
118
|
+
char nan_dump; // NanDump
|
|
119
|
+
bool omit_nil;
|
|
120
|
+
bool omit_null_byte;
|
|
121
|
+
int max_depth;
|
|
122
|
+
const char *only;
|
|
123
|
+
const char *except;
|
|
122
124
|
} *DumpOpts;
|
|
123
125
|
|
|
124
126
|
typedef struct _options {
|
|
125
|
-
int indent;
|
|
126
|
-
char circular;
|
|
127
|
-
char auto_define;
|
|
128
|
-
char sym_key;
|
|
129
|
-
char escape_mode;
|
|
130
|
-
char mode;
|
|
131
|
-
char class_cache;
|
|
132
|
-
char time_format;
|
|
133
|
-
char bigdec_as_num;
|
|
134
|
-
char bigdec_load;
|
|
135
|
-
char compat_bigdec;
|
|
136
|
-
char to_hash;
|
|
137
|
-
char to_json;
|
|
138
|
-
char as_json;
|
|
139
|
-
char raw_json;
|
|
140
|
-
char nilnil;
|
|
141
|
-
char empty_string;
|
|
142
|
-
char allow_gc;
|
|
143
|
-
char quirks_mode;
|
|
144
|
-
char allow_invalid;
|
|
145
|
-
char create_ok;
|
|
146
|
-
char allow_nan;
|
|
147
|
-
char trace;
|
|
148
|
-
char safe;
|
|
149
|
-
char sec_prec_set;
|
|
150
|
-
char ignore_under;
|
|
151
|
-
char cache_keys;
|
|
152
|
-
char cache_str;
|
|
153
|
-
int64_t int_range_min;
|
|
154
|
-
int64_t int_range_max;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
char
|
|
160
|
-
|
|
161
|
-
VALUE
|
|
127
|
+
int indent; // indention for dump, default 2
|
|
128
|
+
char circular; // YesNo
|
|
129
|
+
char auto_define; // YesNo
|
|
130
|
+
char sym_key; // YesNo
|
|
131
|
+
char escape_mode; // Escape_Mode
|
|
132
|
+
char mode; // Mode
|
|
133
|
+
char class_cache; // YesNo
|
|
134
|
+
char time_format; // TimeFormat
|
|
135
|
+
char bigdec_as_num; // YesNo
|
|
136
|
+
char bigdec_load; // BigLoad
|
|
137
|
+
char compat_bigdec; // boolean (0 or 1)
|
|
138
|
+
char to_hash; // YesNo
|
|
139
|
+
char to_json; // YesNo
|
|
140
|
+
char as_json; // YesNo
|
|
141
|
+
char raw_json; // YesNo
|
|
142
|
+
char nilnil; // YesNo
|
|
143
|
+
char empty_string; // YesNo
|
|
144
|
+
char allow_gc; // allow GC during parse
|
|
145
|
+
char quirks_mode; // allow single JSON values instead of documents
|
|
146
|
+
char allow_invalid; // YesNo - allow invalid unicode
|
|
147
|
+
char create_ok; // YesNo allow create_id
|
|
148
|
+
char allow_nan; // YesNo for parsing only
|
|
149
|
+
char trace; // YesNo
|
|
150
|
+
char safe; // YesNo
|
|
151
|
+
char sec_prec_set; // boolean (0 or 1)
|
|
152
|
+
char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
|
|
153
|
+
char cache_keys; // YesNo
|
|
154
|
+
char cache_str; // string short than or equal to this are cache
|
|
155
|
+
int64_t int_range_min; // dump numbers below as string
|
|
156
|
+
int64_t int_range_max; // dump numbers above as string
|
|
157
|
+
size_t max_integer_digits; // 0 = unlimited; max decimal digits for parsed integers
|
|
158
|
+
const char *create_id; // 0 or string
|
|
159
|
+
size_t create_id_len; // length of create_id
|
|
160
|
+
int sec_prec; // second precision when dumping time
|
|
161
|
+
char float_prec; // float precision, linked to float_fmt
|
|
162
|
+
char float_fmt[7]; // float format for dumping, if empty use Ruby
|
|
163
|
+
VALUE hash_class; // class to use in place of Hash on load
|
|
164
|
+
VALUE array_class; // class to use in place of Array on load
|
|
162
165
|
struct _dumpOpts dump_opts;
|
|
163
166
|
struct _rxClass str_rx;
|
|
164
167
|
VALUE *ignore; // Qnil terminated array of classes or NULL
|
|
@@ -253,6 +256,10 @@ extern VALUE oj_custom_parse_cstr(int argc, VALUE *argv, char *json, size_t len)
|
|
|
253
256
|
|
|
254
257
|
extern bool oj_hash_has_key(VALUE hash, VALUE key);
|
|
255
258
|
extern void oj_parse_options(VALUE ropts, Options copts);
|
|
259
|
+
extern void oj_free_call_options(Options copts);
|
|
260
|
+
extern void oj_options_take_ownership(Options copts);
|
|
261
|
+
extern void oj_options_release(Options copts);
|
|
262
|
+
extern void oj_options_mark(Options copts);
|
|
256
263
|
|
|
257
264
|
extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
|
|
258
265
|
extern void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
|
|
@@ -334,6 +341,7 @@ extern ID oj_as_json_id;
|
|
|
334
341
|
extern ID oj_begin_id;
|
|
335
342
|
extern ID oj_bigdecimal_id;
|
|
336
343
|
extern ID oj_end_id;
|
|
344
|
+
extern ID oj_eofq_id;
|
|
337
345
|
extern ID oj_error_id;
|
|
338
346
|
extern ID oj_exclude_end_id;
|
|
339
347
|
extern ID oj_file_id;
|
data/ext/oj/parse.c
CHANGED
|
@@ -15,12 +15,9 @@
|
|
|
15
15
|
#include "mem.h"
|
|
16
16
|
#include "oj.h"
|
|
17
17
|
#include "rxclass.h"
|
|
18
|
+
#include "simd.h"
|
|
18
19
|
#include "val_stack.h"
|
|
19
20
|
|
|
20
|
-
#ifdef OJ_USE_SSE4_2
|
|
21
|
-
#include <nmmintrin.h>
|
|
22
|
-
#endif
|
|
23
|
-
|
|
24
21
|
// Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
|
|
25
22
|
#define OJ_INFINITY (1.0 / 0.0)
|
|
26
23
|
|
|
@@ -183,32 +180,189 @@ static void unicode_to_chars(ParseInfo pi, Buf buf, uint32_t code) {
|
|
|
183
180
|
}
|
|
184
181
|
}
|
|
185
182
|
|
|
183
|
+
static const unsigned char end_of_scan_string[] = {
|
|
184
|
+
// Filled 1 at the positions of '\0', '\\', and '"'
|
|
185
|
+
1, 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, 1, 0, 0,
|
|
186
|
+
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,
|
|
187
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
188
|
+
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,
|
|
189
|
+
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,
|
|
190
|
+
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,
|
|
191
|
+
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,
|
|
192
|
+
};
|
|
186
193
|
static inline const char *scan_string_noSIMD(const char *str, const char *end) {
|
|
187
|
-
for (;
|
|
188
|
-
if (
|
|
194
|
+
for (; str < end; str++) {
|
|
195
|
+
if (end_of_scan_string[(unsigned char)*str]) {
|
|
189
196
|
break;
|
|
190
197
|
}
|
|
191
198
|
}
|
|
192
199
|
return str;
|
|
193
200
|
}
|
|
194
201
|
|
|
195
|
-
#ifdef
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
const
|
|
202
|
+
#ifdef HAVE_SIMD_NEON
|
|
203
|
+
|
|
204
|
+
static inline const char *string_scan_neon(const char *str, const char *end) {
|
|
205
|
+
const uint8x16_t null_char = vdupq_n_u8(0);
|
|
206
|
+
const uint8x16_t backslash = vdupq_n_u8('\\');
|
|
207
|
+
const uint8x16_t quote = vdupq_n_u8('"');
|
|
208
|
+
|
|
209
|
+
while (str + sizeof(uint8x16_t) <= end) {
|
|
210
|
+
uint8x16_t chunk = vld1q_u8((const uint8_t *)str);
|
|
211
|
+
uint8x16_t tmp = vorrq_u8(vorrq_u8(vceqq_u8(chunk, null_char), vceqq_u8(chunk, backslash)),
|
|
212
|
+
vceqq_u8(chunk, quote));
|
|
213
|
+
const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(tmp), 4);
|
|
214
|
+
uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(res), 0);
|
|
215
|
+
if (mask != 0) {
|
|
216
|
+
mask &= 0x8888888888888888ull;
|
|
217
|
+
return str + (OJ_CTZ64(mask) >> 2);
|
|
218
|
+
}
|
|
219
|
+
str += sizeof(uint8x16_t);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return scan_string_noSIMD(str, end);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#endif
|
|
226
|
+
|
|
227
|
+
#ifdef HAVE_SIMD_SSE4_2
|
|
228
|
+
// Optimized SIMD string scanner using SSE4.2 instructions
|
|
229
|
+
// Uses prefetching and processes multiple chunks in parallel to reduce latency
|
|
230
|
+
// Note: OJ_TARGET_SSE42 attribute allows this to compile even without global -msse4.2
|
|
231
|
+
static OJ_TARGET_SSE42 const char *scan_string_SSE42(const char *str, const char *end) {
|
|
232
|
+
static const char chars[16] = "\x00\\\"";
|
|
233
|
+
const __m128i terminate = _mm_loadu_si128((const __m128i *)&chars[0]);
|
|
234
|
+
const char *safe_end_64 = end - 64;
|
|
235
|
+
const char *safe_end_16 = end - 16;
|
|
236
|
+
|
|
237
|
+
// Process 64 bytes at a time with parallel SIMD operations
|
|
238
|
+
// This reduces pipeline stalls and improves instruction-level parallelism
|
|
239
|
+
while (str <= safe_end_64) {
|
|
240
|
+
// Prefetch next cache line for better memory throughput
|
|
241
|
+
OJ_PREFETCH(str + 64);
|
|
242
|
+
|
|
243
|
+
// Load and compare 4 chunks in parallel
|
|
244
|
+
const __m128i chunk0 = _mm_loadu_si128((const __m128i *)(str));
|
|
245
|
+
const __m128i chunk1 = _mm_loadu_si128((const __m128i *)(str + 16));
|
|
246
|
+
const __m128i chunk2 = _mm_loadu_si128((const __m128i *)(str + 32));
|
|
247
|
+
const __m128i chunk3 = _mm_loadu_si128((const __m128i *)(str + 48));
|
|
248
|
+
|
|
249
|
+
const int r0 = _mm_cmpestri(terminate,
|
|
250
|
+
3,
|
|
251
|
+
chunk0,
|
|
252
|
+
16,
|
|
253
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
254
|
+
if (OJ_UNLIKELY(r0 != 16))
|
|
255
|
+
return str + r0;
|
|
256
|
+
|
|
257
|
+
const int r1 = _mm_cmpestri(terminate,
|
|
258
|
+
3,
|
|
259
|
+
chunk1,
|
|
260
|
+
16,
|
|
261
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
262
|
+
if (OJ_UNLIKELY(r1 != 16))
|
|
263
|
+
return str + 16 + r1;
|
|
264
|
+
|
|
265
|
+
const int r2 = _mm_cmpestri(terminate,
|
|
266
|
+
3,
|
|
267
|
+
chunk2,
|
|
268
|
+
16,
|
|
269
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
270
|
+
if (OJ_UNLIKELY(r2 != 16))
|
|
271
|
+
return str + 32 + r2;
|
|
272
|
+
|
|
273
|
+
const int r3 = _mm_cmpestri(terminate,
|
|
274
|
+
3,
|
|
275
|
+
chunk3,
|
|
276
|
+
16,
|
|
277
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
278
|
+
if (OJ_UNLIKELY(r3 != 16))
|
|
279
|
+
return str + 48 + r3;
|
|
280
|
+
|
|
281
|
+
str += 64;
|
|
282
|
+
}
|
|
200
283
|
|
|
201
|
-
|
|
284
|
+
// Handle remaining 16-byte chunks
|
|
285
|
+
for (; str <= safe_end_16; str += 16) {
|
|
202
286
|
const __m128i string = _mm_loadu_si128((const __m128i *)str);
|
|
203
287
|
const int r = _mm_cmpestri(terminate,
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (r != 16)
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
288
|
+
3,
|
|
289
|
+
string,
|
|
290
|
+
16,
|
|
291
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
292
|
+
if (r != 16)
|
|
293
|
+
return str + r;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return scan_string_noSIMD(str, end);
|
|
297
|
+
}
|
|
298
|
+
#endif
|
|
299
|
+
|
|
300
|
+
#ifdef HAVE_SIMD_SSE2
|
|
301
|
+
// Optimized SSE2 string scanner (fallback for older x86_64 CPUs)
|
|
302
|
+
// Uses SSE2 instructions with prefetching and parallel processing
|
|
303
|
+
// Note: OJ_TARGET_SSE2 attribute allows this to compile even without global -msse2
|
|
304
|
+
static OJ_TARGET_SSE2 const char *scan_string_SSE2(const char *str, const char *end) {
|
|
305
|
+
const char *safe_end_64 = end - 64;
|
|
306
|
+
const char *safe_end_16 = end - 16;
|
|
307
|
+
|
|
308
|
+
// Create comparison vectors for our three special characters
|
|
309
|
+
const __m128i null_char = _mm_setzero_si128();
|
|
310
|
+
const __m128i backslash = _mm_set1_epi8('\\');
|
|
311
|
+
const __m128i quote = _mm_set1_epi8('"');
|
|
312
|
+
|
|
313
|
+
// Process 64 bytes at a time for better throughput
|
|
314
|
+
while (str <= safe_end_64) {
|
|
315
|
+
OJ_PREFETCH(str + 64);
|
|
316
|
+
|
|
317
|
+
// Load 4 chunks
|
|
318
|
+
const __m128i chunk0 = _mm_loadu_si128((const __m128i *)(str));
|
|
319
|
+
const __m128i chunk1 = _mm_loadu_si128((const __m128i *)(str + 16));
|
|
320
|
+
const __m128i chunk2 = _mm_loadu_si128((const __m128i *)(str + 32));
|
|
321
|
+
const __m128i chunk3 = _mm_loadu_si128((const __m128i *)(str + 48));
|
|
322
|
+
|
|
323
|
+
// Compare all chunks (allows CPU to parallelize)
|
|
324
|
+
const __m128i cmp0 = _mm_or_si128(
|
|
325
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk0, null_char), _mm_cmpeq_epi8(chunk0, backslash)),
|
|
326
|
+
_mm_cmpeq_epi8(chunk0, quote));
|
|
327
|
+
const __m128i cmp1 = _mm_or_si128(
|
|
328
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk1, null_char), _mm_cmpeq_epi8(chunk1, backslash)),
|
|
329
|
+
_mm_cmpeq_epi8(chunk1, quote));
|
|
330
|
+
const __m128i cmp2 = _mm_or_si128(
|
|
331
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk2, null_char), _mm_cmpeq_epi8(chunk2, backslash)),
|
|
332
|
+
_mm_cmpeq_epi8(chunk2, quote));
|
|
333
|
+
const __m128i cmp3 = _mm_or_si128(
|
|
334
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk3, null_char), _mm_cmpeq_epi8(chunk3, backslash)),
|
|
335
|
+
_mm_cmpeq_epi8(chunk3, quote));
|
|
336
|
+
|
|
337
|
+
// Convert to masks
|
|
338
|
+
int mask0 = _mm_movemask_epi8(cmp0);
|
|
339
|
+
if (OJ_UNLIKELY(mask0 != 0))
|
|
340
|
+
return str + OJ_CTZ(mask0);
|
|
341
|
+
|
|
342
|
+
int mask1 = _mm_movemask_epi8(cmp1);
|
|
343
|
+
if (OJ_UNLIKELY(mask1 != 0))
|
|
344
|
+
return str + 16 + OJ_CTZ(mask1);
|
|
345
|
+
|
|
346
|
+
int mask2 = _mm_movemask_epi8(cmp2);
|
|
347
|
+
if (OJ_UNLIKELY(mask2 != 0))
|
|
348
|
+
return str + 32 + OJ_CTZ(mask2);
|
|
349
|
+
|
|
350
|
+
int mask3 = _mm_movemask_epi8(cmp3);
|
|
351
|
+
if (OJ_UNLIKELY(mask3 != 0))
|
|
352
|
+
return str + 48 + OJ_CTZ(mask3);
|
|
353
|
+
|
|
354
|
+
str += 64;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Handle remaining 16-byte chunks
|
|
358
|
+
for (; str <= safe_end_16; str += 16) {
|
|
359
|
+
const __m128i chunk = _mm_loadu_si128((const __m128i *)str);
|
|
360
|
+
const __m128i matches = _mm_or_si128(
|
|
361
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk, null_char), _mm_cmpeq_epi8(chunk, backslash)),
|
|
362
|
+
_mm_cmpeq_epi8(chunk, quote));
|
|
363
|
+
int mask = _mm_movemask_epi8(matches);
|
|
364
|
+
if (mask != 0)
|
|
365
|
+
return str + OJ_CTZ(mask);
|
|
212
366
|
}
|
|
213
367
|
|
|
214
368
|
return scan_string_noSIMD(str, end);
|
|
@@ -218,16 +372,29 @@ static inline const char *scan_string_SIMD(const char *str, const char *end) {
|
|
|
218
372
|
static const char *(*scan_func)(const char *str, const char *end) = scan_string_noSIMD;
|
|
219
373
|
|
|
220
374
|
void oj_scanner_init(void) {
|
|
221
|
-
|
|
222
|
-
|
|
375
|
+
// Use runtime CPU detection to select the best SIMD implementation
|
|
376
|
+
// This ensures we don't crash on CPUs that don't support SSE4.2
|
|
377
|
+
SIMD_Implementation impl = oj_get_simd_implementation();
|
|
378
|
+
|
|
379
|
+
switch (impl) {
|
|
380
|
+
#ifdef HAVE_SIMD_SSE4_2
|
|
381
|
+
case SIMD_SSE42: scan_func = scan_string_SSE42; break;
|
|
382
|
+
#endif
|
|
383
|
+
#ifdef HAVE_SIMD_SSE2
|
|
384
|
+
case SIMD_SSE2: scan_func = scan_string_SSE2; break;
|
|
223
385
|
#endif
|
|
386
|
+
#ifdef HAVE_SIMD_NEON
|
|
387
|
+
case SIMD_NEON: scan_func = string_scan_neon; break;
|
|
388
|
+
#endif
|
|
389
|
+
default: scan_func = scan_string_noSIMD; break;
|
|
390
|
+
}
|
|
224
391
|
}
|
|
225
392
|
|
|
226
393
|
// entered at /
|
|
227
394
|
static void read_escaped_str(ParseInfo pi, const char *start) {
|
|
228
395
|
struct _buf buf;
|
|
229
396
|
const char *s;
|
|
230
|
-
|
|
397
|
+
size_t cnt = pi->cur - start;
|
|
231
398
|
uint32_t code;
|
|
232
399
|
Val parent = stack_peek(&pi->stack);
|
|
233
400
|
|
|
@@ -444,7 +611,7 @@ static void read_num(ParseInfo pi) {
|
|
|
444
611
|
ni.bigdec_load = pi->options.compat_bigdec;
|
|
445
612
|
} else {
|
|
446
613
|
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load ||
|
|
447
|
-
|
|
614
|
+
RubyDec == pi->options.bigdec_load);
|
|
448
615
|
ni.bigdec_load = pi->options.bigdec_load;
|
|
449
616
|
}
|
|
450
617
|
|
|
@@ -502,7 +669,7 @@ static void read_num(ParseInfo pi) {
|
|
|
502
669
|
// A trailing . is not a valid decimal but if encountered allow it
|
|
503
670
|
// except when mimicking the JSON gem or in strict mode.
|
|
504
671
|
if (StrictMode == pi->options.mode || CompatMode == pi->options.mode) {
|
|
505
|
-
|
|
672
|
+
size_t pos = pi->cur - ni.str;
|
|
506
673
|
|
|
507
674
|
if (1 == pos || (2 == pos && ni.neg)) {
|
|
508
675
|
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number");
|
|
@@ -681,7 +848,7 @@ void oj_parse2(ParseInfo pi) {
|
|
|
681
848
|
pi->cur = pi->json;
|
|
682
849
|
err_init(&pi->err);
|
|
683
850
|
while (1) {
|
|
684
|
-
if (0 < pi->max_depth && pi->max_depth <= pi->stack.tail - pi->stack.head - 1) {
|
|
851
|
+
if (RB_UNLIKELY(0 < pi->max_depth && pi->max_depth <= pi->stack.tail - pi->stack.head - 1)) {
|
|
685
852
|
VALUE err_clas = oj_get_json_err_class("NestingError");
|
|
686
853
|
|
|
687
854
|
oj_set_error_at(pi, err_clas, __FILE__, __LINE__, "Too deeply nested.");
|
|
@@ -689,18 +856,20 @@ void oj_parse2(ParseInfo pi) {
|
|
|
689
856
|
return;
|
|
690
857
|
}
|
|
691
858
|
next_non_white(pi);
|
|
692
|
-
if (
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
859
|
+
if (first) {
|
|
860
|
+
// If no tokens are consumed (i.e. empty string), throw a parse error
|
|
861
|
+
// this is the behavior of JSON.parse in both Ruby and JS.
|
|
862
|
+
if (RB_UNLIKELY('\0' == *pi->cur && No == pi->options.empty_string)) {
|
|
863
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "unexpected character");
|
|
864
|
+
}
|
|
865
|
+
} else {
|
|
866
|
+
if (RB_UNLIKELY('\0' != *pi->cur)) {
|
|
867
|
+
oj_set_error_at(pi,
|
|
868
|
+
oj_parse_error_class,
|
|
869
|
+
__FILE__,
|
|
870
|
+
__LINE__,
|
|
871
|
+
"unexpected characters after the JSON document");
|
|
872
|
+
}
|
|
704
873
|
}
|
|
705
874
|
|
|
706
875
|
switch (*pi->cur++) {
|
|
@@ -761,7 +930,7 @@ void oj_parse2(ParseInfo pi) {
|
|
|
761
930
|
case '\0': pi->cur--; return;
|
|
762
931
|
default: oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "unexpected character"); return;
|
|
763
932
|
}
|
|
764
|
-
if (err_has(&pi->err)) {
|
|
933
|
+
if (RB_UNLIKELY(err_has(&pi->err))) {
|
|
765
934
|
return;
|
|
766
935
|
}
|
|
767
936
|
if (stack_empty(&pi->stack)) {
|
|
@@ -802,6 +971,20 @@ static long double exp_plus[] = {
|
|
|
802
971
|
1.0e39, 1.0e40, 1.0e41, 1.0e42, 1.0e43, 1.0e44, 1.0e45, 1.0e46, 1.0e47, 1.0e48, 1.0e49,
|
|
803
972
|
};
|
|
804
973
|
|
|
974
|
+
static void validate_integer_size(size_t limit, NumInfo ni) {
|
|
975
|
+
size_t digit_count = ni->len - (ni->neg ? 1 : 0);
|
|
976
|
+
|
|
977
|
+
if (digit_count > limit) {
|
|
978
|
+
oj_set_error_at(ni->pi,
|
|
979
|
+
(Qnil != ni->pi->err_class) ? ni->pi->err_class : oj_parse_error_class,
|
|
980
|
+
__FILE__,
|
|
981
|
+
__LINE__,
|
|
982
|
+
"integer exceeds :max_integer_digits (%lu > %lu)",
|
|
983
|
+
(unsigned long)digit_count,
|
|
984
|
+
(unsigned long)limit);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
805
988
|
VALUE
|
|
806
989
|
oj_num_as_value(NumInfo ni) {
|
|
807
990
|
VALUE rnum = Qnil;
|
|
@@ -815,6 +998,12 @@ oj_num_as_value(NumInfo ni) {
|
|
|
815
998
|
} else if (ni->nan) {
|
|
816
999
|
rnum = rb_float_new(0.0 / 0.0);
|
|
817
1000
|
} else if (1 == ni->div && 0 == ni->exp && !ni->has_exp) { // fixnum
|
|
1001
|
+
size_t limit = (NULL != ni->pi) ? ni->pi->options.max_integer_digits : 0;
|
|
1002
|
+
|
|
1003
|
+
if (0 < limit) {
|
|
1004
|
+
validate_integer_size(limit, ni);
|
|
1005
|
+
}
|
|
1006
|
+
|
|
818
1007
|
if (ni->big) {
|
|
819
1008
|
if (256 > ni->len) {
|
|
820
1009
|
char buf[256];
|
|
@@ -1024,11 +1213,19 @@ oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yie
|
|
|
1024
1213
|
buf = OJ_R_ALLOC_N(char, len + 1);
|
|
1025
1214
|
pi->json = buf;
|
|
1026
1215
|
pi->end = buf + len;
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1216
|
+
{
|
|
1217
|
+
size_t total = 0;
|
|
1218
|
+
|
|
1219
|
+
while (total < len) {
|
|
1220
|
+
cnt = read(fd, (char *)pi->json + total, len - total);
|
|
1221
|
+
if (cnt <= 0) {
|
|
1222
|
+
if (0 != buf) {
|
|
1223
|
+
OJ_R_FREE(buf);
|
|
1224
|
+
}
|
|
1225
|
+
rb_raise(rb_eIOError, "failed to read from IO Object.");
|
|
1226
|
+
}
|
|
1227
|
+
total += cnt;
|
|
1030
1228
|
}
|
|
1031
|
-
rb_raise(rb_eIOError, "failed to read from IO Object.");
|
|
1032
1229
|
}
|
|
1033
1230
|
((char *)pi->json)[len] = '\0';
|
|
1034
1231
|
/* skip UTF-8 BOM if present */
|
|
@@ -1109,9 +1306,16 @@ CLEANUP:
|
|
|
1109
1306
|
OJ_R_FREE(json);
|
|
1110
1307
|
}
|
|
1111
1308
|
stack_cleanup(&pi->stack);
|
|
1112
|
-
|
|
1113
|
-
|
|
1309
|
+
// The parsers match against pi->options.str_rx. A :match_string option
|
|
1310
|
+
// builds a chain of its own for this call, which has to be freed. Without
|
|
1311
|
+
// the option the member still aliases the chain owned by the defaults and
|
|
1312
|
+
// must be left alone.
|
|
1313
|
+
if (pi->options.str_rx.head != oj_default_options.str_rx.head) {
|
|
1314
|
+
oj_rxclass_cleanup(&pi->options.str_rx);
|
|
1315
|
+
pi->options.str_rx.head = NULL;
|
|
1316
|
+
pi->options.str_rx.tail = NULL;
|
|
1114
1317
|
}
|
|
1318
|
+
oj_free_call_options(&pi->options);
|
|
1115
1319
|
if (err_has(&pi->err)) {
|
|
1116
1320
|
rb_set_errinfo(Qnil);
|
|
1117
1321
|
if (Qnil != pi->err_class) {
|
|
@@ -1121,12 +1325,12 @@ CLEANUP:
|
|
|
1121
1325
|
// The json gem requires the error message be UTF-8 encoded. In
|
|
1122
1326
|
// additional the complete JSON source must be returned. There
|
|
1123
1327
|
// does not seem to be a size limit.
|
|
1124
|
-
VALUE msg =
|
|
1328
|
+
VALUE msg = rb_utf8_str_new_cstr(pi->err.msg);
|
|
1125
1329
|
VALUE args[1];
|
|
1126
1330
|
|
|
1127
1331
|
if (NULL != pi->json) {
|
|
1128
|
-
msg = rb_str_append(msg,
|
|
1129
|
-
msg = rb_str_append(msg,
|
|
1332
|
+
msg = rb_str_append(msg, rb_utf8_str_new_cstr(" in '"));
|
|
1333
|
+
msg = rb_str_append(msg, rb_utf8_str_new_cstr(pi->json));
|
|
1130
1334
|
}
|
|
1131
1335
|
args[0] = msg;
|
|
1132
1336
|
if (pi->err.clas == oj_parse_error_class) {
|