oj 3.9.1 → 3.16.11

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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +21 -6
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +50 -68
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +9 -36
  10. data/ext/oj/circarray.c +38 -42
  11. data/ext/oj/circarray.h +12 -13
  12. data/ext/oj/code.c +158 -179
  13. data/ext/oj/code.h +20 -22
  14. data/ext/oj/compat.c +145 -205
  15. data/ext/oj/custom.c +740 -880
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1145 -844
  18. data/ext/oj/dump.h +71 -57
  19. data/ext/oj/dump_compat.c +575 -655
  20. data/ext/oj/dump_leaf.c +96 -186
  21. data/ext/oj/dump_object.c +533 -660
  22. data/ext/oj/dump_strict.c +306 -340
  23. data/ext/oj/encode.h +4 -33
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -28
  26. data/ext/oj/err.h +39 -42
  27. data/ext/oj/extconf.rb +28 -7
  28. data/ext/oj/fast.c +1052 -1113
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +471 -430
  34. data/ext/oj/object.c +532 -580
  35. data/ext/oj/odd.c +156 -142
  36. data/ext/oj/odd.h +25 -26
  37. data/ext/oj/oj.c +1346 -961
  38. data/ext/oj/oj.h +307 -290
  39. data/ext/oj/parse.c +954 -858
  40. data/ext/oj/parse.h +74 -72
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +819 -836
  44. data/ext/oj/rails.h +8 -11
  45. data/ext/oj/reader.c +136 -147
  46. data/ext/oj/reader.h +69 -83
  47. data/ext/oj/resolve.c +41 -63
  48. data/ext/oj/resolve.h +4 -6
  49. data/ext/oj/rxclass.c +69 -72
  50. data/ext/oj/rxclass.h +12 -13
  51. data/ext/oj/saj.c +440 -485
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +79 -118
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +739 -709
  57. data/ext/oj/stream_writer.c +141 -175
  58. data/ext/oj/strict.c +103 -128
  59. data/ext/oj/string_writer.c +244 -261
  60. data/ext/oj/trace.c +34 -41
  61. data/ext/oj/trace.h +42 -15
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +107 -107
  65. data/ext/oj/util.h +4 -3
  66. data/ext/oj/val_stack.c +61 -78
  67. data/ext/oj/val_stack.h +80 -114
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +316 -361
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +9 -9
  73. data/lib/oj/error.rb +1 -2
  74. data/lib/oj/json.rb +162 -150
  75. data/lib/oj/mimic.rb +54 -20
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +12 -8
  79. data/lib/oj/version.rb +1 -2
  80. data/lib/oj.rb +2 -8
  81. data/pages/Compatibility.md +1 -1
  82. data/pages/Encoding.md +1 -1
  83. data/pages/InstallOptions.md +20 -0
  84. data/pages/JsonGem.md +15 -0
  85. data/pages/Modes.md +9 -3
  86. data/pages/Options.md +62 -12
  87. data/pages/Parser.md +309 -0
  88. data/pages/Rails.md +73 -22
  89. metadata +68 -192
  90. data/ext/oj/hash.c +0 -163
  91. data/ext/oj/hash.h +0 -46
  92. data/ext/oj/hash_test.c +0 -512
  93. data/test/_test_active.rb +0 -76
  94. data/test/_test_active_mimic.rb +0 -96
  95. data/test/_test_mimic_rails.rb +0 -126
  96. data/test/activerecord/result_test.rb +0 -27
  97. data/test/activesupport4/decoding_test.rb +0 -108
  98. data/test/activesupport4/encoding_test.rb +0 -531
  99. data/test/activesupport4/test_helper.rb +0 -41
  100. data/test/activesupport5/decoding_test.rb +0 -125
  101. data/test/activesupport5/encoding_test.rb +0 -485
  102. data/test/activesupport5/encoding_test_cases.rb +0 -90
  103. data/test/activesupport5/test_helper.rb +0 -50
  104. data/test/activesupport5/time_zone_test_helpers.rb +0 -24
  105. data/test/bar.rb +0 -25
  106. data/test/files.rb +0 -29
  107. data/test/foo.rb +0 -21
  108. data/test/helper.rb +0 -26
  109. data/test/isolated/shared.rb +0 -308
  110. data/test/isolated/test_mimic_after.rb +0 -13
  111. data/test/isolated/test_mimic_alone.rb +0 -12
  112. data/test/isolated/test_mimic_as_json.rb +0 -45
  113. data/test/isolated/test_mimic_before.rb +0 -13
  114. data/test/isolated/test_mimic_define.rb +0 -28
  115. data/test/isolated/test_mimic_rails_after.rb +0 -22
  116. data/test/isolated/test_mimic_rails_before.rb +0 -21
  117. data/test/isolated/test_mimic_redefine.rb +0 -15
  118. data/test/json_gem/json_addition_test.rb +0 -216
  119. data/test/json_gem/json_common_interface_test.rb +0 -148
  120. data/test/json_gem/json_encoding_test.rb +0 -107
  121. data/test/json_gem/json_ext_parser_test.rb +0 -20
  122. data/test/json_gem/json_fixtures_test.rb +0 -35
  123. data/test/json_gem/json_generator_test.rb +0 -383
  124. data/test/json_gem/json_generic_object_test.rb +0 -90
  125. data/test/json_gem/json_parser_test.rb +0 -470
  126. data/test/json_gem/json_string_matching_test.rb +0 -42
  127. data/test/json_gem/test_helper.rb +0 -18
  128. data/test/perf.rb +0 -107
  129. data/test/perf_compat.rb +0 -130
  130. data/test/perf_fast.rb +0 -164
  131. data/test/perf_file.rb +0 -64
  132. data/test/perf_object.rb +0 -138
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -151
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -145
  137. data/test/perf_wab.rb +0 -131
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -54
  151. data/test/sample_json.rb +0 -37
  152. data/test/test_compat.rb +0 -509
  153. data/test/test_custom.rb +0 -503
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -470
  156. data/test/test_file.rb +0 -239
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_integer_range.rb +0 -73
  160. data/test/test_null.rb +0 -376
  161. data/test/test_object.rb +0 -1018
  162. data/test/test_saj.rb +0 -186
  163. data/test/test_scp.rb +0 -433
  164. data/test/test_strict.rb +0 -410
  165. data/test/test_various.rb +0 -741
  166. data/test/test_wab.rb +0 -307
  167. data/test/test_writer.rb +0 -380
  168. data/test/tests.rb +0 -24
  169. data/test/tests_mimic.rb +0 -14
  170. data/test/tests_mimic_addition.rb +0 -7
  171. data/test/zoo.rb +0 -13
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
1
  # [![{}j](http://www.ohler.com/dev/images/oj_comet_64.svg)](http://www.ohler.com/oj) gem
2
2
 
3
- [![Build Status](https://img.shields.io/travis/ohler55/oj/master.svg)](http://travis-ci.org/ohler55/oj?branch=master) [![AppVeyor](https://img.shields.io/appveyor/ci/ohler55/oj/master.svg)](https://ci.appveyor.com/project/ohler55/oj) ![Gem](https://img.shields.io/gem/v/oj.svg) ![Gem](https://img.shields.io/gem/dt/oj.svg) [![SemVer compatibility](https://api.dependabot.com/badges/compatibility_score?dependency-name=oj&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=oj&package-manager=bundler&version-scheme=semver) [![TideLift](https://tidelift.com/badges/github/ohler55/oj)](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
3
+ [![CI](https://github.com/ohler55/oj/actions/workflows/CI.yml/badge.svg)](https://github.com/ohler55/oj/actions/workflows/CI.yml)
4
+ ![Gem](https://img.shields.io/gem/v/oj.svg)
5
+ ![Gem](https://img.shields.io/gem/dt/oj.svg)
6
+ [![TideLift](https://tidelift.com/badges/github/ohler55/oj)](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
4
7
 
5
8
  A *fast* JSON parser and Object marshaller as a Ruby gem.
6
9
 
7
- Version 3.0 is out! 3.0 provides better json gem and Rails compatibility. It
8
- also provides additional optimization options.
10
+ Version 3.13 is out with a much faster parser (`Oj::Parser`) and option isolation.
9
11
 
10
12
  ## Using
11
13
 
@@ -40,9 +42,18 @@ or in Bundler:
40
42
  gem 'oj'
41
43
  ```
42
44
 
45
+ ## Rails and json quickstart
46
+
47
+ See the Quickstart sections of the [Rails](pages/Rails.md) and [json](pages/JsonGem.md) docs.
48
+
49
+ ## multi_json
50
+
51
+ Code which uses [multi_json](https://github.com/intridea/multi_json)
52
+ will automatically prefer Oj if it is installed.
53
+
43
54
  ## Support
44
55
 
45
- [Get supported Oj with a Tidelift Subscription.](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
56
+ [Get supported Oj with a Tidelift Subscription.](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme) Security updates are [supported](https://tidelift.com/security).
46
57
 
47
58
  ## Further Reading
48
59
 
@@ -50,7 +61,7 @@ For more details on options, modes, advanced features, and more follow these
50
61
  links.
51
62
 
52
63
  - [{file:Options.md}](pages/Options.md) for parse and dump options.
53
- - [{file:Modes.md}](pages/Modes.md) for details on modes for strict JSON compliance, mimicing the JSON gem, and mimicing Rails and ActiveSupport behavior.
64
+ - [{file:Modes.md}](pages/Modes.md) for details on modes for strict JSON compliance, mimicking the JSON gem, and mimicking Rails and ActiveSupport behavior.
54
65
  - [{file:JsonGem.md}](pages/JsonGem.md) includes more details on json gem compatibility and use.
55
66
  - [{file:Rails.md}](pages/Rails.md) includes more details on Rails and ActiveSupport compatibility and use.
56
67
  - [{file:Custom.md}](pages/Custom.md) includes more details on Custom mode.
@@ -58,10 +69,11 @@ links.
58
69
  - [{file:Compatibility.md}](pages/Compatibility.md) lists current compatibility with Rubys and Rails.
59
70
  - [{file:Advanced.md}](pages/Advanced.md) for fast parser and marshalling features.
60
71
  - [{file:Security.md}](pages/Security.md) for security considerations.
72
+ - [{file:InstallOptions.md}](pages/InstallOptions.md) for install option.
61
73
 
62
74
  ## Releases
63
75
 
64
- See [{file:CHANGELOG.md}](CHANGELOG.md)
76
+ See [{file:CHANGELOG.md}](CHANGELOG.md) and [{file:RELEASE_NOTES.md}](RELEASE_NOTES.md)
65
77
 
66
78
  ## Links
67
79
 
@@ -97,8 +109,11 @@ Follow [@peterohler on Twitter](http://twitter.com/peterohler) for announcements
97
109
 
98
110
  - *Agoo-C, a high performance C web server supporting GraphQL on GitHub*: https://github.com/ohler55/agoo-c
99
111
 
112
+ - *oj-introspect, an example of creating an Oj parser extension in C*: https://github.com/meinac/oj-introspect
113
+
100
114
  #### Contributing
101
115
 
102
116
  + Provide a Pull Request off the `develop` branch.
103
117
  + Report a bug
104
118
  + Suggest an idea
119
+ + Code is now formatted with the clang-format tool with the configuration file in the root of the repo.
data/RELEASE_NOTES.md ADDED
@@ -0,0 +1,61 @@
1
+ # RELEASE NOTES
2
+
3
+ The release notes here are organized by release. For a list of changes
4
+ see the See [{file:CHANGELOG.md}](CHANGELOG.md) file. In this file are
5
+ the steps to take to aid in keeping things rolling after updating to
6
+ the latest version.
7
+
8
+ ## 3.13.7
9
+
10
+ The default for JSON when mimicked by Oj is now to set
11
+ `:allow_invalid_unicode`. To change that behavior JSON.load, set that
12
+ option to false.
13
+
14
+ ## 3.13.x
15
+
16
+ This release included a new cache that performs better than the
17
+ earlier cache and a new high performance parser.
18
+
19
+ ### Cache
20
+
21
+ The new cache includes a least recently used expiration to reduce
22
+ memory use. The cache is also self adjusting and will expand as needed
23
+ for better performance. It also handles Hash keys and string values
24
+ with two options, `:cache_keys`, a boolean and `:cache_str` an
25
+ integer. The `:cache_str` if set to more than zero is the limit for
26
+ the length of string values to cache. The maximum value is 35 which
27
+ allows strings up to 34 bytes to be cached.
28
+
29
+ One interesting aspect of the cache is not so much the string caching
30
+ which performs similar to the Ruby intern functions but the caching of
31
+ symbols and object attribute names. There is a significant gain for
32
+ symbols and object attributes.
33
+
34
+ If the cache is not desired then setting the default options to turn
35
+ it off can be done with this line:
36
+
37
+ ``` ruby
38
+ Oj.default_options = { cache_keys: false, cache_str: 0 }
39
+ ```
40
+
41
+ ### Oj::Parser
42
+
43
+ The new parser uses a different core that follows the approach taken
44
+ by [OjC](https://github.com/ohler55/ojc) and
45
+ [OjG](https://github.com/ohler55/ojg). It also takes advantage of the
46
+ bulk Array and Hash functions. Another issue the new parser addresses
47
+ is option management. Instead of a single global default_options each
48
+ parser instance maintains it's own options.
49
+
50
+ There is a price to be paid when using the Oj::Parser. The API is not
51
+ the same the older parser. A single parser can only be used in a
52
+ single thread. This allows reuse of internal buffers for additional
53
+ improvements in performance.
54
+
55
+ The performane advantage of the Oj::Parse is that it is more than 3
56
+ times faster than the Oj::compat_load call and 6 times faster than the
57
+ JSON gem.
58
+
59
+ ### Dump Performance
60
+
61
+ Thanks to Watson1978 Oj.dump also received a speed boost.
data/ext/oj/buf.h CHANGED
@@ -1,99 +1,81 @@
1
- /* buf.h
2
- * Copyright (c) 2011, Peter Ohler
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * - Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- *
11
- * - Redistributions in binary form must reproduce the above copyright notice,
12
- * this list of conditions and the following disclaimer in the documentation
13
- * and/or other materials provided with the distribution.
14
- *
15
- * - Neither the name of Peter Ohler nor the names of its contributors may be
16
- * used to endorse or promote products derived from this software without
17
- * specific prior written permission.
18
- *
19
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
30
3
 
31
4
  #ifndef OJ_BUF_H
32
5
  #define OJ_BUF_H
33
6
 
7
+ #include "mem.h"
34
8
  #include "ruby.h"
35
9
 
36
10
  typedef struct _buf {
37
- char *head;
38
- char *end;
39
- char *tail;
40
- char base[1024];
11
+ char *head;
12
+ char *end;
13
+ char *tail;
14
+ char base[1024];
41
15
  } *Buf;
42
16
 
43
- inline static void
44
- buf_init(Buf buf) {
17
+ inline static void buf_init(Buf buf) {
45
18
  buf->head = buf->base;
46
- buf->end = buf->base + sizeof(buf->base) - 1;
19
+ buf->end = buf->base + sizeof(buf->base) - 1;
47
20
  buf->tail = buf->head;
48
21
  }
49
22
 
50
- inline static void
51
- buf_cleanup(Buf buf) {
23
+ inline static void buf_reset(Buf buf) {
24
+ buf->tail = buf->head;
25
+ }
26
+
27
+ inline static void buf_cleanup(Buf buf) {
52
28
  if (buf->base != buf->head) {
53
- xfree(buf->head);
29
+ OJ_R_FREE(buf->head);
54
30
  }
55
31
  }
56
32
 
57
- inline static size_t
58
- buf_len(Buf buf) {
33
+ inline static size_t buf_len(Buf buf) {
59
34
  return buf->tail - buf->head;
60
35
  }
61
36
 
62
- inline static void
63
- buf_append_string(Buf buf, const char *s, size_t slen) {
37
+ inline static const char *buf_str(Buf buf) {
38
+ *buf->tail = '\0';
39
+ return buf->head;
40
+ }
41
+
42
+ inline static void buf_append_string(Buf buf, const char *s, size_t slen) {
43
+ if (0 == slen) {
44
+ return;
45
+ }
46
+
64
47
  if (buf->end <= buf->tail + slen) {
65
- size_t len = buf->end - buf->head;
66
- size_t toff = buf->tail - buf->head;
67
- size_t new_len = len + slen + len / 2;
48
+ size_t len = buf->end - buf->head;
49
+ size_t toff = buf->tail - buf->head;
50
+ size_t new_len = len + slen + len / 2;
68
51
 
69
- if (buf->base == buf->head) {
70
- buf->head = ALLOC_N(char, new_len);
71
- memcpy(buf->head, buf->base, len);
72
- } else {
73
- REALLOC_N(buf->head, char, new_len);
74
- }
75
- buf->tail = buf->head + toff;
76
- buf->end = buf->head + new_len - 1;
52
+ if (buf->base == buf->head) {
53
+ buf->head = OJ_R_ALLOC_N(char, new_len);
54
+ memcpy(buf->head, buf->base, len);
55
+ } else {
56
+ OJ_R_REALLOC_N(buf->head, char, new_len);
57
+ }
58
+ buf->tail = buf->head + toff;
59
+ buf->end = buf->head + new_len - 1;
77
60
  }
78
61
  memcpy(buf->tail, s, slen);
79
62
  buf->tail += slen;
80
63
  }
81
-
82
- inline static void
83
- buf_append(Buf buf, char c) {
64
+
65
+ inline static void buf_append(Buf buf, char c) {
84
66
  if (buf->end <= buf->tail) {
85
- size_t len = buf->end - buf->head;
86
- size_t toff = buf->tail - buf->head;
87
- size_t new_len = len + len / 2;
67
+ size_t len = buf->end - buf->head;
68
+ size_t toff = buf->tail - buf->head;
69
+ size_t new_len = len + len / 2;
88
70
 
89
- if (buf->base == buf->head) {
90
- buf->head = ALLOC_N(char, new_len);
91
- memcpy(buf->head, buf->base, len);
92
- } else {
93
- REALLOC_N(buf->head, char, new_len);
94
- }
95
- buf->tail = buf->head + toff;
96
- buf->end = buf->head + new_len - 1;
71
+ if (buf->base == buf->head) {
72
+ buf->head = OJ_R_ALLOC_N(char, new_len);
73
+ memcpy(buf->head, buf->base, len);
74
+ } else {
75
+ OJ_R_REALLOC_N(buf->head, char, new_len);
76
+ }
77
+ buf->tail = buf->head + toff;
78
+ buf->end = buf->head + new_len - 1;
97
79
  }
98
80
  *buf->tail = c;
99
81
  buf->tail++;
data/ext/oj/cache.c ADDED
@@ -0,0 +1,329 @@
1
+ // Copyright (c) 2011, 2021 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #if HAVE_PTHREAD_MUTEX_INIT
5
+ #include <pthread.h>
6
+ #endif
7
+ #include <stdlib.h>
8
+
9
+ #include "cache.h"
10
+ #include "mem.h"
11
+
12
+ // The stdlib calloc, realloc, and free are used instead of the Ruby ALLOC,
13
+ // ALLOC_N, REALLOC, and xfree since the later could trigger a GC which will
14
+ // either corrupt memory or if the mark function locks will deadlock.
15
+
16
+ #define REHASH_LIMIT 4
17
+ #define MIN_SHIFT 8
18
+ #define REUSE_MAX 8192
19
+
20
+ #if HAVE_PTHREAD_MUTEX_INIT
21
+ #define CACHE_LOCK(c) pthread_mutex_lock(&((c)->mutex))
22
+ #define CACHE_UNLOCK(c) pthread_mutex_unlock(&((c)->mutex))
23
+ #else
24
+ #define CACHE_LOCK(c) rb_mutex_lock((c)->mutex)
25
+ #define CACHE_UNLOCK(c) rb_mutex_unlock((c)->mutex)
26
+ #endif
27
+
28
+ // almost the Murmur hash algorithm
29
+ #define M 0x5bd1e995
30
+
31
+ typedef struct _slot {
32
+ struct _slot *next;
33
+ VALUE val;
34
+ uint64_t hash;
35
+ volatile uint32_t use_cnt;
36
+ uint8_t klen;
37
+ char key[CACHE_MAX_KEY];
38
+ } *Slot;
39
+
40
+ typedef struct _cache {
41
+ volatile Slot *slots;
42
+ volatile size_t cnt;
43
+ VALUE (*form)(const char *str, size_t len);
44
+ uint64_t size;
45
+ uint64_t mask;
46
+ VALUE (*intern)(struct _cache *c, const char *key, size_t len);
47
+ volatile Slot reuse;
48
+ size_t rcnt;
49
+ #if HAVE_PTHREAD_MUTEX_INIT
50
+ pthread_mutex_t mutex;
51
+ #else
52
+ VALUE mutex;
53
+ #endif
54
+ uint8_t xrate;
55
+ bool mark;
56
+ } *Cache;
57
+
58
+ void cache_set_form(Cache c, VALUE (*form)(const char *str, size_t len)) {
59
+ c->form = form;
60
+ }
61
+
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;
84
+ }
85
+ if (key < end) {
86
+ h ^= *key;
87
+ }
88
+ h *= M;
89
+ h ^= h >> 13;
90
+ h *= M;
91
+ h ^= h >> 15;
92
+
93
+ return h;
94
+ }
95
+
96
+ static void rehash(Cache c) {
97
+ uint64_t osize;
98
+ Slot *end;
99
+ Slot *sp;
100
+
101
+ osize = c->size;
102
+ c->size = osize * 4;
103
+ c->mask = c->size - 1;
104
+ c->slots = OJ_REALLOC((void *)c->slots, sizeof(Slot) * c->size);
105
+ memset((Slot *)c->slots + osize, 0, sizeof(Slot) * osize * 3);
106
+ end = (Slot *)c->slots + osize;
107
+ for (sp = (Slot *)c->slots; sp < end; sp++) {
108
+ Slot s = *sp;
109
+ Slot next = NULL;
110
+
111
+ *sp = NULL;
112
+ for (; NULL != s; s = next) {
113
+ uint64_t h = s->hash & c->mask;
114
+ Slot *bucket = (Slot *)c->slots + h;
115
+
116
+ next = s->next;
117
+ s->next = *bucket;
118
+ *bucket = s;
119
+ }
120
+ }
121
+ }
122
+
123
+ static VALUE lockless_intern(Cache c, const char *key, size_t len) {
124
+ uint64_t h = hash_calc((const uint8_t *)key, len);
125
+ Slot *bucket = (Slot *)c->slots + (h & c->mask);
126
+ Slot b;
127
+ volatile VALUE rkey;
128
+
129
+ while (REUSE_MAX < c->rcnt) {
130
+ if (NULL != (b = c->reuse)) {
131
+ c->reuse = b->next;
132
+ OJ_FREE(b);
133
+ c->rcnt--;
134
+ } else {
135
+ // An accounting error occured somewhere so correct it.
136
+ c->rcnt = 0;
137
+ }
138
+ }
139
+ for (b = *bucket; NULL != b; b = b->next) {
140
+ if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
141
+ b->use_cnt += 16;
142
+ return b->val;
143
+ }
144
+ }
145
+ rkey = c->form(key, len);
146
+ if (NULL == (b = c->reuse)) {
147
+ b = OJ_CALLOC(1, sizeof(struct _slot));
148
+ } else {
149
+ c->reuse = b->next;
150
+ c->rcnt--;
151
+ }
152
+ b->hash = h;
153
+ memcpy(b->key, key, len);
154
+ b->klen = (uint8_t)len;
155
+ b->key[len] = '\0';
156
+ b->val = rkey;
157
+ b->use_cnt = 4;
158
+ b->next = *bucket;
159
+ *bucket = b;
160
+ c->cnt++; // Don't worry about wrapping. Worse case is the entry is removed and recreated.
161
+ if (REHASH_LIMIT < c->cnt / c->size) {
162
+ rehash(c);
163
+ }
164
+ return rkey;
165
+ }
166
+
167
+ static VALUE locking_intern(Cache c, const char *key, size_t len) {
168
+ uint64_t h;
169
+ Slot *bucket;
170
+ Slot b;
171
+ uint64_t old_size;
172
+ volatile VALUE rkey;
173
+
174
+ CACHE_LOCK(c);
175
+ while (REUSE_MAX < c->rcnt) {
176
+ if (NULL != (b = c->reuse)) {
177
+ c->reuse = b->next;
178
+ OJ_FREE(b);
179
+ c->rcnt--;
180
+ } else {
181
+ // An accounting error occured somewhere so correct it.
182
+ c->rcnt = 0;
183
+ }
184
+ }
185
+ h = hash_calc((const uint8_t *)key, len);
186
+ bucket = (Slot *)c->slots + (h & c->mask);
187
+ for (b = *bucket; NULL != b; b = b->next) {
188
+ if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
189
+ b->use_cnt += 4;
190
+ CACHE_UNLOCK(c);
191
+
192
+ return b->val;
193
+ }
194
+ }
195
+ old_size = c->size;
196
+ // The creation of a new value may trigger a GC which be a problem if the
197
+ // cache is locked so make sure it is unlocked for the key value creation.
198
+ if (NULL != (b = c->reuse)) {
199
+ c->reuse = b->next;
200
+ c->rcnt--;
201
+ }
202
+ CACHE_UNLOCK(c);
203
+ if (NULL == b) {
204
+ b = OJ_CALLOC(1, sizeof(struct _slot));
205
+ }
206
+ rkey = c->form(key, len);
207
+ b->hash = h;
208
+ memcpy(b->key, key, len);
209
+ b->klen = (uint8_t)len;
210
+ b->key[len] = '\0';
211
+ b->val = rkey;
212
+ b->use_cnt = 16;
213
+
214
+ // Lock again to add the new entry.
215
+ CACHE_LOCK(c);
216
+ if (old_size != c->size) {
217
+ h = hash_calc((const uint8_t *)key, len);
218
+ bucket = (Slot *)c->slots + (h & c->mask);
219
+ }
220
+ b->next = *bucket;
221
+ *bucket = b;
222
+ c->cnt++; // Don't worry about wrapping. Worse case is the entry is removed and recreated.
223
+ if (REHASH_LIMIT < c->cnt / c->size) {
224
+ rehash(c);
225
+ }
226
+ CACHE_UNLOCK(c);
227
+
228
+ return rkey;
229
+ }
230
+
231
+ Cache cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking) {
232
+ Cache c = OJ_CALLOC(1, sizeof(struct _cache));
233
+ int shift = 0;
234
+
235
+ for (; REHASH_LIMIT < size; size /= 2, shift++) {
236
+ }
237
+ if (shift < MIN_SHIFT) {
238
+ shift = MIN_SHIFT;
239
+ }
240
+ #if HAVE_PTHREAD_MUTEX_INIT
241
+ pthread_mutex_init(&c->mutex, NULL);
242
+ #else
243
+ c->mutex = rb_mutex_new();
244
+ #endif
245
+ c->size = 1 << shift;
246
+ c->mask = c->size - 1;
247
+ c->slots = OJ_CALLOC(c->size, sizeof(Slot));
248
+ c->form = form;
249
+ c->xrate = 1; // low
250
+ c->mark = mark;
251
+ if (locking) {
252
+ c->intern = locking_intern;
253
+ } else {
254
+ c->intern = lockless_intern;
255
+ }
256
+ return c;
257
+ }
258
+
259
+ void cache_set_expunge_rate(Cache c, int rate) {
260
+ c->xrate = (uint8_t)rate;
261
+ }
262
+
263
+ void cache_free(void *data) {
264
+ Cache c = (Cache)data;
265
+ uint64_t i;
266
+
267
+ for (i = 0; i < c->size; i++) {
268
+ Slot next;
269
+ Slot s;
270
+
271
+ for (s = c->slots[i]; NULL != s; s = next) {
272
+ next = s->next;
273
+ OJ_FREE(s);
274
+ }
275
+ }
276
+ OJ_FREE((void *)c->slots);
277
+ OJ_FREE(c);
278
+ }
279
+
280
+ void cache_mark(void *data) {
281
+ Cache c = (Cache)data;
282
+ uint64_t i;
283
+
284
+ #if !HAVE_PTHREAD_MUTEX_INIT
285
+ rb_gc_mark(c->mutex);
286
+ #endif
287
+ if (0 == c->cnt) {
288
+ return;
289
+ }
290
+ for (i = 0; i < c->size; i++) {
291
+ Slot s;
292
+ Slot prev = NULL;
293
+ Slot next;
294
+
295
+ for (s = c->slots[i]; NULL != s; s = next) {
296
+ next = s->next;
297
+ if (0 == s->use_cnt) {
298
+ if (NULL == prev) {
299
+ c->slots[i] = next;
300
+ } else {
301
+ prev->next = next;
302
+ }
303
+ c->cnt--;
304
+ s->next = c->reuse;
305
+ c->reuse = s;
306
+ c->rcnt++;
307
+ continue;
308
+ }
309
+ switch (c->xrate) {
310
+ case 0: break;
311
+ case 2: s->use_cnt -= 2; break;
312
+ case 3: s->use_cnt /= 2; break;
313
+ default: s->use_cnt--; break;
314
+ }
315
+ if (c->mark) {
316
+ rb_gc_mark(s->val);
317
+ }
318
+ prev = s;
319
+ }
320
+ }
321
+ }
322
+
323
+ VALUE
324
+ cache_intern(Cache c, const char *key, size_t len) {
325
+ if (CACHE_MAX_KEY <= len) {
326
+ return c->form(key, len);
327
+ }
328
+ return c->intern(c, key, len);
329
+ }
data/ext/oj/cache.h ADDED
@@ -0,0 +1,22 @@
1
+ // Copyright (c) 2021 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #ifndef CACHE_H
5
+ #define CACHE_H
6
+
7
+ #include <ruby.h>
8
+ #include <stdbool.h>
9
+
10
+ #define CACHE_MAX_KEY 35
11
+
12
+ struct _cache;
13
+ typedef struct _cache *Cache;
14
+
15
+ extern struct _cache *cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
16
+ extern void cache_free(void *data);
17
+ extern void cache_mark(void *data);
18
+ extern void cache_set_form(struct _cache *c, VALUE (*form)(const char *str, size_t len));
19
+ extern VALUE cache_intern(struct _cache *c, const char *key, size_t len);
20
+ extern void cache_set_expunge_rate(struct _cache *c, int rate);
21
+
22
+ #endif /* CACHE_H */