Skip to content

Commit 0a10950

Browse files
committed
Added documentation of the tape format.
1 parent 779ce18 commit 0a10950

4 files changed

Lines changed: 141 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ make benchmark
7878
## Tools
7979

8080
- `json2json mydoc.json` parses the document, constructs a model and then dumps back the result to standard output.
81-
- `json2json -d mydoc.json` parses the document, constructs a model and then dumps model (as a tape) to standard output.
81+
- `json2json -d mydoc.json` parses the document, constructs a model and then dumps model (as a tape) to standard output. The tape format is described in the accompanying file tape.md.
8282
- `minify mydoc.json` minifies the JSON document, outputting the result to standard output. Minifying means to remove the unneeded white space charaters.
8383

8484
## Scope
@@ -100,7 +100,7 @@ To simplify the engineering, we make some assumptions.
100100
## Features
101101

102102
- The input string is unmodified. (Parsers like sajson and RapidJSON use the input string as a buffer.)
103-
- We parse integers and floating-point numbers as separate types which allows us to support large 64-bit integers in [-9223372036854775808,9223372036854775808), like a Java `long` or a C/C++ `long long`. Among the parsers that differentiate between integers and floating-point numbers, not all support 64-bit integers. (For example, sajson rejects JSON files with integers larger than or equal to 2147483648. RapidJSON will parse a file containing an overly long integer like 18446744073709551616 as a floating-point number) When we cannot represent exactly an integer as a signed 64-bit value, we reject the JSON document.
103+
- We parse integers and floating-point numbers as separate types which allows us to support large 64-bit integers in [-9223372036854775808,9223372036854775808), like a Java `long` or a C/C++ `long long`. Among the parsers that differentiate between integers and floating-point numbers, not all support 64-bit integers. (For example, sajson rejects JSON files with integers larger than or equal to 2147483648. RapidJSON will parse a file containing an overly long integer like 18446744073709551616 as a floating-point number.) When we cannot represent exactly an integer as a signed 64-bit value, we reject the JSON document.
104104
- We do full UTF-8 validation as part of the parsing. (Parsers like fastjson, gason and dropbox json11 do not do UTF-8 validation.)
105105
- We fully validate the numbers. (Parsers like gason and ultranjson will accept `[0e+]` as valid JSON.)
106106
- We validate string content for unescaped characters. (Parsers like fastjson and ultrajson accept unescaped line breaks and tags in strings.)

include/simdjson/parsedjson.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
#define DEFAULTMAXDEPTH 1024// a JSON document with a depth exceeding 1024 is probably de facto invalid
2020

21+
22+
/************
23+
* The JSON is parsed to a tape, see the accompanying tape.md file
24+
* for documentation.
25+
***********/
2126
struct ParsedJson {
2227
public:
2328

src/stage34_unified.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ really_inline bool is_valid_null_atom(const u8 *loc) {
6868
return error == 0;
6969
}
7070

71+
72+
/************
73+
* The JSON is parsed to a tape, see the accompanying tape.md file
74+
* for documentation.
75+
***********/
7176
// Implemented using Labels as Values which works in GCC and CLANG (and maybe
7277
// also in Intel's compiler), but won't work in MSVC. This would need to be
7378
// reimplemented differently if one wants to be standard compliant.

tape.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
# Tape structure in simdjson
3+
4+
We parse a JSON document to a tape. A tape is an array of 64-bit values. Each node encountered in the JSON document is written to the tape using one or more 64-bit tape elements; the layout of the tape is in "document order". Throughout, little endian encoding is assumed. The tape is indexed starting at 0 (the first element is at index 0).
5+
6+
## Example
7+
8+
It is sometimes useful to start with an example. Consider the following JSON document:
9+
10+
```
11+
{
12+
"Image": {
13+
"Width": 800,
14+
"Height": 600,
15+
"Title": "View from 15th Floor",
16+
"Thumbnail": {
17+
"Url": "http://www.example.com/image/481989943",
18+
"Height": 125,
19+
"Width": 100
20+
},
21+
"Animated": false,
22+
"IDs": [116, 943, 234, 38793]
23+
}
24+
}
25+
```
26+
27+
The following is a dump of the content of the tape, with the first number of each line representing the index of a tape element.
28+
29+
```
30+
$ ./json2json -d jsonexamples/small/demo.json
31+
0 : r // pointing to 38 (right after last node)
32+
1 : { // pointing to next tape location 38 (first node after the scope)
33+
2 : string "Image"
34+
3 : { // pointing to next tape location 37 (first node after the scope)
35+
4 : string "Width"
36+
5 : integer 800
37+
7 : string "Height"
38+
8 : integer 600
39+
10 : string "Title"
40+
11 : string "View from 15th Floor"
41+
12 : string "Thumbnail"
42+
13 : { // pointing to next tape location 23 (first node after the scope)
43+
14 : string "Url"
44+
15 : string "http://www.example.com/image/481989943"
45+
16 : string "Height"
46+
17 : integer 125
47+
19 : string "Width"
48+
20 : integer 100
49+
22 : } // pointing to previous tape location 13 (start of the scope)
50+
23 : string "Animated"
51+
24 : false
52+
25 : string "IDs"
53+
26 : [ // pointing to next tape location 36 (first node after the scope)
54+
27 : integer 116
55+
29 : integer 943
56+
31 : integer 234
57+
33 : integer 38793
58+
35 : ] // pointing to previous tape location 26 (start of the scope)
59+
36 : } // pointing to previous tape location 3 (start of the scope)
60+
37 : } // pointing to previous tape location 1 (start of the scope)
61+
38 : r // pointing to 0 (start root)
62+
63+
```
64+
65+
## General formal of the tape elements
66+
67+
Most tape elements are written as ('c' << 56) + x where 'c' is some ASCII character determining the type of the element and where x is a 56-bit value called the payload.
68+
69+
70+
## Simple JSON values
71+
72+
Simple JSON nodes are represented with one tape element:
73+
74+
- null is represented as the 64-bit value ('n' << 56) where 'n' is the 8-bit code point values (in ASCII) corresponding to the letter 'n'.
75+
- true is represented as the 64-bit value ('t' << 56).
76+
- false is represented as the 64-bit value ('f' << 56).
77+
78+
Performance consideration: It is somewhat wasteful to use 64-bit tape elements to store values that would require far less storage. However, we believe that this has no significant performance impact in most practical applications.
79+
80+
## Integer and Double values
81+
82+
Integer values are represented as two 64-bit tape elements:
83+
- The 64-bit value ('l' << 56) followed by the 64-bit integer value litterally. Integer values are assumed to be signed 64-bit values, using two's complement notation.
84+
85+
Float values are represented as two 64-bit tape elements:
86+
- The 64-bit value ('d' << 56) followed by the 64-bit double value litterally in standard IEEE 754 notation.
87+
88+
Performance consideration: We store numbers of the main tape because we believe that locality of reference is helpful for performance. The format is somewhat storage wasteful as 56 bits are ignored.
89+
90+
## Root node
91+
92+
Each JSON document will have two special 64-bit tape element representing a root node, one at the beginning and one at the end.
93+
94+
- The first 64-bit tape element contains the value ('r'<<56) + x where x is the location on the tape of the last root element.
95+
- The last 64-bit tape element contains the value ('r'<< 56).
96+
97+
All of the parsed document is located between these two 64-bit tape elements.
98+
99+
Hint: we can read the first tape element to determine the length of the tape.
100+
101+
102+
## Strings
103+
104+
We store string values using UTF-8 encoding with null termination on a separate tape. A string value is represented on the main tape as the 64-bit tape element ('"'<< 56) + x where x is the location on the string tape of the null-terminated string.
105+
106+
## Arrays
107+
108+
JSON arrays are represented using two 64-bit tape elements.
109+
110+
- The first 64-bit tape element contains the value ('[' << 56) + x where the payload x is 1 + the index of the second 64-bit tape element on the tape.
111+
- The second 64-bit tape element contains the value (']' << 56) + x where the payload x contains the index of the first 64-bit tape element on the tape.
112+
113+
All the content of the array is located between these two tape elements,including arrays and objects.
114+
115+
Performance consideration: We can skip the content of an array entirely by accessing the first 64-bit tape element, reading the payload and moving to the corresponding index on the tape.
116+
117+
## Objects
118+
119+
JSON objects are represented using two 64-bit tape elements.
120+
121+
- The first 64-bit tape element contains the value ('{' << 56) + x where the payload x is 1 + the index of the second 64-bit tape element on the tape.
122+
- The second 64-bit tape element contains the value ('{' << 56) + x where the payload x contains the index of the first 64-bit tape element on the tape.
123+
124+
In-between these two tape elements, we alternate between key (which must strings) and values. A value could be an object or an array.
125+
126+
All the content of the array is located between these two tape elements, including arrays and objects.
127+
128+
Performance consideration: We can skip the content of an object entirely by accessing the first 64-bit tape element, reading the payload and moving to the corresponding index on the tape.
129+

0 commit comments

Comments
 (0)