8000 Quickstart and intro (#575) · JavaScriptExpert/simdjson@0e45663 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e45663

Browse files
lemirejkeiser
andauthored
Quickstart and intro (simdjson#575)
* Add quickstart * Fix amalgamation Co-authored-by: John Keiser <john@johnkeiser.com>
1 parent 80a9f4d commit 0e45663

File tree

4 files changed

+7559
-3400
lines changed

4 files changed

+7559
-3400
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
1-
# simdjson : Parsing gigabytes of JSON per second
1+
# simdjson : Parsing gigabytes of JSON per second
2+
3+
<img src="images/logo.png" width="10%" style="float: right">
4+
JSON is everywhere on the Internet. Servers spend a *lot* of time parsing it. We need a fresh approach. simdjson uses commonly available SIMD instructions and microparallel algorithms to parse JSON 2.5x faster than anything else out there.
5+
6+
* **Ludicrous Speed:** Over 2.5x faster than other production-grade JSON parsers.
7+
* **Delightfully Easy:** First-class, easy to use API.
8+
* **Complete Validation:** Full JSON and UTF-8 validation, with no compromises.
9+
* **Rock-Solid Reliability:** From memory allocation to error handling, simdjson's design avoids surprises.
10+
11+
This library is part of the [Awesome Modern C++](https://awesomecpp.com) list.
12+
213
[![Build Status](https://cloud.drone.io/api/badges/simdjson/simdjson/status.svg)](https://cloud.drone.io/simdjson/simdjson)
314
[![CircleCI](https://circleci.com/gh/simdjson/simdjson.svg?style=svg)](https://circleci.com/gh/simdjson/simdjson)
415
[![Build status](https://ci.appveyor.com/api/projects/status/ae77wp5v3lebmu6n/branch/master?svg=true)](https://ci.appveyor.com/project/lemire/simdjson-jmmti/branch/master)
516
[![][license img]][license]
617
[![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/simdjson.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:simdjson)
718

19+
## Quick Start
820

9-
## A C++ library to see how fast we can parse JSON with complete validation.
21+
simdjson is easily consumable with a single .h and .cpp file.
1022

11-
JSON documents are everywhere on the Internet. Servers spend a lot of time parsing these documents. We want to accelerate the parsing of JSON per se using commonly available SIMD instructions as much as possible while doing full validation (including character encoding). This library is part of the [Awesome Modern C++](https://awesomecpp.com) list.
23+
0. Prerequisites: `g++` or `clang++`.
24+
1. Pull [simdjson.h](singleheader/simdjson.h) and [simdjson.cpp](singleheader/simdjson.h) into a directory, along with the sample file [twitter.json](jsonexamples/twitter.json).
25+
```
26+
wget https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.h https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.cpp https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json
27+
```
28+
2. Create `parser.cpp`:
1229

13-
<img src="images/logo.png" width="10%">
30+
```c++
31+
#include "simdjson.h"
32+
int main(void) {
33+
simdjson::document::parser parser;
34+
simdjson::document& tweets = parser.load("twitter.json");
35+
std::cout << tweets["search_metadata"]["count"] << " results." << std::endl;
36+
}
37+
```
38+
3. `g++ -o parser p 8000 arser.cpp` (or clang++)
39+
4. `./parser`
40+
```
41+
100 results.
42+
```
1443
1544
## Real-world usage
1645

singleheader/amalgamation_demo.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on Thu Mar 5 10:30:07 PST 2020. Do not edit! */
1+
/* auto-generated on Fri Mar 20 11:47:31 PDT 2020. Do not edit! */
22

33
#include <iostream>
44
#include "simdjson.h"
@@ -8,37 +8,31 @@ int main(int argc, char *argv[]) {
88
std::cerr << "Please specify at least one file name. " << std::endl;
99
}
1010
const char * filename = argv[1];
11-
simdjson::padded_string p = simdjson::get_corpus(filename);
12-
auto [doc, error] = simdjson::document::parse(p); // do the parsing
11+
simdjson::document::parser parser;
12+
auto [doc, error] = parser.load(filename); // do the parsing
1313
if (error) {
14-
std::cout << "document::parse failed" << std::endl;
14+
std::cout << "parse failed" << std::endl;
1515
std::cout << "error code: " << error << std::endl;
1616
std::cout << error << std::endl;
1717
} else {
18-
std::cout << "document::parse valid" << std::endl;
18+
std::cout << "parse valid" << std::endl;
1919
}
2020
if(argc == 2) {
2121
return EXIT_SUCCESS;
2222
}
2323

24-
//JsonStream
24+
// parse_many
2525
const char * filename2 = argv[2];
26-
simdjson::padded_string p2 = simdjson::get_corpus(filename2);
27-
simdjson::document::parser parser;
28-
simdjson::JsonStream js{p2};
29-
int parse_res = simdjson::SUCCESS_AND_HAS_MORE;
30-
31-
while (parse_res == simdjson::SUCCESS_AND_HAS_MORE) {
32-
parse_res = js.json_parse(parser);
26+
for (auto result : parser.load_many(filename2)) {
27+
error = result.error();
3328
}
34-
35-
if( ! parser.is_valid()) {
36-
std::cout << "JsonStream not valid" << std::endl;
29+
if (error) {
30+
std::cout << "parse_many failed" << std::endl;
31+
std::cout << "error code: " << error << std::endl;
32+
std::cout << error << std::endl;
3733
} else {
38-
std::cout << "JsonStream valid" << std::endl;
34+
std::cout << "parse_many valid" << std::endl;
3935
}
40-
41-
4236
return EXIT_SUCCESS;
4337
}
4438

0 commit comments

Comments
 (0)
0