You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+72-5Lines changed: 72 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
-
# Rapidjson
1
+
# RapidJSON
2
2
3
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rapidjson`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
-
TODO: Delete this and the text above, and describe your gem
3
+
(Maybe) Ruby's fastest JSON library! Built using the [RapidJSON C++ library](https://rapidjson.org/)
6
4
7
5
## Installation
8
6
@@ -22,7 +20,76 @@ Or install it yourself as:
22
20
23
21
## Usage
24
22
25
-
TODO: Write usage instructions here
23
+
```
24
+
RapidJSON.parse <<JSON
25
+
{
26
+
"foo":"bar"
27
+
}
28
+
JSON
29
+
# => {"foo" => "bar"}
30
+
```
31
+
32
+
```
33
+
RapidJSON.encode(json_string)
34
+
# => '{"foo":"bar"}'
35
+
```
36
+
37
+
```
38
+
RapidJSON.pretty_encode(json_string)
39
+
# =>
40
+
# {
41
+
# "foo": "bar"
42
+
# }
43
+
```
44
+
45
+
## Performance
46
+
47
+
Your current JSON parser/encoder is probably fine.
48
+
49
+
Unless there's good reason, it's probably best sticking with the standard `json` gem, which ships with Ruby. It become much faster in version 2.3, so try it again if you haven't recently!
50
+
51
+
However this library has a few performance advantages:
52
+
53
+
* JSON parsing
54
+
* Performance is achieved mostly through using RapidJSON one of the fastest open source JSON parsing libraries. It supports SIMD (SSE2, SSE4.2, NEON), avoids allocated memory, and has been honed to be if not the fastest library (that honour likely going to simdjson) the library to beat for JSON performance.
55
+
* Object allocation
56
+
* Wherever possible we avoid allocating objects. When generating JSON, RapidJSON will write the emitted JSON directly into the buffer of a Ruby string. (This is an optimization most Ruby JSON libraries will have)
57
+
* When parsing JSON we parse directly form the source string with a single copy
58
+
* When building a Hash for a JSON object, we use an fstrings (dedup'd and frozen strings) as the key
59
+
* Whenever possible we build Ruby objects from C types (int, char \*, double) rather than constructing intermediate Ruby string objects.
60
+
61
+
Many of these optimization can be found in all popular Ruby JSON libraries
62
+
63
+
```
64
+
== Encoding canada.json (2090234 bytes)
65
+
yajl 13.957 (± 0.0%) i/s - 70.000 in 5.015358s
66
+
json 13.912 (± 0.0%) i/s - 70.000 in 5.032247s
67
+
oj 20.821 (± 0.0%) i/s - 106.000 in 5.090981s
68
+
rapidjson 84.110 (± 2.4%) i/s - 424.000 in 5.042792s
69
+
```
70
+
71
+
```
72
+
== Parsing canada.json (2251051 bytes)
73
+
yajl 35.510 (± 2.8%) i/s - 180.000 in 5.070803s
74
+
json 22.105 (± 0.0%) i/s - 112.000 in 5.067063s
75
+
oj 15.163 (± 6.6%) i/s - 76.000 in 5.042864s
76
+
fast_jsonparser 10.791 (± 0.0%) i/s - 54.000 in 5.004290s
77
+
rapidjson 148.263 (± 2.0%) i/s - 742.000 in 5.006370s
78
+
```
79
+
Notes: oj seems unusually bad at this test, and is usually faster than yajl and
80
+
json, and comparable to rapidjson.
81
+
`fast_jsonparser` has been slow since Ruby 3.0, likely a bug, if fixed I'd
82
+
expect it to be the fasest JSON parser.
83
+
84
+
Other libraries may include modes to avoid constructing all objects. Currently
85
+
RapidJSON only focuses on the patterns and APIs users are likely to actually
86
+
use.
87
+
88
+
## Why another JSON library
89
+
90
+
I spent a week working on YAJL/yajl-ruby, and though I really liked the library, it hasn't kept up with the performance of the modern JSON libraries, specifically simdjson (C++), serde-json (Rust), and RapidJSON (C++). I was interested in how those libraries would integrate into Ruby. Of these, RapidJSON was the simplest fit for a Ruby extension. It's in C++ (clean Rust/Ruby bindings is unfortunately a work in progress), fast, uses SIMD instructions, supports encoding and decoding, and has a nice API to work with.
91
+
92
+
However, if you're happy with your current Ruby JSON library (including `json`) you should keep using it. They're all very good.
0 commit comments