oj 3.11.5 → 3.16.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1421 -0
- data/README.md +19 -5
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +20 -6
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +10 -9
- data/ext/oj/circarray.c +8 -6
- data/ext/oj/circarray.h +2 -2
- data/ext/oj/code.c +19 -33
- data/ext/oj/code.h +2 -2
- data/ext/oj/compat.c +27 -77
- data/ext/oj/custom.c +86 -179
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +256 -249
- data/ext/oj/dump.h +26 -12
- data/ext/oj/dump_compat.c +565 -642
- data/ext/oj/dump_leaf.c +17 -63
- data/ext/oj/dump_object.c +65 -187
- data/ext/oj/dump_strict.c +27 -51
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +2 -13
- data/ext/oj/err.h +24 -8
- data/ext/oj/extconf.rb +21 -6
- data/ext/oj/fast.c +149 -149
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +121 -106
- data/ext/oj/object.c +85 -162
- data/ext/oj/odd.c +89 -67
- data/ext/oj/odd.h +15 -15
- data/ext/oj/oj.c +542 -411
- data/ext/oj/oj.h +99 -73
- data/ext/oj/parse.c +175 -187
- data/ext/oj/parse.h +26 -24
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +101 -0
- data/ext/oj/rails.c +112 -159
- data/ext/oj/rails.h +1 -1
- data/ext/oj/reader.c +11 -14
- data/ext/oj/reader.h +4 -2
- data/ext/oj/resolve.c +5 -24
- data/ext/oj/rxclass.c +7 -6
- data/ext/oj/rxclass.h +1 -1
- data/ext/oj/saj.c +22 -33
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +5 -28
- data/ext/oj/sparse.c +28 -72
- data/ext/oj/stream_writer.c +50 -40
- data/ext/oj/strict.c +56 -61
- data/ext/oj/string_writer.c +72 -39
- data/ext/oj/trace.h +31 -4
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.h +1 -1
- data/ext/oj/val_stack.c +14 -3
- data/ext/oj/val_stack.h +8 -7
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +63 -88
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +7 -1
- data/lib/oj/easy_hash.rb +4 -5
- data/lib/oj/error.rb +1 -2
- data/lib/oj/json.rb +162 -150
- data/lib/oj/mimic.rb +9 -7
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +12 -8
- data/lib/oj/version.rb +1 -2
- data/lib/oj.rb +2 -0
- data/pages/Compatibility.md +1 -1
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +8 -3
- data/pages/Options.md +43 -5
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +14 -2
- data/test/_test_active.rb +8 -9
- data/test/_test_active_mimic.rb +7 -8
- data/test/_test_mimic_rails.rb +17 -20
- data/test/activerecord/result_test.rb +5 -6
- data/test/activesupport6/encoding_test.rb +63 -28
- data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
- data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
- data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
- data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
- data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
- data/test/files.rb +15 -15
- data/test/foo.rb +16 -45
- data/test/helper.rb +11 -8
- data/test/isolated/shared.rb +3 -2
- data/test/json_gem/json_addition_test.rb +2 -2
- data/test/json_gem/json_common_interface_test.rb +8 -6
- data/test/json_gem/json_encoding_test.rb +0 -0
- data/test/json_gem/json_ext_parser_test.rb +1 -0
- data/test/json_gem/json_fixtures_test.rb +3 -2
- data/test/json_gem/json_generator_test.rb +56 -38
- data/test/json_gem/json_generic_object_test.rb +11 -11
- data/test/json_gem/json_parser_test.rb +54 -47
- data/test/json_gem/json_string_matching_test.rb +9 -9
- data/test/json_gem/test_helper.rb +7 -3
- data/test/mem.rb +34 -0
- data/test/perf.rb +22 -27
- data/test/perf_compat.rb +31 -33
- data/test/perf_dump.rb +50 -0
- data/test/perf_fast.rb +80 -82
- data/test/perf_file.rb +27 -29
- data/test/perf_object.rb +65 -69
- data/test/perf_once.rb +59 -0
- data/test/perf_parser.rb +183 -0
- data/test/perf_saj.rb +46 -54
- data/test/perf_scp.rb +58 -69
- data/test/perf_simple.rb +41 -39
- data/test/perf_strict.rb +74 -82
- data/test/perf_wab.rb +67 -69
- data/test/prec.rb +5 -5
- data/test/sample/change.rb +0 -1
- data/test/sample/dir.rb +0 -1
- data/test/sample/doc.rb +0 -1
- data/test/sample/file.rb +0 -1
- data/test/sample/group.rb +0 -1
- data/test/sample/hasprops.rb +0 -1
- data/test/sample/layer.rb +0 -1
- data/test/sample/rect.rb +0 -1
- data/test/sample/shape.rb +0 -1
- data/test/sample/text.rb +0 -1
- data/test/sample.rb +16 -16
- data/test/sample_json.rb +8 -8
- data/test/test_compat.rb +95 -43
- data/test/test_custom.rb +73 -51
- data/test/test_debian.rb +7 -10
- data/test/test_fast.rb +135 -79
- data/test/test_file.rb +41 -30
- data/test/test_gc.rb +16 -5
- data/test/test_generate.rb +5 -5
- data/test/test_hash.rb +5 -5
- data/test/test_integer_range.rb +9 -9
- data/test/test_null.rb +20 -20
- data/test/test_object.rb +99 -96
- data/test/test_parser.rb +11 -0
- data/test/test_parser_debug.rb +27 -0
- data/test/test_parser_saj.rb +337 -0
- data/test/test_parser_usual.rb +251 -0
- data/test/test_rails.rb +2 -2
- data/test/test_saj.rb +10 -8
- data/test/test_scp.rb +37 -39
- data/test/test_strict.rb +40 -32
- data/test/test_various.rb +165 -84
- data/test/test_wab.rb +48 -44
- data/test/test_writer.rb +47 -47
- data/test/tests.rb +13 -5
- data/test/tests_mimic.rb +12 -3
- data/test/tests_mimic_addition.rb +12 -3
- metadata +74 -128
- data/ext/oj/hash.c +0 -131
- data/ext/oj/hash.h +0 -19
- data/ext/oj/hash_test.c +0 -491
- data/test/activesupport4/decoding_test.rb +0 -108
- data/test/activesupport4/encoding_test.rb +0 -531
- data/test/activesupport4/test_helper.rb +0 -41
- data/test/activesupport5/test_helper.rb +0 -72
- data/test/bar.rb +0 -35
- data/test/baz.rb +0 -16
- data/test/zoo.rb +0 -13
data/pages/Options.md
CHANGED
@@ -66,17 +66,35 @@ Determines how to load decimals.
|
|
66
66
|
|
67
67
|
- `:auto` the most precise for the number of digits is used.
|
68
68
|
|
69
|
+
- `:fast` faster conversion to Float.
|
70
|
+
|
71
|
+
- `:ruby` convert to Float using the Ruby `to_f` conversion.
|
72
|
+
|
69
73
|
This can also be set with `:decimal_class` when used as a load or
|
70
74
|
parse option to match the JSON gem. In that case either `Float`,
|
71
75
|
`BigDecimal`, or `nil` can be provided.
|
72
76
|
|
73
|
-
### :
|
77
|
+
### :cache_keys [Boolean]
|
74
78
|
|
75
|
-
|
79
|
+
If true Hash keys are cached or interned. There are trade-offs with
|
80
|
+
caching keys. Large caches will use more memory and in extreme cases
|
81
|
+
(like over a million) the cache may be slower than not using
|
82
|
+
it. Repeated parsing of similar JSON docs is where cache_keys shines
|
83
|
+
especially with symbol keys.
|
76
84
|
|
77
|
-
|
85
|
+
There is a maximum length for cached keys. Any key longer than 34
|
86
|
+
bytes is not cached. Everything still works but the key is not cached.
|
78
87
|
|
79
|
-
|
88
|
+
### :cache_strings [Int]
|
89
|
+
|
90
|
+
Shorter strings can be cached for better performance. A limit,
|
91
|
+
cache_strings, defines the upper limit on what strings are cached. As
|
92
|
+
with cached keys only strings less than 35 bytes are cached even if
|
93
|
+
the limit is set higher. Setting the limit to zero effectively
|
94
|
+
disables the caching of string values.
|
95
|
+
|
96
|
+
Note that caching for strings is for string values and not Hash keys
|
97
|
+
or Object attributes.
|
80
98
|
|
81
99
|
### :circular [Boolean]
|
82
100
|
|
@@ -90,6 +108,14 @@ recreate the looped references on load.
|
|
90
108
|
Cache classes for faster parsing. This option should not be used if
|
91
109
|
dynamically modifying classes or reloading classes then don't use this.
|
92
110
|
|
111
|
+
### :compat_bigdecimal [Boolean]
|
112
|
+
|
113
|
+
Determines how to load decimals when in `:compat` mode.
|
114
|
+
|
115
|
+
- `true` convert all decimal numbers to BigDecimal.
|
116
|
+
|
117
|
+
- `false` convert all decimal numbers to Float.
|
118
|
+
|
93
119
|
### :create_additions
|
94
120
|
|
95
121
|
A flag indicating that the :create_id key, when encountered during parsing,
|
@@ -132,6 +158,8 @@ Determines the characters to escape when dumping. Only the :ascii and
|
|
132
158
|
|
133
159
|
- `:json` follows the JSON specification. This is the default mode.
|
134
160
|
|
161
|
+
- `:slash` escapes `/` characters.
|
162
|
+
|
135
163
|
- `:xss_safe` escapes HTML and XML characters such as `&` and `<`.
|
136
164
|
|
137
165
|
- `:ascii` escapes all non-ascii or characters with the hi-bit set.
|
@@ -237,6 +265,10 @@ to true.
|
|
237
265
|
|
238
266
|
The number of digits after the decimal when dumping the seconds of time.
|
239
267
|
|
268
|
+
### :skip_null_byte [Boolean]
|
269
|
+
|
270
|
+
If true, null bytes in strings will be omitted when dumping.
|
271
|
+
|
240
272
|
### :space
|
241
273
|
|
242
274
|
String inserted after the ':' character when dumping a JSON object. The
|
@@ -251,7 +283,13 @@ compatibility. Using just indent as an integer gives better performance.
|
|
251
283
|
|
252
284
|
### :symbol_keys [Boolean]
|
253
285
|
|
254
|
-
Use symbols instead of strings for hash keys.
|
286
|
+
Use symbols instead of strings for hash keys.
|
287
|
+
|
288
|
+
### :symbolize_names [Boolean]
|
289
|
+
|
290
|
+
Like :symbol_keys has keys are made into symbols but only when
|
291
|
+
mimicking the JSON gem and then only as the JSON gem honors it so
|
292
|
+
JSON.parse honors the option but JSON.load does not.
|
255
293
|
|
256
294
|
### :trace
|
257
295
|
|
data/pages/Parser.md
ADDED
@@ -0,0 +1,309 @@
|
|
1
|
+
# How Oj Just Got Faster
|
2
|
+
|
3
|
+
The original Oj parser is a performant parser that supports several
|
4
|
+
modes. As of this writing Oj is almost 10 years old. A dinosaur by
|
5
|
+
coding standards. It was time for an upgrade. Dealing with issues over
|
6
|
+
the years it became clear that a few things could have been done
|
7
|
+
better. The new `Oj::Parser` is a response that not only attempts to
|
8
|
+
address some of the issues but also give the Oj parser a significant
|
9
|
+
boost in performance. `Oj::Parser` takes a different approach to JSON
|
10
|
+
parsing than the now legacy Oj parser. Not really a legacy parser yet
|
11
|
+
since the `Oj::Parser` is not a drop-in replacement for the JSON gem
|
12
|
+
but it is as much 3 times or more faster than the previous parser in
|
13
|
+
some modes.
|
14
|
+
|
15
|
+
## Address Issues
|
16
|
+
|
17
|
+
There are a few features of the`Oj.load` parser that continue to be
|
18
|
+
the reason for many of the issue on the project. The most significant
|
19
|
+
area is compatibility with both Rails and the JSON gem as they battle
|
20
|
+
it out for which behavior will win out in any particular
|
21
|
+
situation. Most of the issues are on the writing or dumping side of
|
22
|
+
the JSON packages but some are present on the parsing as
|
23
|
+
well. Conversion of decimals is one area where the Rails and the JSON
|
24
|
+
gem vary. The `Oj::Parser` addresses this by allowing for completely
|
25
|
+
separate parser instances. Create a parser and configure it for the
|
26
|
+
situation and leave the others parsers on their own.
|
27
|
+
|
28
|
+
The `Oj::Parser` is mostly compatible with the JSON gem and Rails but
|
29
|
+
no claims are made that the behavior will be the same as either.
|
30
|
+
|
31
|
+
The most frequent issues that can addressed with the new parser are
|
32
|
+
around the handling of options. For `Oj.load` there is a set of
|
33
|
+
default options that can be set and the same options can be specified
|
34
|
+
for each call to parse or load. This approach as a couple of
|
35
|
+
downsides. One the defaults are shared across all calls to parse no
|
36
|
+
matter what the desire mode is. The second is that having to provide
|
37
|
+
all the options on each parse call incurs a performance penalty and is
|
38
|
+
just annoying to repeat the same set of options over may calls.
|
39
|
+
|
40
|
+
By localizing options to a specific parser instance there is never any
|
41
|
+
bleed over to other instances.
|
42
|
+
|
43
|
+
## How
|
44
|
+
|
45
|
+
It's wonderful to wish for a faster parser that solves all the
|
46
|
+
annoyances of the previous parser but how was it done is a much more
|
47
|
+
interesting question to answer.
|
48
|
+
|
49
|
+
At the core, the API for parsing was changed. Instead of a sinle
|
50
|
+
global parser any number of parsers can be created and each is separate
|
51
|
+
from the others. The parser itself is able to rip through a JSON
|
52
|
+
string, stream, or file and then make calls to a delegate to process
|
53
|
+
the JSON elements according to the delegate behavior. This is similar
|
54
|
+
to the `Oj.load` parser but the new parser takes advantage of
|
55
|
+
character maps, reduced conditional branching, and calling function
|
56
|
+
pointers.
|
57
|
+
|
58
|
+
### Options
|
59
|
+
|
60
|
+
As mentioned, one way to change the options issues was to change the
|
61
|
+
API. Instead of having a shared set of default options a separate
|
62
|
+
parser is created and configured for each use case. Options are set
|
63
|
+
with methods on the parser so no more guessing what options are
|
64
|
+
available. With options isolated to individual parsers there is no
|
65
|
+
unintended leakage to other parse use cases.
|
66
|
+
|
67
|
+
### Structure
|
68
|
+
|
69
|
+
A relative small amount of time is spent in the actual parsing of JSON
|
70
|
+
in `Oj.load`. Most of the time is spent building the Ruby
|
71
|
+
Objects. Even cutting the parsing time in half only gives a 10%
|
72
|
+
improvement in performance but 10% is still an improvement.
|
73
|
+
|
74
|
+
The `Oj::Parser` is designed to reduce conditional branching. To do
|
75
|
+
that it uses character maps for the various states that the parser
|
76
|
+
goes through when parsing. There is no recursion as the JSON elements
|
77
|
+
are parsed. The use of a character maps for each parser state means
|
78
|
+
the parser function can and is re-entrant so partial blocks of JSON
|
79
|
+
can be parsed and the results combined.
|
80
|
+
|
81
|
+
There are no Ruby calls in the parser itself. Instead delegates are
|
82
|
+
used to implement the various behaviors of the parser which are
|
83
|
+
currently validation (validate), callbacks (SAJ), or building Ruby
|
84
|
+
objects (usual). The delegates are where all the Ruby calls and
|
85
|
+
related optimizations take place.
|
86
|
+
|
87
|
+
Considering JSON file parsing, `Oj.load_file` is able to read a file a
|
88
|
+
block at a time and the new `Oj::Parser` does the same. There was a
|
89
|
+
change in how that is done though. `Oj.load_file` sets up a reader
|
90
|
+
that must be called for each character. Basically a buffered
|
91
|
+
reader. `Oj::Parser` drops down a level and uses a re-entrant parser
|
92
|
+
that takes a block of bytes at a time so there is no call needed for
|
93
|
+
each character but rather just iterating over the block read from the
|
94
|
+
file.
|
95
|
+
|
96
|
+
Reading a block at a time also allows for an efficient second thread
|
97
|
+
to be used for reading blocks. That feature is not in the first
|
98
|
+
iteration of the `Oj::Parser` but the stage is set for it in the
|
99
|
+
future. The same approach was used successfully in
|
100
|
+
[OjC](https://github.com/ohler55/ojc) which is where the code for the
|
101
|
+
parser was taken from.
|
102
|
+
|
103
|
+
### Delegates
|
104
|
+
|
105
|
+
There are three delegates; validate, SAJ, and usual.
|
106
|
+
|
107
|
+
#### Validate
|
108
|
+
|
109
|
+
The validate delegate is trivial in that does nothing other than let
|
110
|
+
the parser complete. There are no options for the validate
|
111
|
+
delegate. By not making any Ruby calls other than to start the parsing
|
112
|
+
the validate delegate is no surprise that the validate delegate is the
|
113
|
+
best performer.
|
114
|
+
|
115
|
+
#### SAJ (Simple API for JSON)
|
116
|
+
|
117
|
+
The SAJ delegate is compatible with the SAJ handlers used with
|
118
|
+
`Oj.saj_parse` so it needs to keep track of keys for the
|
119
|
+
callbacks. Two optimizations are used. The first is a reuseable key
|
120
|
+
stack while the second is a string cache similar to the Ruby intern
|
121
|
+
function.
|
122
|
+
|
123
|
+
When parsing a Hash (JSON object) element the key is passed to the
|
124
|
+
callback function if the SAJ handler responds to the method. The key
|
125
|
+
is also provided when closing an Array or Hash that is part of a
|
126
|
+
parent Hash. A key stack supports this.
|
127
|
+
|
128
|
+
If the option is turned on a lookup is made and previously cached key
|
129
|
+
VALUEs are used. This avoids creating the string for the key and
|
130
|
+
setting the encoding on it. The cache used is a auto expanding hash
|
131
|
+
implementation that is limited to strings less than 35 characters
|
132
|
+
which covers most keys. Larger strings use the slower string creation
|
133
|
+
approach. The use of the cache reduces object creation which save on
|
134
|
+
both memory allocation and time. It is not appropriate for one time
|
135
|
+
parsing of say all the keys in a dictionary but is ideally suited for
|
136
|
+
loading similar JSON multiple times.
|
137
|
+
|
138
|
+
#### Usual
|
139
|
+
|
140
|
+
By far the more complex of the delegates is the 'usual' delegate. The
|
141
|
+
usual delegate builds Ruby Objects when parsing JSON. It incorporates
|
142
|
+
many options for configuration and makes use of a number of
|
143
|
+
optimizations.
|
144
|
+
|
145
|
+
##### Reduce Branching
|
146
|
+
|
147
|
+
In keeping with the goal of reducing conditional branching most of the
|
148
|
+
delegate options are implemented by changing a function pointer
|
149
|
+
according to the option selected. For example when turning on or off
|
150
|
+
`:symbol_keys` the function to calculate the key is changed so no
|
151
|
+
decision needs to be made during parsing. Using this approach option
|
152
|
+
branching happens when the option is set and not each time when
|
153
|
+
parsing.
|
154
|
+
|
155
|
+
##### Cache
|
156
|
+
|
157
|
+
Creating Ruby Objects whether Strings, Array, or some other class is
|
158
|
+
expensive. Well expensive when running at the speeds Oj runs at. One
|
159
|
+
way to reduce Object creation is to cache those objects on the
|
160
|
+
assumption that they will most likely be used again. This is
|
161
|
+
especially true of Hash keys and Object attribute IDs. When creating
|
162
|
+
Objects from a class name in the JSON a class cache saves resolving
|
163
|
+
the string to a class each time. Of course there are times when
|
164
|
+
caching is not preferred so caching can be turned on or off with
|
165
|
+
option methods on the parser which are passed down to the delegate..
|
166
|
+
|
167
|
+
The Oj cache implementation is an auto expanding hash. When certain
|
168
|
+
limits are reached the hash is expanded and rehashed. Rehashing can
|
169
|
+
take some time as the number of items cached increases so there is
|
170
|
+
also an option to start with a larger cache size to avoid or reduce
|
171
|
+
the likelihood of a rehash.
|
172
|
+
|
173
|
+
The Oj cache has an advantage over the Ruby intern function
|
174
|
+
(`rb_intern()`) in that several steps are needed for some cached
|
175
|
+
items. As an example Object attribute IDs are created by adding an `@`
|
176
|
+
character prefix to a string and then converting to a ID. This is done
|
177
|
+
once when inserting into the cache and after that only a lookup is
|
178
|
+
needed.
|
179
|
+
|
180
|
+
##### Bulk Insert
|
181
|
+
|
182
|
+
The Ruby functions available for C extension functions are extensive
|
183
|
+
and offer many options across the board. The bulk insert functions for
|
184
|
+
both Arrays and Hashes are much faster than appending or setting
|
185
|
+
functions that set one value at a time. The Array bulk insert is
|
186
|
+
around 15 times faster and for Hash it is about 3 times faster.
|
187
|
+
|
188
|
+
To take advantage of the bulk inserts arrays of VALUEs are
|
189
|
+
needed. With a little planning there VALUE arrays can be reused which
|
190
|
+
leads into another optimization, the use of stacks.
|
191
|
+
|
192
|
+
##### Stacks
|
193
|
+
|
194
|
+
Parsing requires memory to keep track of values when parsing nested
|
195
|
+
JSON elements. That can be done on the call stack making use of
|
196
|
+
recursive calls or it can be done with a stack managed by the
|
197
|
+
parser. The `Oj.load` method maintains a stack for Ruby object and
|
198
|
+
builds the output as the parsing progresses.
|
199
|
+
|
200
|
+
`Oj::Parser` uses three different stacks. One stack for values, one
|
201
|
+
for keys, and one for collections (Array and Hash). By postponing the
|
202
|
+
creation of the collection elements the bulk insertions for Array and
|
203
|
+
Hash can be used. For arrays the use of a value stack and creating the
|
204
|
+
array after all elements have been identified gives a 15x improvement
|
205
|
+
in array creation.
|
206
|
+
|
207
|
+
For Hash the story is a little different. The bulk insert for Hash
|
208
|
+
alternates keys and values but there is a wrinkle to consider. Since
|
209
|
+
Ruby Object creation is triggered by the occurrence of an element that
|
210
|
+
matches a creation identifier the creation of a collection is not just
|
211
|
+
for Array and Hash but also Object. Setting Object attributes uses an
|
212
|
+
ID and not a VALUE. For that reason the keys should not be created as
|
213
|
+
String or Symbol types as they would be ignored and the VALUE creation
|
214
|
+
wasted when setting Object attributes. Using the bulk insert for Hash
|
215
|
+
gives a 3x improvement for that part of the object building.
|
216
|
+
|
217
|
+
Looking at the Object creation the JSON gem expects a class method of
|
218
|
+
`#json_create(arg)`. The single argument is the Hash resulting from
|
219
|
+
the parsing assuming that the parser parsed to a Hash first. This is
|
220
|
+
less than ideal from a performance perspective so `Oj::Parser`
|
221
|
+
provides an option to take that approach or to use the much more
|
222
|
+
efficient approach of never creating the Hash but instead creating the
|
223
|
+
Object and then setting the attributes directly.
|
224
|
+
|
225
|
+
To further improve performance and reduce the amount of memory
|
226
|
+
allocations and frees the stacks are reused from one call to `#parse`
|
227
|
+
to another.
|
228
|
+
|
229
|
+
## Results
|
230
|
+
|
231
|
+
The results are even better than expected. Running the
|
232
|
+
[perf_parser.rb](https://github.com/ohler55/oj/blob/develop/test/perf_parser.rb)
|
233
|
+
file shows the improvements. There are four comparisons all run on a
|
234
|
+
MacBook Pro with Intel processor.
|
235
|
+
|
236
|
+
### Validation
|
237
|
+
|
238
|
+
Without a comparible parser that just validates a JSON document the
|
239
|
+
`Oj.saj_parse` callback parser with a nil handler is used for
|
240
|
+
comparison to the new `Oj::Parser.new(:validate)`. In that case the
|
241
|
+
comparison is:
|
242
|
+
|
243
|
+
```
|
244
|
+
System time (secs) rate (ops/sec)
|
245
|
+
------------------- ----------- --------------
|
246
|
+
Oj::Parser.validate 0.101 494369.136
|
247
|
+
Oj::Saj.none 0.205 244122.745
|
248
|
+
```
|
249
|
+
|
250
|
+
The `Oj::Parser.new(:validate)` is **2.03** times faster!
|
251
|
+
|
252
|
+
### Callback
|
253
|
+
|
254
|
+
Oj has two callback parsers. One is SCP and the other SAJ. Both are
|
255
|
+
similar in that a handler is provided that implements methods for
|
256
|
+
processing the various element types in a JSON document. Comparing
|
257
|
+
`Oj.saj_parse` to `Oj::Parser.new(:saj)` with a all callback methods
|
258
|
+
implemented handler gives the following raw results:
|
259
|
+
|
260
|
+
```
|
261
|
+
System time (secs) rate (ops/sec)
|
262
|
+
-------------- ----------- --------------
|
263
|
+
Oj::Parser.saj 0.783 63836.986
|
264
|
+
Oj::Saj.all 1.182 42315.397
|
265
|
+
```
|
266
|
+
|
267
|
+
The `Oj::Parser.new(:saj)` is **1.51** times faster.
|
268
|
+
|
269
|
+
### Parse to Ruby primitives
|
270
|
+
|
271
|
+
Parsing to Ruby primitives and Array and Hash is possible with most
|
272
|
+
parsers including the JSON gem parser. The raw results comparing
|
273
|
+
`Oj.strict_load`, `Oj::Parser.new(:usual)`, and the JSON gem are:
|
274
|
+
|
275
|
+
```
|
276
|
+
System time (secs) rate (ops/sec)
|
277
|
+
---------------- ----------- --------------
|
278
|
+
Oj::Parser.usual 0.452 110544.876
|
279
|
+
Oj::strict_load 0.699 71490.257
|
280
|
+
JSON::Ext 1.009 49555.094
|
281
|
+
```
|
282
|
+
|
283
|
+
The `Oj::Parser.new(:saj)` is **1.55** times faster than `Oj.load` and
|
284
|
+
**2.23** times faster than the JSON gem.
|
285
|
+
|
286
|
+
### Object
|
287
|
+
|
288
|
+
Oj supports two modes for Object serialization and
|
289
|
+
deserialization. Comparing to the JSON gem compatible mode
|
290
|
+
`Oj.compat_load`, `Oj::Parser.new(:usual)`, and the JSON gem yields
|
291
|
+
the following raw results:
|
292
|
+
|
293
|
+
```
|
294
|
+
System time (secs) rate (ops/sec)
|
295
|
+
---------------- ----------- --------------
|
296
|
+
Oj::Parser.usual 0.071 703502.033
|
297
|
+
Oj::compat_load 0.225 221762.927
|
298
|
+
JSON::Ext 0.401 124638.859
|
299
|
+
```
|
300
|
+
|
301
|
+
The `Oj::Parser.new(:saj)` is **3.17** times faster than
|
302
|
+
`Oj.compat_load` and **5.64** times faster than the JSON gem.
|
303
|
+
|
304
|
+
## Summary
|
305
|
+
|
306
|
+
With a performance boost of from 1.5x to over 3x over the `Oj.load`
|
307
|
+
parser the new `Oj::Parser` is a big win in the performance arena. The
|
308
|
+
isolation of options is another feature that should make life easier
|
309
|
+
for developers.
|
data/pages/Rails.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
# Rails Quickstart
|
2
|
+
|
3
|
+
To universally replace Rails' use of the json gem with Oj, and also
|
4
|
+
have Oj "take over" many methods on the JSON constant (`load`, `parse`, etc.) with
|
5
|
+
their faster Oj counterparts, add this to an initializer:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
Oj.optimize_rails()
|
9
|
+
```
|
10
|
+
|
11
|
+
For more details and options, read on...
|
12
|
+
|
1
13
|
# Oj Rails Compatibility
|
2
14
|
|
3
15
|
The `:rails` mode mimics the ActiveSupport version 5 encoder. Rails and
|
@@ -41,7 +53,7 @@ The globals that ActiveSupport uses for encoding are:
|
|
41
53
|
|
42
54
|
Those globals are aliased to also be accessed from the ActiveSupport module
|
43
55
|
directly so `ActiveSupport::JSON::Encoding.time_precision` can also be accessed
|
44
|
-
from `ActiveSupport.time_precision`. Oj makes use of these globals in
|
56
|
+
from `ActiveSupport.time_precision`. Oj makes use of these globals in mimicking
|
45
57
|
Rails after the `Oj::Rails.set_encode()` method is called. That also sets the
|
46
58
|
`ActiveSupport.json_encoder` to the `Oj::Rails::Encoder` class.
|
47
59
|
|
@@ -125,7 +137,7 @@ gem 'oj', '3.7.12'
|
|
125
137
|
Ruby which is used by the json gem and by Rails. Ruby varies the
|
126
138
|
significant digits which can be either 16 or 17 depending on the value.
|
127
139
|
|
128
|
-
2. Optimized
|
140
|
+
2. Optimized Hashes do not collapse keys that become the same in the output. As
|
129
141
|
an example, a non-String object that has a `to_s()` method will become the
|
130
142
|
return value of the `to_s()` method in the output without checking to see if
|
131
143
|
that has already been used. This could occur is a mix of String and Symbols
|
data/test/_test_active.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
$LOAD_PATH << __dir__
|
5
5
|
%w(lib ext test).each do |dir|
|
6
6
|
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
7
7
|
end
|
@@ -13,14 +13,14 @@ require 'sqlite3'
|
|
13
13
|
require 'active_record'
|
14
14
|
require 'oj'
|
15
15
|
|
16
|
-
#Oj.mimic_JSON()
|
16
|
+
# Oj.mimic_JSON()
|
17
17
|
Oj.default_options = {mode: :compat, indent: 2}
|
18
18
|
|
19
|
-
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
19
|
+
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
20
20
|
|
21
21
|
ActiveRecord::Base.establish_connection(
|
22
|
-
:adapter =>
|
23
|
-
:database =>
|
22
|
+
:adapter => 'sqlite3',
|
23
|
+
:database => ':memory:'
|
24
24
|
)
|
25
25
|
|
26
26
|
ActiveRecord::Schema.define do
|
@@ -37,8 +37,8 @@ end
|
|
37
37
|
class ActiveTest < Minitest::Test
|
38
38
|
|
39
39
|
def test_active
|
40
|
-
User.find_or_create_by(first_name:
|
41
|
-
User.find_or_create_by(first_name:
|
40
|
+
User.find_or_create_by(first_name: 'John', last_name: 'Smith', email: 'john@example.com')
|
41
|
+
User.find_or_create_by(first_name: 'Joan', last_name: 'Smith', email: 'joan@example.com')
|
42
42
|
|
43
43
|
# Single instance.
|
44
44
|
assert_equal(%|{
|
@@ -71,6 +71,5 @@ class ActiveTest < Minitest::Test
|
|
71
71
|
|
72
72
|
# Array of instances as json. (not Oj)
|
73
73
|
assert_equal(%|[{"id":1,"first_name":"John","last_name":"Smith","email":"john@example.com"},{"id":2,"first_name":"Joan","last_name":"Smith","email":"joan@example.com"}]|, User.all.to_json)
|
74
|
-
|
75
74
|
end
|
76
75
|
end
|
data/test/_test_active_mimic.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
$LOAD_PATH << __dir__
|
5
5
|
%w(lib ext test).each do |dir|
|
6
6
|
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
7
7
|
end
|
@@ -16,11 +16,11 @@ require 'oj'
|
|
16
16
|
Oj.mimic_JSON()
|
17
17
|
Oj.default_options = {mode: :compat, indent: 2}
|
18
18
|
|
19
|
-
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
19
|
+
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
20
20
|
|
21
21
|
ActiveRecord::Base.establish_connection(
|
22
|
-
:adapter =>
|
23
|
-
:database =>
|
22
|
+
:adapter => 'sqlite3',
|
23
|
+
:database => ':memory:'
|
24
24
|
)
|
25
25
|
|
26
26
|
ActiveRecord::Schema.define do
|
@@ -37,8 +37,8 @@ end
|
|
37
37
|
class ActiveTest < Minitest::Test
|
38
38
|
|
39
39
|
def test_active
|
40
|
-
User.find_or_create_by(first_name:
|
41
|
-
User.find_or_create_by(first_name:
|
40
|
+
User.find_or_create_by(first_name: 'John', last_name: 'Smith', email: 'john@example.com')
|
41
|
+
User.find_or_create_by(first_name: 'Joan', last_name: 'Smith', email: 'joan@example.com')
|
42
42
|
|
43
43
|
# Single instance.
|
44
44
|
assert_equal(%|{
|
@@ -91,6 +91,5 @@ class ActiveTest < Minitest::Test
|
|
91
91
|
}
|
92
92
|
]
|
93
93
|
|, User.all.to_json)
|
94
|
-
|
95
94
|
end
|
96
95
|
end
|
data/test/_test_mimic_rails.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
$LOAD_PATH << __dir__
|
5
5
|
|
6
6
|
require 'helper'
|
7
|
-
#Oj.mimic_JSON
|
7
|
+
# Oj.mimic_JSON
|
8
8
|
require 'rails/all'
|
9
9
|
|
10
10
|
require 'active_model'
|
@@ -36,14 +36,12 @@ end
|
|
36
36
|
class MimicRails < Minitest::Test
|
37
37
|
|
38
38
|
def test_mimic_exception
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
assert(false, 'Expected a JSON::ParserError')
|
46
|
-
end
|
39
|
+
ActiveSupport::JSON.decode('{')
|
40
|
+
puts 'Failed'
|
41
|
+
rescue ActiveSupport::JSON.parse_error
|
42
|
+
assert(true)
|
43
|
+
rescue Exception
|
44
|
+
assert(false, 'Expected a JSON::ParserError')
|
47
45
|
end
|
48
46
|
|
49
47
|
def test_dump_string
|
@@ -84,18 +82,17 @@ class MimicRails < Minitest::Test
|
|
84
82
|
category = Category.new(1, 'test')
|
85
83
|
serializer = CategorySerializer.new(category)
|
86
84
|
|
87
|
-
|
85
|
+
serializer.to_json()
|
88
86
|
puts "*** serializer.to_json() #{serializer.to_json()}"
|
89
|
-
|
87
|
+
serializer.as_json()
|
90
88
|
puts "*** serializer.as_json() #{serializer.as_json()}"
|
91
|
-
|
89
|
+
JSON.dump(serializer)
|
92
90
|
puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
|
93
91
|
|
94
92
|
puts "*** category.to_json() #{category.to_json()}"
|
95
93
|
puts "*** category.as_json() #{category.as_json()}"
|
96
94
|
puts "*** JSON.dump(serializer) #{JSON.dump(category)}"
|
97
95
|
puts "*** Oj.dump(serializer) #{Oj.dump(category)}"
|
98
|
-
|
99
96
|
end
|
100
97
|
|
101
98
|
def test_dump_object_array
|
@@ -104,7 +101,7 @@ class MimicRails < Minitest::Test
|
|
104
101
|
cat2 = Category.new(2, 'test')
|
105
102
|
a = Array.wrap([cat1, cat2])
|
106
103
|
|
107
|
-
#serializer = CategorySerializer.new(a)
|
104
|
+
# serializer = CategorySerializer.new(a)
|
108
105
|
|
109
106
|
puts "*** a.to_json() #{a.to_json()}"
|
110
107
|
puts "*** a.as_json() #{a.as_json()}"
|
@@ -114,13 +111,13 @@ class MimicRails < Minitest::Test
|
|
114
111
|
|
115
112
|
def test_dump_time
|
116
113
|
Oj.default_options= {:indent => 2}
|
117
|
-
now = ActiveSupport::TimeZone['America/Chicago'].parse(
|
114
|
+
now = ActiveSupport::TimeZone['America/Chicago'].parse('2014-11-01 13:20:47')
|
118
115
|
json = Oj.dump(now, mode: :object, time_format: :xmlschema)
|
119
|
-
#puts "*** json: #{json}"
|
116
|
+
# puts "*** json: #{json}"
|
120
117
|
|
121
118
|
oj_dump = Oj.load(json, mode: :object, time_format: :xmlschema)
|
122
|
-
#puts "Now: #{now}\n Oj: #{oj_dump}"
|
123
|
-
assert_equal(
|
119
|
+
# puts "Now: #{now}\n Oj: #{oj_dump}"
|
120
|
+
assert_equal('2014-11-01T13:20:47-05:00', oj_dump.xmlschema)
|
124
121
|
end
|
125
122
|
|
126
123
|
end # MimicRails
|
@@ -13,13 +13,12 @@ Oj.default_options = { mode: :rails }
|
|
13
13
|
|
14
14
|
class ActiveRecordResultTest < Minitest::Test
|
15
15
|
def test_hash_rows
|
16
|
-
|
17
16
|
result = ActiveRecord::Result.new(["one", "two"],
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
[
|
18
|
+
["row 1 col 1", "row 1 col 2"],
|
19
|
+
["row 2 col 1", "row 2 col 2"],
|
20
|
+
["row 3 col 1", "row 3 col 2"],
|
21
|
+
])
|
23
22
|
#puts "*** result: #{Oj.dump(result, indent: 2)}"
|
24
23
|
json_result = if ActiveRecord.version >= Gem::Version.new("6")
|
25
24
|
result.to_a
|