Oj Object Mode Performance |
|
Author: Published: |
Optimized JSON (Oj) is a C extension to Ruby. The Oj parser in object mode has been optimized to the point where, the non-Ruby C code now contributes only 10% (8% or less for large JSON and 12% for small JSON documents) to the overall time it takes to parse and generate the Ruby objects represented in a JSON document. In addition to being the fastest Ruby JSON parser it also uses a constant stack size no matter how deeply nested the JSON document is. Oj does this by using a single pass, non-recursive callback parser. Minimizing stack use is important if Ruby fibers are used as the default stack size is small. There are no other JSON parsers that support Object encoding and decoding but the Ruby Marshal module does encode asn decode Ruby Objects into a binary format. Another efficient Object encoder is Ox which encodes Objects into XML. Oj is compared to these modules. To benchmark the Oj object parser a Ruby Object was selected that included all the various types. The sample Object is defined in GitHub. For the small test the sample size was set to zero and produces a 263 byte JSON file. For the larger files a larger was used to generate the 1M JSON file. The file used for generating the benchmark results is in GitHub. The JSON used for the small tests is: [{"^o":"Sample::Doc","title":"Sample","user":"ohler","create_time":{"^t":1371361533.272099000},"layers":{},"change_history":[{"^o":"Sample::Change","user":"ohler","time":{"^t":1371361533.272106000},"comment":"Changed at t+0."}],"props":{":purpose":"an example"}}] Benchmarks were done against the latest version of Ruby, 2.0.0p195 for the Ruby Marshaller and Ox version 2.0.3. Results In the tables, a higher ratio is better. The ratio is a comparison to the slowest parser in the test. The times listed are in seconds and are the number of seconds it took to parse the number of iterations noted in the test title. |
|||||||||||||||||||||||||||||
Small: 200,000 parses of a 263 byte JSON
|
Small Parse Results
|
||||||||||||||||||||||||||||
For a larger Object the files() function was called instead to generate the file.
For larger documents Marshal falls further behind. Surprisingly, the binary marshal document size was 844K which is not much smaller than the JSON format. |
Large Parse Results
|