oj 3.17.3 → 3.17.8

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -0
  3. data/ext/oj/cache.c +72 -39
  4. data/ext/oj/cache.h +5 -0
  5. data/ext/oj/circarray.c +6 -2
  6. data/ext/oj/code.c +38 -28
  7. data/ext/oj/code.h +2 -0
  8. data/ext/oj/compat.c +63 -31
  9. data/ext/oj/custom.c +43 -8
  10. data/ext/oj/dump.c +120 -41
  11. data/ext/oj/dump.h +10 -0
  12. data/ext/oj/dump_compat.c +19 -15
  13. data/ext/oj/dump_object.c +38 -12
  14. data/ext/oj/dump_strict.c +2 -1
  15. data/ext/oj/extconf.rb +7 -0
  16. data/ext/oj/fast.c +132 -72
  17. data/ext/oj/intern.c +80 -77
  18. data/ext/oj/mimic_json.c +5 -29
  19. data/ext/oj/object.c +47 -17
  20. data/ext/oj/odd.c +6 -2
  21. data/ext/oj/oj.c +393 -44
  22. data/ext/oj/oj.h +10 -3
  23. data/ext/oj/parse.c +75 -7
  24. data/ext/oj/parse.h +23 -2
  25. data/ext/oj/parser.c +219 -71
  26. data/ext/oj/parser.h +1 -1
  27. data/ext/oj/rails.c +188 -45
  28. data/ext/oj/reader.c +22 -6
  29. data/ext/oj/reader.h +1 -0
  30. data/ext/oj/resolve.c +16 -6
  31. data/ext/oj/resolve.h +9 -0
  32. data/ext/oj/rxclass.c +16 -0
  33. data/ext/oj/rxclass.h +1 -0
  34. data/ext/oj/safe.c +62 -48
  35. data/ext/oj/safe.h +14 -2
  36. data/ext/oj/saj.c +60 -17
  37. data/ext/oj/saj2.c +44 -28
  38. data/ext/oj/simd.h +5 -3
  39. data/ext/oj/sparse.c +23 -6
  40. data/ext/oj/stream_writer.c +19 -1
  41. data/ext/oj/strict.c +42 -4
  42. data/ext/oj/string_writer.c +40 -10
  43. data/ext/oj/usual.c +17 -5
  44. data/ext/oj/util.c +27 -0
  45. data/ext/oj/util.h +2 -1
  46. data/ext/oj/val_stack.c +11 -22
  47. data/ext/oj/val_stack.h +70 -30
  48. data/ext/oj/wab.c +17 -26
  49. data/lib/oj/generic_object.rb +82 -0
  50. data/lib/oj/json.rb +1 -71
  51. data/lib/oj/mimic.rb +0 -1
  52. data/lib/oj/version.rb +1 -1
  53. data/pages/Compatibility.md +1 -1
  54. data/pages/Custom.md +2 -3
  55. data/pages/InstallOptions.md +8 -6
  56. data/pages/Modes.md +1 -1
  57. data/pages/Options.md +5 -6
  58. data/pages/Parser.md +2 -2
  59. data/pages/Rails.md +6 -0
  60. data/pages/Security.md +21 -7
  61. metadata +17 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7458dcdf494ef6b1b283ca86d51fba0b3102ecf5cec13f43682878c01708c80
4
- data.tar.gz: ab8099b8b275aa5acab45a012bbc96ef0c860e041be2c4ae7d6f7cd331da1755
3
+ metadata.gz: 887bd6056f15f0ed95de347bc87d12c00c2de9a071b972f16be14952c408f6a6
4
+ data.tar.gz: af830ec844053ce4c955ac193260d53f5edf88159038c30e5c047b8fd9b334d7
5
5
  SHA512:
6
- metadata.gz: 60d445fd27bbea120359c21d0c1b0ac1d4fbbd678c8fd28efe620706d382a2f0cc434645967c9bac0b6d08d1065fe99dc4b15e2623d98ca0e15a61eb591a993b
7
- data.tar.gz: b7736fd3a7b27f98ef3e4df05464000849403ff33a3f82ecd6d8c51b40d0c2753d23ed14c078eb26506d504de991e35ebe68e5703225c80aab6ebbd28b7a80dc
6
+ metadata.gz: 1aa3a234183cfd1e86b6a2b08f91ec553a6048405042ca887647e43198a08ab06287329389bb0c0d4f6c73a637eff158e29da7f187ae4778ebab7379984c771d
7
+ data.tar.gz: 6e1604f05a6b48364549b350c9b18c4e9b1c5dd819881d82e172ee466ce9c10ff49fbb60309f56ec951ce99f0c2791ca31b94a12e23ebef2bde40c0d44a0a8c4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,151 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 3.17.8 - 2026-11-03
4
+
5
+ ### Fixed
6
+
7
+ - Fixed issue #988 by no longer requiring `ostruct` when Oj is loaded, autoloading `JSON::GenericObject` instead. (#1113)
8
+ - Always include `cpuid.h` and fix the cache typedef for older compilers. (#1112)
9
+ - Keep the sign of `-0.0` when dumping in compat and rails modes. (#1115)
10
+ - Fixed big negative integers and big decimals in `Oj::Parser.usual`. (#1116)
11
+ - Reject a value after a number and whitespace in `Oj::Parser`. (#1117)
12
+ - Combine an escaped surrogate pair in `Oj::Parser`. (#1118)
13
+ - Reject an object close after a colon in `Oj::Parser`. (#1119)
14
+ - Accept an exponent right after a zero integer part in `Oj::Parser`. (#1121)
15
+ - Raise for an incomplete top-level number in `Oj::Parser`. (#1122)
16
+ - Reject unescaped control characters in strict and compat mode strings. (#1123)
17
+ - Fixed memory and handle leaks in `Oj.load` with recent winpthreads on Windows. (#1125)
18
+ - Fixed writer state after an excess pop. (#1126)
19
+ - Fixed memory corruption when `^u` names a class that is not a Struct. (#1127)
20
+ - Fixed cached keys and strings containing NUL returning a different value. (#1129)
21
+ - Fixed a segmentation fault on a `create_id` class path through a non-module constant. (#1130)
22
+ - Fixed a segmentation fault with the `Oj::Parser` class cache after GC compaction. (#1131)
23
+ - Fixed a use-after-free of converted non-UTF-8 input in `Oj.load`. (#1132)
24
+ - Fixed a use-after-free of converted non-UTF-8 strings in `Oj.dump`. (#1133)
25
+ - Fixed a segmentation fault when `Oj::Parser#parse` is given a non-String. (#1134)
26
+ - Fixed a segmentation fault when `Oj.safe_load` is given a null StringIO. (#1135)
27
+ - Fixed a segmentation fault and memory leak in `Oj.saj_parse` with IO input. (#1136)
28
+ - Fixed a segmentation fault when `to_json` returns a non-String in custom mode. (#1137)
29
+ - Fixed a segmentation fault when an exception message is not a String in compat mode. (#1138)
30
+ - Fixed a segmentation fault when `raw_json` returns a non-String. (#1139)
31
+ - Fixed a use-after-free of keys in the `Oj::Parser` saj delegate. (#1140)
32
+ - Fixed wrong keys and `IndexError` for `Oj::Parser` saj handlers with only some callbacks. (#1141)
33
+ - Fixed a buffer overflow when dumping nested arrays with a String indent. (#1142)
34
+ - Fixed a segmentation fault when `array_class` is not an Array in compat and custom mode. (#1143)
35
+ - Fixed a double free when closing an `Oj::Doc` inside the open block. (#1144)
36
+ - Fixed buffer leaks and unchecked file sizes when reading special files. (#1145)
37
+ - Fixed reading freed memory when `Oj.load` parses an object through `to_s`. (#1146)
38
+ - Fixed reading a freed buffer when a `StringWriter` is pushed into itself. (#1147)
39
+ - Fixed a segmentation fault and use-after-free when using a closed `Oj::Doc`. (#1148)
40
+ - Fixed reading and writing a different file when a path contains a NUL byte. (#1149)
41
+ - Fixed buffer leaks when a dump to a String, file, or stream raises. (#1152)
42
+ - Fixed unknown class names filling the class cache and loading as a Hash. (#1153)
43
+ - Fixed object mode ranges and exceptions on Ruby 4.1. (#1154)
44
+ - Fixed a segmentation fault after `Oj::Doc#move` fails on a path with `..`. (#1155)
45
+ - Fixed a segmentation fault in wab mode after GC compaction. (#1156)
46
+ - Fixed OpenStruct dumps and loads changing after GC compaction. (#1157)
47
+ - Fixed a segmentation fault when pushing to a writer with `circular: true`. (#1158)
48
+ - Fixed exception loading and Range subclass dumps in custom mode on Ruby 4.1. (#1159)
49
+
50
+ ### Performance
51
+
52
+ - Speed up parsing of object keys that collide in the key cache by using SipHash. (#1150)
53
+ - Speed up dumping ASCII-only strings that are not UTF-8. (#1160)
54
+ - Speed up dumping objects in object and custom modes. (#1161)
55
+ - Speed up dumping plain objects in custom mode. (#1162)
56
+
57
+ ## 3.17.7 - 2026-09-29
58
+
59
+ ### Fixed
60
+ - issue #1104, where `Oj.dump` in compat and custom mode forwarded its whole options hash to the `to_json` methods it calls, so with json 3.0, whose generator raises `ArgumentError` on options it does not know, dumping a `Time` or any other object whose `to_json` comes from the json gem failed with `unknown keywords: mode, use_to_json`. Nested values reach `to_json` even without `use_to_json`, so `Oj.dump(data, mode: :compat)` failed the same way for data holding a `BigDecimal` or `Time`. The options Oj acts on are now dropped from the hash `to_json` receives, except `:only` and `:except`, which ActiveSupport's `as_json` reads. Keys Oj does not know are passed through untouched.
61
+ - Speed up hash-heavy documents: skip the 11KB per-parse memset and bulk-insert hashes (#1096)
62
+ - Preserve decimal placement for large parser numbers (#1099)
63
+ - Keep zero digits in oversized parser exponents (#1100)
64
+ - Parse very small usual-mode numbers correctly (#1101)
65
+ - Speed up symbol keys by interning without a temporary String (#1102)
66
+ - Keep Oj's own options out of the hash passed to to_json (#1106)
67
+ - Ensure class-cache mutex is released on exceptions (#1108)
68
+ - Guard __has_include with an #ifdef to support older GCC versions (4.4.7 etc.) (#1111)
69
+
70
+ ## 3.17.6 - 2026-08-10
71
+
72
+ - Fixed issue #1092, where the encoder ActiveSupport 8.1 caches with `escape: false` froze the options as they stood before `set_encoder` wrote `time_precision` into them, so `to_json(escape: false)` emitted 9 fractional digits. An options hash that names no option Oj knows no longer detaches an encoder from the defaults. (#1093)
73
+
74
+ ## 3.17.5 - 2026-07-31
75
+
76
+ Most of this release comes out of a security review of the C extension. There are no API changes.
77
+
78
+ ### Memory safety
79
+
80
+ - Fixed hash flooding by seeding the key hash per process. The hash was decided entirely at compile time, so a document of colliding keys turned key interning into O(n^2) and left the process slow for every later parse. (#1053)
81
+ - Fixed a one byte buffer over-read on a document that ends with a backslash inside an unterminated string. (#1054)
82
+ - Fixed two length underflows that made an empty `^m` value and a key of exactly 65,536 bytes read forward until they left mapped memory. (#1055)
83
+ - Fixed a SEGV from type confusion in class path resolution. A name segment that resolved to something other than a class or module was dereferenced as one. (#1056)
84
+ - Fixed uninitialized stack memory being read into the message of an unresolved class name error. (#1057)
85
+ - Fixed output buffer overflows when writing indentation, where several callers of `fill_indent()` had not assured the buffer first. (#1058)
86
+ - Fixed the `Oj::Doc` path depth limit being checked one level too late. (#1061)
87
+ - Fixed the use-after-free of a key when the value stack grows. (#1062)
88
+ - Fixed the buffer over-read when a document ends where a key belongs. (#1063)
89
+ - Fixed the id from a circular reference being used unchecked. (#1064)
90
+ - Fixed the SEGV dumping an exception that was never raised, where the missing backtrace was read as an Array. (#1068)
91
+ - Fixed the stack buffer overflow writing the error location. (#1071)
92
+ - Fixed a read error in `Oj::Parser#file` being taken as a read of `SIZE_MAX` bytes, which wrote in front of the buffer and then reparsed it forever without checking for interrupts. (#1072)
93
+ - Fixed a `:float_format` too long for the 64 byte buffer copying stack bytes past the end of it into the output. (#1073)
94
+ - Fixed the double free of the default `create_id` when a call passes `:create_id => nil`. (#1074)
95
+ - Fixed the buffer over-read of an odd attribute name and the leak of its arguments. (#1075)
96
+ - Fixed the SEGV reading a truncated document with `Oj::Parser#file` and the descriptor it leaked. (#1076)
97
+ - Fixed the SEGV from a malformed number in `Oj::Doc`. (#1077)
98
+ - Fixed the stack exhaustion from a long key with `:only` or `:except`. (#1078)
99
+ - Fixed the odd module match being decided by uninitialized memory. (#1079)
100
+ - Fixed `:float_format` accepting directives it has no argument for. (#1083)
101
+
102
+ ### Memory leaks
103
+
104
+ - Fixed the leak of an escaped hash key in the streaming parser. (#1067)
105
+ - Fixed the leak of the parser `create_id` when it is set a second time. (#1085)
106
+ - Fixed the leak of the parser when `Oj::Parser.new` or `Oj::Parser.safe` raises on an argument it does not take. (#1086)
107
+
108
+ ### Parsing and dumping
109
+
110
+ - Fixed a safe parser limit of 4 being ignored. (#1065)
111
+ - Fixed the exponent bound being checked after it is narrowed. (#1066)
112
+ - Fixed a document being rejected when it opens with a number of 4093 digits or more read through a file descriptor or an IO, and the reader buffer that leaked when it grew. (#1069)
113
+ - Fixed the safe parser limits being dropped when an option is set after the parser is built. (#1070)
114
+ - Fixed the `:custom` mode dump of an exception that was never raised opening with `{,`. (#1082)
115
+ - Fixed the `:wab` mode time load being an offset out on Windows, where `mktime()` stood in for `timegm()` and returned -1 for any time before the epoch. (#1088)
116
+ - Fixed `Oj::Doc` dropping the exponent of a number written without a decimal point, so `1e3` loaded as `1`. (#1089)
117
+
118
+ ### Rails
119
+
120
+ - Fixed a `to_json(only:)` or `to_json(except:)` dropping the `include_root_in_json` wrapper key and the keys added by `:methods`. (#1039)
121
+ - Fixed the `as_json` options reaching only the first element of a container, so `to_json(only:)`, `to_json(methods:)` and any custom `as_json(opts)` took effect on one row and nowhere else. (#1040)
122
+ - An encoder now reads the global optimized class table and options instead of copying them, saving about 6.7KB of malloc per `ActiveSupport::JSON.encode`, and picks up later `Oj.default_options` and `ActiveSupport::JSON::Encoding` changes. (#1049)
123
+ - Fixed `as_json` being handed the encoder's own options hash. ActiveSupport allows `as_json` to write into it, and ActiveSupport 8.1 caches one encoder per process, so a single such `as_json` could leave `:only` set for every later `to_json`. (#1050)
124
+
125
+ ### Documentation
126
+
127
+ - Renamed the documented `:skip_null_byte` option to `:omit_null_byte`, which is the name that works. (#1041)
128
+ - Dropped the `--with-sse42` install option from InstallOptions.md. SIMD support is detected at runtime. (#1042)
129
+ - Corrected the documented minimum Ruby version from 2.4+ to 2.7+. (#1043)
130
+ - Corrected the Parser.md benchmark conclusions to reference `:usual` rather than `:saj`. (#1044)
131
+ - Corrected Custom.md and Options.md, which called `:custom` the default mode. The default is `:object`. (#1045)
132
+ - Corrected the documented type of the `:safe` option from String to Boolean. (#1046)
133
+ - Corrected Security.md, which said no methods are ever called on an Object created by a load. `exception`, `set_backtrace` and `replace` are. The page now also says which mode `Oj.load` uses. (#1080)
134
+
135
+ ### Tests and CI
136
+
137
+ - Added the ActiveSupport 8 test suite. The Rakefile picked the suite by major version, so the rails_8 job had been running zero ActiveSupport tests. (#1047)
138
+ - Added the ActiveSupport 8.1 test suite and pinned the #985 regression. (#1051)
139
+ - Fixed `rake` never running the `test/test_*.rb` suite. The `Rake::TestTask` that defined `test` was commented out, so `test_all` invoked a synthesized no-op file task. (#1084)
140
+ - Added a job timeout to the workflows. (#1087)
141
+ - Removed the pre-1970 time test skips on Windows, which were left from a 32 bit Ruby limit that no longer applies. (#1090)
142
+
143
+ ## 3.17.4 - 2026-07-14
144
+
145
+ - Fixed issue in fast.c where rescuing from a panic did not reset the where counter.
146
+
147
+ - Multiple fixes thanks to @Watson1978.
148
+
3
149
  ## 3.17.3 - 2026-06-04
4
150
 
5
151
  - Fixed issue in intern.c and fast.c.
data/ext/oj/cache.c CHANGED
@@ -25,8 +25,42 @@
25
25
  #define CACHE_UNLOCK(c) rb_mutex_unlock((c)->mutex)
26
26
  #endif
27
27
 
28
- // almost the Murmur hash algorithm
29
- #define M 0x5bd1e995
28
+ #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
29
+ #define SIPROUND \
30
+ do { \
31
+ v0 += v1; \
32
+ v1 = ROTL(v1, 13); \
33
+ v1 ^= v0; \
34
+ v0 = ROTL(v0, 32); \
35
+ v2 += v3; \
36
+ v3 = ROTL(v3, 16); \
37
+ v3 ^= v2; \
38
+ v0 += v3; \
39
+ v3 = ROTL(v3, 21); \
40
+ v3 ^= v0; \
41
+ v2 += v1; \
42
+ v1 = ROTL(v1, 17); \
43
+ v1 ^= v2; \
44
+ v2 = ROTL(v2, 32); \
45
+ } while (0)
46
+
47
+ static uint64_t hash_key[2];
48
+
49
+ void oj_hash_seed_init(void) {
50
+ static bool seeded = false;
51
+ VALUE seed;
52
+ VALUE mask;
53
+
54
+ if (seeded) {
55
+ return;
56
+ }
57
+ seed = rb_funcall(rb_cRandom, rb_intern("new_seed"), 0);
58
+ mask = ULL2NUM(UINT64_MAX);
59
+ hash_key[0] = NUM2ULL(rb_funcall(seed, rb_intern("&"), 1, mask)) ^ (uint64_t)rb_memhash("oj0", 3);
60
+ hash_key[1] = NUM2ULL(rb_funcall(rb_funcall(seed, rb_intern(">>"), 1, INT2FIX(64)), rb_intern("&"), 1, mask)) ^
61
+ (uint64_t)rb_memhash("oj1", 3);
62
+ seeded = true;
63
+ }
30
64
 
31
65
  typedef struct _slot {
32
66
  struct _slot *next;
@@ -37,7 +71,7 @@ typedef struct _slot {
37
71
  char key[CACHE_MAX_KEY];
38
72
  } *Slot;
39
73
 
40
- typedef struct _cache {
74
+ struct _cache {
41
75
  volatile Slot *slots;
42
76
  volatile size_t cnt;
43
77
  VALUE (*form)(const char *str, size_t len);
@@ -53,44 +87,43 @@ typedef struct _cache {
53
87
  #endif
54
88
  uint8_t xrate;
55
89
  bool mark;
56
- } *Cache;
90
+ };
57
91
 
58
92
  void cache_set_form(Cache c, VALUE (*form)(const char *str, size_t len)) {
59
93
  c->form = form;
60
94
  }
61
95
 
62
- static uint64_t hash_calc(const uint8_t *key, size_t len) {
63
- const uint8_t *end = key + len;
64
- const uint8_t *endless = key + (len & 0xFFFFFFFC);
65
- uint64_t h = (uint64_t)len;
66
- uint64_t k;
67
-
68
- while (key < endless) {
69
- k = (uint64_t)*key++;
70
- k |= (uint64_t)*key++ << 8;
71
- k |= (uint64_t)*key++ << 16;
72
- k |= (uint64_t)*key++ << 24;
73
-
74
- k *= M;
75
- k ^= k >> 24;
76
- h *= M;
77
- h ^= k * M;
78
- }
79
- if (1 < end - key) {
80
- uint16_t k16 = (uint16_t)*key++;
81
-
82
- k16 |= (uint16_t)*key++ << 8;
83
- h ^= k16 << 8;
96
+ uint64_t oj_hash_calc(const uint8_t *key, size_t len) {
97
+ const uint8_t *end = key + (len & ~(size_t)7);
98
+ uint64_t v0 = 0x736f6d6570736575ULL ^ hash_key[0];
99
+ uint64_t v1 = 0x646f72616e646f6dULL ^ hash_key[1];
100
+ uint64_t v2 = 0x6c7967656e657261ULL ^ hash_key[0];
101
+ uint64_t v3 = 0x7465646279746573ULL ^ hash_key[1];
102
+ uint64_t b = (uint64_t)len << 56;
103
+ uint64_t m;
104
+ size_t i;
105
+
106
+ for (; key < end; key += 8) {
107
+ m = 0;
108
+ for (i = 0; i < 8; i++) {
109
+ m |= (uint64_t)key[i] << (8 * i);
110
+ }
111
+ v3 ^= m;
112
+ SIPROUND;
113
+ v0 ^= m;
84
114
  }
85
- if (key < end) {
86
- h ^= *key;
115
+ for (i = 0; i < (len & 7); i++) {
116
+ b |= (uint64_t)key[i] << (8 * i);
87
117
  }
88
- h *= M;
89
- h ^= h >> 13;
90
- h *= M;
91
- h ^= h >> 15;
92
-
93
- return h;
118
+ v3 ^= b;
119
+ SIPROUND;
120
+ v0 ^= b;
121
+ v2 ^= 0xff;
122
+ SIPROUND;
123
+ SIPROUND;
124
+ SIPROUND;
125
+
126
+ return v0 ^ v1 ^ v2 ^ v3;
94
127
  }
95
128
 
96
129
  static void rehash(Cache c) {
@@ -121,7 +154,7 @@ static void rehash(Cache c) {
121
154
  }
122
155
 
123
156
  static VALUE lockless_intern(Cache c, const char *key, size_t len) {
124
- uint64_t h = hash_calc((const uint8_t *)key, len);
157
+ uint64_t h = oj_hash_calc((const uint8_t *)key, len);
125
158
  Slot *bucket = (Slot *)c->slots + (h & c->mask);
126
159
  Slot b;
127
160
  volatile VALUE rkey;
@@ -137,7 +170,7 @@ static VALUE lockless_intern(Cache c, const char *key, size_t len) {
137
170
  }
138
171
  }
139
172
  for (b = *bucket; NULL != b; b = b->next) {
140
- if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
173
+ if ((uint8_t)len == b->klen && 0 == memcmp(b->key, key, len)) {
141
174
  b->use_cnt += 16;
142
175
  return b->val;
143
176
  }
@@ -182,10 +215,10 @@ static VALUE locking_intern(Cache c, const char *key, size_t len) {
182
215
  c->rcnt = 0;
183
216
  }
184
217
  }
185
- h = hash_calc((const uint8_t *)key, len);
218
+ h = oj_hash_calc((const uint8_t *)key, len);
186
219
  bucket = (Slot *)c->slots + (h & c->mask);
187
220
  for (b = *bucket; NULL != b; b = b->next) {
188
- if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
221
+ if ((uint8_t)len == b->klen && 0 == memcmp(b->key, key, len)) {
189
222
  b->use_cnt += 4;
190
223
  CACHE_UNLOCK(c);
191
224
 
@@ -214,7 +247,7 @@ static VALUE locking_intern(Cache c, const char *key, size_t len) {
214
247
  // Lock again to add the new entry.
215
248
  CACHE_LOCK(c);
216
249
  if (old_size != c->size) {
217
- h = hash_calc((const uint8_t *)key, len);
250
+ h = oj_hash_calc((const uint8_t *)key, len);
218
251
  bucket = (Slot *)c->slots + (h & c->mask);
219
252
  }
220
253
  b->next = *bucket;
data/ext/oj/cache.h CHANGED
@@ -6,9 +6,14 @@
6
6
 
7
7
  #include <ruby.h>
8
8
  #include <stdbool.h>
9
+ #include <stdint.h>
9
10
 
10
11
  #define CACHE_MAX_KEY 35
11
12
 
13
+ extern uint64_t oj_hash_calc(const uint8_t *key, size_t len);
14
+
15
+ extern void oj_hash_seed_init(void);
16
+
12
17
  struct _cache;
13
18
  typedef struct _cache *Cache;
14
19
 
data/ext/oj/circarray.c CHANGED
@@ -25,8 +25,12 @@ void oj_circ_array_free(CircArray ca) {
25
25
  OJ_R_FREE(ca);
26
26
  }
27
27
 
28
+ // Objects are numbered in encounter order starting at 1, so the only id that
29
+ // can extend the array is one past the highest already seen. Anything further
30
+ // is not a reference to an object the document can contain and must not be
31
+ // used to size an allocation.
28
32
  void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
29
- if (0 < id && 0 != ca) {
33
+ if (0 != ca && 0 < id && id <= ca->cnt + 1) {
30
34
  unsigned long i;
31
35
 
32
36
  if (ca->size < id) {
@@ -57,7 +61,7 @@ VALUE
57
61
  oj_circ_array_get(CircArray ca, unsigned long id) {
58
62
  VALUE obj = Qnil;
59
63
 
60
- if (id <= ca->cnt && 0 != ca) {
64
+ if (0 != ca && 0 < id && id <= ca->cnt) {
61
65
  obj = ca->objs[id - 1];
62
66
  }
63
67
  return obj;
data/ext/oj/code.c CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  #include "code.h"
5
5
 
6
+ #include <string.h>
7
+
6
8
  #include "dump.h"
7
9
 
8
10
  inline static VALUE resolve_classname(VALUE mod, const char *classname) {
@@ -45,18 +47,44 @@ static VALUE path2class(const char *name) {
45
47
  return resolve_classname(clas, class_name);
46
48
  }
47
49
 
50
+ // Resolves the class lazily and returns Qundef while it is not defined, as
51
+ // OpenStruct is not until ostruct is required. A miss is checked again on
52
+ // every call so a class loaded later is found by whichever dump comes next,
53
+ // through any entry point. For a top-level name that check is a single
54
+ // constant table probe, so it costs about the same as a cached miss would.
55
+ VALUE oj_code_class(Code c) {
56
+ if (Qnil != c->clas && Qundef != c->clas) {
57
+ return c->clas;
58
+ }
59
+ if (0 == c->id && NULL == strstr(c->name, "::")) {
60
+ c->id = rb_intern(c->name);
61
+ }
62
+ if (0 == c->id) {
63
+ c->clas = path2class(c->name);
64
+ } else if (rb_const_defined_at(rb_cObject, c->id)) {
65
+ c->clas = rb_const_get_at(rb_cObject, c->id);
66
+ } else {
67
+ c->clas = Qundef;
68
+ }
69
+ if (Qnil != c->clas && Qundef != c->clas) {
70
+ rb_gc_register_address(&c->clas);
71
+ }
72
+ return c->clas;
73
+ }
74
+
75
+ inline static bool code_is(Code c, VALUE clas) {
76
+ if (Qnil != c->clas && Qundef != c->clas) {
77
+ return clas == c->clas;
78
+ }
79
+ return clas == oj_code_class(c);
80
+ }
81
+
48
82
  bool oj_code_dump(Code codes, VALUE obj, int depth, Out out) {
49
83
  VALUE clas = rb_obj_class(obj);
50
84
  Code c = codes;
51
85
 
52
86
  for (; NULL != c->name; c++) {
53
- if (Qundef == c->clas) { // indicates not defined
54
- continue;
55
- }
56
- if (Qnil == c->clas) {
57
- c->clas = path2class(c->name);
58
- }
59
- if (clas == c->clas && c->active) {
87
+ if (c->active && code_is(c, clas)) {
60
88
  c->encode(obj, depth, out);
61
89
  return true;
62
90
  }
@@ -69,13 +97,7 @@ oj_code_load(Code codes, VALUE clas, VALUE args) {
69
97
  Code c = codes;
70
98
 
71
99
  for (; NULL != c->name; c++) {
72
- if (Qundef == c->clas) { // indicates not defined
73
- continue;
74
- }
75
- if (Qnil == c->clas) {
76
- c->clas = path2class(c->name);
77
- }
78
- if (clas == c->clas) {
100
+ if (code_is(c, clas)) {
79
101
  if (NULL == c->decode) {
80
102
  break;
81
103
  }
@@ -89,13 +111,7 @@ void oj_code_set_active(Code codes, VALUE clas, bool active) {
89
111
  Code c = codes;
90
112
 
91
113
  for (; NULL != c->name; c++) {
92
- if (Qundef == c->clas) { // indicates not defined
93
- continue;
94
- }
95
- if (Qnil == c->clas) {
96
- c->clas = path2class(c->name);
97
- }
98
- if (clas == c->clas || Qnil == clas) {
114
+ if (Qnil == clas || code_is(c, clas)) {
99
115
  c->active = active;
100
116
  if (Qnil != clas) {
101
117
  break;
@@ -108,13 +124,7 @@ bool oj_code_has(Code codes, VALUE clas, bool encode) {
108
124
  Code c = codes;
109
125
 
110
126
  for (; NULL != c->name; c++) {
111
- if (Qundef == c->clas) { // indicates not defined
112
- continue;
113
- }
114
- if (Qnil == c->clas) {
115
- c->clas = path2class(c->name);
116
- }
117
- if (clas == c->clas) {
127
+ if (code_is(c, clas)) {
118
128
  if (encode) {
119
129
  return c->active && NULL != c->encode;
120
130
  } else {
data/ext/oj/code.h CHANGED
@@ -17,6 +17,7 @@ typedef struct _code {
17
17
  EncodeFunc encode;
18
18
  DecodeFunc decode;
19
19
  bool active; // For compat mode.
20
+ ID id; // Interned name, set on first lookup of a top-level class.
20
21
  } *Code;
21
22
 
22
23
  // Used by encode functions.
@@ -28,6 +29,7 @@ typedef struct _attr {
28
29
  VALUE time;
29
30
  } *Attr;
30
31
 
32
+ extern VALUE oj_code_class(Code c);
31
33
  extern bool oj_code_dump(Code codes, VALUE obj, int depth, Out out);
32
34
  extern VALUE oj_code_load(Code codes, VALUE clas, VALUE args);
33
35
  extern void oj_code_set_active(Code codes, VALUE clas, bool active);
data/ext/oj/compat.c CHANGED
@@ -14,11 +14,11 @@
14
14
 
15
15
  static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
16
16
  const char *key = kval->key;
17
- int klen = kval->klen;
17
+ size_t klen = kval->klen;
18
18
  Val parent = stack_peek(&pi->stack);
19
19
 
20
20
  if (Qundef == kval->key_val && Yes == pi->options.create_ok && NULL != pi->options.create_id &&
21
- *pi->options.create_id == *key && (int)pi->options.create_id_len == klen &&
21
+ *pi->options.create_id == *key && (size_t)pi->options.create_id_len == klen &&
22
22
  0 == strncmp(pi->options.create_id, key, klen)) {
23
23
  parent->classname = oj_strndup(str, len);
24
24
  parent->clen = len;
@@ -33,7 +33,10 @@ static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, c
33
33
  rstr = rb_funcall(clas, oj_json_create_id, 1, rstr);
34
34
  }
35
35
  }
36
- if (rb_cHash != rb_obj_class(parent->val)) {
36
+ if (Qnil == pi->options.hash_class) {
37
+ stack_pair_push(&pi->stack, rkey, rstr);
38
+ parent->pcnt += 2;
39
+ } else if (rb_cHash != rb_obj_class(parent->val)) {
37
40
  // The rb_hash_set would still work but the unit tests for the
38
41
  // json gem require the less efficient []= method be called to set
39
42
  // values. Even using the store method to set the values will fail
@@ -47,20 +50,34 @@ static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, c
47
50
  }
48
51
 
49
52
  static VALUE start_hash(ParseInfo pi) {
50
- volatile VALUE h;
51
-
52
53
  if (Qnil != pi->options.hash_class) {
53
- h = rb_class_new_instance(0, NULL, pi->options.hash_class);
54
- } else {
55
- h = rb_hash_new();
54
+ return rb_class_new_instance(0, NULL, pi->options.hash_class);
56
55
  }
57
56
  TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
58
- return h;
57
+ // The Hash is built at end_hash from the buffered pairs.
58
+ return Qundef;
59
59
  }
60
60
 
61
61
  static void end_hash(struct _parseInfo *pi) {
62
62
  Val parent = stack_peek(&pi->stack);
63
63
 
64
+ if (Qnil == pi->options.hash_class) {
65
+ ValStack stack = &pi->stack;
66
+ size_t cnt = parent->pcnt;
67
+ volatile VALUE h;
68
+
69
+ #if HAVE_RB_HASH_NEW_CAPA
70
+ h = rb_hash_new_capa((long)(cnt / 2));
71
+ #else
72
+ h = rb_hash_new();
73
+ #endif
74
+ if (0 < cnt) {
75
+ rb_hash_bulk_insert((long)cnt, stack->pairs + stack->pcnt - cnt, h);
76
+ stack->pcnt -= cnt;
77
+ parent->pcnt = 0;
78
+ }
79
+ parent->val = h;
80
+ }
64
81
  if (0 != parent->classname) {
65
82
  volatile VALUE clas;
66
83
 
@@ -103,7 +120,10 @@ static void add_num(ParseInfo pi, NumInfo ni) {
103
120
  static void hash_set_num(struct _parseInfo *pi, Val parent, NumInfo ni) {
104
121
  volatile VALUE rval = oj_num_as_value(ni);
105
122
 
106
- if (rb_cHash != rb_obj_class(parent->val)) {
123
+ if (Qnil == pi->options.hash_class) {
124
+ stack_pair_push(&pi->stack, oj_calc_hash_key(pi, parent), rval);
125
+ parent->pcnt += 2;
126
+ } else if (rb_cHash != rb_obj_class(parent->val)) {
107
127
  // The rb_hash_set would still work but the unit tests for the
108
128
  // json gem require the less efficient []= method be called to set
109
129
  // values. Even using the store method to set the values will fail
@@ -116,7 +136,10 @@ static void hash_set_num(struct _parseInfo *pi, Val parent, NumInfo ni) {
116
136
  }
117
137
 
118
138
  static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
119
- if (rb_cHash != rb_obj_class(parent->val)) {
139
+ if (Qnil == pi->options.hash_class) {
140
+ stack_pair_push(&pi->stack, oj_calc_hash_key(pi, parent), value);
141
+ parent->pcnt += 2;
142
+ } else if (rb_cHash != rb_obj_class(parent->val)) {
120
143
  // The rb_hash_set would still work but the unit tests for the
121
144
  // json gem require the less efficient []= method be called to set
122
145
  // values. Even using the store method to set the values will fail
@@ -136,18 +159,21 @@ static VALUE start_array(ParseInfo pi) {
136
159
  return rb_ary_new();
137
160
  }
138
161
 
162
+ void oj_compat_array_append_other(VALUE a, VALUE value) {
163
+ if (rb_cArray == rb_obj_class(a)) {
164
+ rb_ary_push(a, value);
165
+ } else {
166
+ // The array_class may not be an Array, and the json gem calls << even
167
+ // for Array subclasses.
168
+ rb_funcall(a, rb_intern("<<"), 1, value);
169
+ }
170
+ }
171
+
139
172
  static void array_append_num(ParseInfo pi, NumInfo ni) {
140
173
  Val parent = stack_peek(&pi->stack);
141
174
  volatile VALUE rval = oj_num_as_value(ni);
142
175
 
143
- if (!oj_use_array_alt && rb_cArray != rb_obj_class(parent->val)) {
144
- // The rb_ary_push would still work but the unit tests for the json
145
- // gem require the less efficient << method be called to push the
146
- // values.
147
- rb_funcall(parent->val, rb_intern("<<"), 1, rval);
148
- } else {
149
- rb_ary_push(parent->val, rval);
150
- }
176
+ oj_compat_array_append(parent->val, rval);
151
177
  TRACE_PARSE_CALL(pi->options.trace, "append_number", pi, rval);
152
178
  }
153
179
 
@@ -158,26 +184,32 @@ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const c
158
184
  VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
159
185
 
160
186
  if (Qnil != clas) {
161
- rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
187
+ oj_compat_array_append(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
162
188
  return;
163
189
  }
164
190
  }
165
- rb_ary_push(stack_peek(&pi->stack)->val, rstr);
191
+ oj_compat_array_append(stack_peek(&pi->stack)->val, rstr);
166
192
  TRACE_PARSE_CALL(pi->options.trace, "append_string", pi, rstr);
167
193
  }
168
194
 
195
+ static void array_append_value(ParseInfo pi, VALUE value) {
196
+ oj_compat_array_append(stack_peek(&pi->stack)->val, value);
197
+ TRACE_PARSE_CALL(pi->options.trace, "append_value", pi, value);
198
+ }
199
+
169
200
  void oj_set_compat_callbacks(ParseInfo pi) {
170
201
  oj_set_strict_callbacks(pi);
171
- pi->start_hash = start_hash;
172
- pi->end_hash = end_hash;
173
- pi->hash_set_cstr = hash_set_cstr;
174
- pi->hash_set_num = hash_set_num;
175
- pi->hash_set_value = hash_set_value;
176
- pi->add_num = add_num;
177
- pi->add_cstr = add_cstr;
178
- pi->array_append_cstr = array_append_cstr;
179
- pi->start_array = start_array;
180
- pi->array_append_num = array_append_num;
202
+ pi->start_hash = start_hash;
203
+ pi->end_hash = end_hash;
204
+ pi->hash_set_cstr = hash_set_cstr;
205
+ pi->hash_set_num = hash_set_num;
206
+ pi->hash_set_value = hash_set_value;
207
+ pi->add_num = add_num;
208
+ pi->add_cstr = add_cstr;
209
+ pi->array_append_cstr = array_append_cstr;
210
+ pi->array_append_value = array_append_value;
211
+ pi->start_array = start_array;
212
+ pi->array_append_num = array_append_num;
181
213
  }
182
214
 
183
215
  VALUE