fastsimdjson 2.0.0__cp315-cp315-pyemscripten_2026_5_wasm32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,1109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastsimdjson
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Fast JSON parsing for Python, built on simdjson
|
|
5
|
+
Author-email: Daniel Lemire <daniel@lemire.me>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/simdjson/fastpysimdjson
|
|
8
|
+
Project-URL: Repository, https://github.com/simdjson/fastpysimdjson
|
|
9
|
+
Project-URL: Issues, https://github.com/simdjson/fastpysimdjson/issues
|
|
10
|
+
Keywords: json,simdjson,parser,simd,performance
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: C++
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest; extra == "test"
|
|
27
|
+
Requires-Dist: msgpack; extra == "test"
|
|
28
|
+
Requires-Dist: typing_extensions; extra == "test"
|
|
29
|
+
Provides-Extra: bench
|
|
30
|
+
Requires-Dist: orjson; extra == "bench"
|
|
31
|
+
Requires-Dist: jiter; extra == "bench"
|
|
32
|
+
Requires-Dist: msgspec; extra == "bench"
|
|
33
|
+
Requires-Dist: pydantic; extra == "bench"
|
|
34
|
+
Requires-Dist: cattrs; extra == "bench"
|
|
35
|
+
Requires-Dist: pysimdjson; extra == "bench"
|
|
36
|
+
Requires-Dist: cysimdjson; extra == "bench"
|
|
37
|
+
Requires-Dist: pyarrow; extra == "bench"
|
|
38
|
+
Requires-Dist: polars; extra == "bench"
|
|
39
|
+
Requires-Dist: duckdb; extra == "bench"
|
|
40
|
+
Requires-Dist: msgpack; extra == "bench"
|
|
41
|
+
Requires-Dist: ormsgpack; extra == "bench"
|
|
42
|
+
|
|
43
|
+
# fastsimdjson
|
|
44
|
+
|
|
45
|
+
Fast JSON for Python, built on [simdjson](https://github.com/simdjson/simdjson),
|
|
46
|
+
the parser that reads JSON at gigabytes per second. fastsimdjson is a drop-in
|
|
47
|
+
replacement for the `json` module (`loads`, `load`, `dumps`, `dump`) that
|
|
48
|
+
gives the same results 3.5 to 4.2 times faster, and it goes further: lazy
|
|
49
|
+
parsing, streams of documents on many threads, Apache Arrow tables and
|
|
50
|
+
MessagePack, in one package with no dependencies.
|
|
51
|
+
|
|
52
|
+
## How it compares
|
|
53
|
+
|
|
54
|
+
fastsimdjson is often two to four times faster than the alternatives:
|
|
55
|
+
|
|
56
|
+
<picture>
|
|
57
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_summary_dark.png">
|
|
58
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_summary.png" width="80%" alt="fastsimdjson against the competition, speed on one core relative to a fast alternative: Python objects to JSON str, 4.2 times json.dumps; MessagePack to JSON, 4.1 times ormsgpack with orjson; JSON to MessagePack, 3.6 times orjson with ormsgpack; NDJSON to Arrow, 3.6 times polars; Arrow to NDJSON, 2.4 times polars; a few fields of a document, 2.2 times msgspec; JSON to Python objects, 1.3 times orjson">
|
|
59
|
+
</picture>
|
|
60
|
+
|
|
61
|
+
Against the standard `json` module, `loads` is 3.5 times faster and `parse`
|
|
62
|
+
25 times faster. The files, the versions and the scripts are under
|
|
63
|
+
[Benchmarks](#benchmarks).
|
|
64
|
+
|
|
65
|
+
* **Same results.** `loads` returns what `json.loads` returns, `dumps` the
|
|
66
|
+
same `str` as `json.dumps`, character for character, and `dumpb` the same
|
|
67
|
+
bytes as `orjson.dumps`. Switching changes the speed, not the output.
|
|
68
|
+
* **Typed.** `loads(data, type=T)` builds dataclasses, TypedDicts and
|
|
69
|
+
other typed values straight from the parser, with the types checked: 1.5
|
|
70
|
+
to 2.1 times as fast as msgspec, 3.9 to 5.1 times as fast as pydantic.
|
|
71
|
+
* **Only what you need.** `parse` gives lazy views and builds only the
|
|
72
|
+
values you read; `fields=` extracts only some fields from each document of
|
|
73
|
+
a stream.
|
|
74
|
+
* **Many threads.** On free-threaded Python, streams of documents (NDJSON,
|
|
75
|
+
JSON Lines) are read and written on many threads: up to 20 GB/s on 64
|
|
76
|
+
cores. Arrow and MessagePack conversions release the GIL and use many
|
|
77
|
+
threads on any Python: on 32 cores, `loads_arrow` reads NDJSON into Arrow
|
|
78
|
+
at up to 27.5 GB/s, 2.3 to 2.9 times as fast as pyarrow.
|
|
79
|
+
* **Arrow without pyarrow.** `loads_arrow` and `dumpb_arrow` convert NDJSON
|
|
80
|
+
to Arrow tables and back, for pyarrow, polars or DuckDB, without a copy.
|
|
81
|
+
* **MessagePack without Python objects.** `json_to_msgpack` and
|
|
82
|
+
`msgpack_to_json` convert directly, without building Python objects.
|
|
83
|
+
* **Minify without parsing.** `minify` strips insignificant whitespace
|
|
84
|
+
and returns `bytes`, without building Python objects.
|
|
85
|
+
* **Everywhere.** No dependencies; wheels for Linux (x64 and ARM), macOS
|
|
86
|
+
(Intel and Apple silicon) and Windows, Python 3.10 to 3.15, including
|
|
87
|
+
free-threaded Python.
|
|
88
|
+
|
|
89
|
+
## Installation
|
|
90
|
+
|
|
91
|
+
With pip:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
pip install fastsimdjson
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
With uv:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
uv pip install fastsimdjson # in a uv project: uv add fastsimdjson
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Wheels are available for Linux, macOS and Windows, for Python 3.10 to 3.15,
|
|
104
|
+
including free-threaded Python 3.14 and 3.15.
|
|
105
|
+
|
|
106
|
+
Wheels are also available for Pyodide (WebAssembly, in the browser or in
|
|
107
|
+
Node), for Python 3.13 to 3.15:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import micropip
|
|
111
|
+
await micropip.install("fastsimdjson")
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Under Pyodide, Python cannot start threads: `threads=` runs on the calling
|
|
115
|
+
thread.
|
|
116
|
+
|
|
117
|
+
## Usage
|
|
118
|
+
|
|
119
|
+
### `loads`: the whole document
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
import fastsimdjson
|
|
123
|
+
fastsimdjson.loads(b'{"a": [1, 2.5, "x", true, null]}')
|
|
124
|
+
# {'a': [1, 2.5, 'x', True, None]}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`loads(data)` accepts `bytes`, `bytearray`, `memoryview` and `str`, and
|
|
128
|
+
returns the same value as `json.loads`, with the same types and key order.
|
|
129
|
+
Integers that do not fit in 64 bits become exact Python ints. `NaN`,
|
|
130
|
+
`Infinity` and `-Infinity` are accepted, in any capitalization.
|
|
131
|
+
|
|
132
|
+
Invalid input raises `fastsimdjson.JSONDecodeError`, a subclass of
|
|
133
|
+
`json.JSONDecodeError`. When simdjson rejects a document, the same input is
|
|
134
|
+
parsed with `json.loads`. If that succeeds, `loads` returns its value (this
|
|
135
|
+
is how a number that overflows a double becomes `inf`). If it raises
|
|
136
|
+
`JSONDecodeError`, the exception is re-raised with Python's message and byte
|
|
137
|
+
position. Any other exception from `json.loads` propagates.
|
|
138
|
+
|
|
139
|
+
### `parse`: lazy views
|
|
140
|
+
|
|
141
|
+
When you need only part of a document, `parse` avoids building the rest.
|
|
142
|
+
It accepts the same inputs as `loads` and returns read-only views:
|
|
143
|
+
`fastsimdjson.Object` (a `Mapping`) and `fastsimdjson.Array` (a
|
|
144
|
+
`Sequence`). Values are converted when you access them; nested objects and
|
|
145
|
+
arrays are returned as views. A scalar root is returned as a plain value.
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
doc = fastsimdjson.parse(open("twitter.json", "rb").read())
|
|
149
|
+
ids = [(s["id"], s["user"]["screen_name"]) for s in doc["statuses"]]
|
|
150
|
+
doc.at_pointer("/statuses/0/user/name") # JSON Pointer (RFC 6901)
|
|
151
|
+
doc["search_metadata"].as_dict() # convert a subtree, like loads
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`Object` supports `obj[key]`, `get`, `in`, `len`, iteration over the keys,
|
|
155
|
+
`keys()`, `values()`, `items()` (iterators), `at_pointer` and `as_dict()`.
|
|
156
|
+
`Array` supports `arr[i]` (negative indexes and slices), `len`, iteration,
|
|
157
|
+
`at_pointer` and `as_list()`. Both work with `match` statements.
|
|
158
|
+
|
|
159
|
+
* A view keeps its document alive; the document owns its own buffers, so
|
|
160
|
+
it remains valid while other documents are parsed.
|
|
161
|
+
* A key lookup scans the object. With duplicate keys, lookups return the
|
|
162
|
+
first value, whereas `as_dict()` (like `json.loads`) keeps the last.
|
|
163
|
+
* Indexing an array walks it from the last index reached, so a loop over
|
|
164
|
+
`arr[i]` is linear; iteration is the fastest way to visit an array.
|
|
165
|
+
* Errors are handled as in `loads`. A document that simdjson rejects but
|
|
166
|
+
`json.loads` accepts (an overflowing number, an unpaired surrogate) is
|
|
167
|
+
returned as plain Python objects, as `loads` would return it.
|
|
168
|
+
|
|
169
|
+
### `type=`: typed decoding
|
|
170
|
+
|
|
171
|
+
`loads(data, type=T)` returns the document as an instance of `T`, checked
|
|
172
|
+
on the way. It builds dataclasses straight from the simdjson tape, without
|
|
173
|
+
an intermediate dict, and is 1.5 to 2.1 times as fast as msgspec on one core
|
|
174
|
+
(see [Typed decoding](#typed-decoding)).
|
|
175
|
+
|
|
176
|
+
<picture>
|
|
177
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_typed_dark.png">
|
|
178
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_typed.png" width="80%" alt="JSON to dataclasses on one core, GB/s. twitter.json, a few fields: fastsimdjson 2.60, msgspec Struct 1.52, msgspec dataclasses 1.43, pydantic 0.66, orjson with cattrs 0.55. citm_catalog.json typed in full: fastsimdjson 0.98, msgspec Struct 0.63, msgspec dataclasses 0.47, pydantic 0.19, orjson with cattrs 0.19. GH Archive NDJSON, 6 fields: fastsimdjson 2.05, msgspec Struct 1.30, msgspec dataclasses 1.16, pydantic 0.51, orjson with cattrs 0.42">
|
|
179
|
+
</picture>
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from dataclasses import dataclass, field
|
|
183
|
+
from typing import Literal, Optional
|
|
184
|
+
|
|
185
|
+
@dataclass
|
|
186
|
+
class User:
|
|
187
|
+
id: int
|
|
188
|
+
login: str
|
|
189
|
+
followers: int = 0
|
|
190
|
+
|
|
191
|
+
@dataclass
|
|
192
|
+
class Repo:
|
|
193
|
+
id: int
|
|
194
|
+
name: str
|
|
195
|
+
owner: User
|
|
196
|
+
topics: list[str] = field(default_factory=list)
|
|
197
|
+
visibility: Literal["public", "private"] = "public"
|
|
198
|
+
license: Optional[str] = None
|
|
199
|
+
|
|
200
|
+
repo = fastsimdjson.loads(data, type=Repo)
|
|
201
|
+
repos = fastsimdjson.loads(data, type=list[Repo])
|
|
202
|
+
events = list(fastsimdjson.loads_many(ndjson, type=Repo))
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
`type` accepts:
|
|
206
|
+
|
|
207
|
+
* `int`, `float` (an integer becomes a float), `str`, `bool`, `type(None)`
|
|
208
|
+
(JSON `null`; `None` too inside a type, as in `list[None]`), and `Any` or
|
|
209
|
+
`object` (no check: the value as `loads` returns it). `type=None`, the
|
|
210
|
+
default, means no type;
|
|
211
|
+
* `list`, `tuple` (fixed length or `tuple[T, ...]`), `set`, `frozenset`,
|
|
212
|
+
`dict` with `str` keys, and their abstract counterparts (`Sequence`,
|
|
213
|
+
`Mapping`, ...);
|
|
214
|
+
* `Optional[T]`, unions (`int | str`, at most one type per JSON kind; an
|
|
215
|
+
integer goes to `int` rather than to `float`) and `Literal` (of `str`,
|
|
216
|
+
`int`, `bool` and `None`: JSON `1` is not `True`);
|
|
217
|
+
* enums (by value: `str`, `int`, `bool` or `None`), dataclasses (including
|
|
218
|
+
frozen ones and those with `__slots__`), `TypedDict` (from `typing` or
|
|
219
|
+
`typing_extensions`) and `NamedTuple` (from an array);
|
|
220
|
+
* `Annotated`, `NewType` and `type` aliases, and recursive types.
|
|
221
|
+
|
|
222
|
+
Dataclass fields take their defaults (and default factories) when the key
|
|
223
|
+
is missing, keys that are not fields are ignored, `__post_init__` runs, and
|
|
224
|
+
`init=False` fields get their default. The fields are set as the generated
|
|
225
|
+
`__init__` sets them: an `__init__` of your own is not called. A `TypedDict` becomes a `dict` with
|
|
226
|
+
its keys only. A value of the wrong type raises
|
|
227
|
+
`fastsimdjson.ValidationError` (a `ValueError`) with its path:
|
|
228
|
+
|
|
229
|
+
```
|
|
230
|
+
ValidationError: Expected `int`, got `str` - at `$.owner.id`
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
A `ValueError` or `TypeError` raised by `__post_init__` becomes a
|
|
234
|
+
`ValidationError` too, with the path. Invalid JSON raises
|
|
235
|
+
`JSONDecodeError`, and the documents that only `json` accepts (`NaN`, a
|
|
236
|
+
number that overflows a double) are converted the same way, from what
|
|
237
|
+
`json` returns; past what Python's stack allows, those raise
|
|
238
|
+
`RecursionError`, as `json` does. A set of values that cannot be hashed
|
|
239
|
+
(`set[list[int]]`) or a union that contains itself is a `TypeError`. The
|
|
240
|
+
description of each type is computed once and cached (the cache starts over
|
|
241
|
+
past 1024 types, so that types made on the fly do not pile up). `load`,
|
|
242
|
+
`load_file`, `loads_many` and `map_many` accept `type=` too; a
|
|
243
|
+
`ValidationError` ends the iteration of `loads_many`, as any error does. With
|
|
244
|
+
`threads=N` on free-threaded Python, the instances are built on N threads
|
|
245
|
+
(not with `fields=` or `lazy=True`).
|
|
246
|
+
|
|
247
|
+
### `dumps` and `dump`: like `json.dumps`
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
fastsimdjson.dumps({"a": [1, 2.5, None]}) # '{"a": [1, 2.5, null]}'
|
|
251
|
+
fastsimdjson.dumps(obj, indent=2, sort_keys=True)
|
|
252
|
+
with open("out.json", "w", encoding="utf-8") as f:
|
|
253
|
+
fastsimdjson.dump(obj, f)
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`dumps(obj, **kw)` takes the arguments of `json.dumps` and returns the same
|
|
257
|
+
`str`, character for character, including the float format (`repr`), the
|
|
258
|
+
escapes and the default separators. `ensure_ascii`, `indent`, `separators`,
|
|
259
|
+
`sort_keys`, `allow_nan` and `default` are handled in C. Everything else is
|
|
260
|
+
passed to `json.dumps` itself, which produces the result or raises its usual
|
|
261
|
+
exception: a `cls` argument or other encoder options, `skipkeys`, a circular
|
|
262
|
+
reference, `NaN` with `allow_nan=False`, a key or a value that `json` cannot
|
|
263
|
+
serialize. `dump(obj, fp, **kw)` writes `dumps(obj, **kw)` to `fp`.
|
|
264
|
+
|
|
265
|
+
### `dumpb`: like `orjson.dumps`
|
|
266
|
+
|
|
267
|
+
```python
|
|
268
|
+
fastsimdjson.dumpb({"a": [1, 2.5, None]}) # b'{"a":[1,2.5,null]}'
|
|
269
|
+
fastsimdjson.dumpb(obj, option=fastsimdjson.OPT_INDENT_2 | fastsimdjson.OPT_SORT_KEYS)
|
|
270
|
+
fastsimdjson.dumpb({1, 2}, default=sorted) # b'[1,2]'
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
`dumpb(obj, default=None, option=None)` has the arguments, the output and the
|
|
274
|
+
errors of `orjson.dumps` (orjson 3.12): `orjson.dumps(obj, ...)` can be
|
|
275
|
+
replaced by `fastsimdjson.dumpb(obj, ...)` for the types below. It returns
|
|
276
|
+
compact UTF-8 `bytes`, writes floats as orjson does (`1e-6`, `1e+16`, `NaN`
|
|
277
|
+
and infinities as `null`), and raises `TypeError` with orjson's messages:
|
|
278
|
+
integers beyond 64 bits, a dict key that is not a `str`, invalid UTF-8 (a lone
|
|
279
|
+
surrogate), nesting deeper than 254, a type it cannot serialize. `orjson.JSONEncodeError` is a subclass of `TypeError`, so
|
|
280
|
+
`except TypeError` catches the errors of both.
|
|
281
|
+
|
|
282
|
+
It serializes `str`, `int`, `float`, `bool`, `None`, `dict`, `list`, `tuple`,
|
|
283
|
+
enums, and subclasses of `str`, `int`, `dict` and `list`. Anything else goes
|
|
284
|
+
to `default`, as in orjson: its result is serialized in place of the object,
|
|
285
|
+
and an exception it raises becomes the `__cause__` of the `TypeError`. The
|
|
286
|
+
options are exported under orjson's names and values (`OPT_APPEND_NEWLINE`,
|
|
287
|
+
`OPT_INDENT_2`, `OPT_NON_STR_KEYS`, `OPT_PASSTHROUGH_SUBCLASS`,
|
|
288
|
+
`OPT_SORT_KEYS`, `OPT_STRICT_INTEGER`, ...). Unlike orjson, `dumpb` does not
|
|
289
|
+
serialize dataclasses, `datetime`, `date`, `time`, `UUID`, numpy arrays or
|
|
290
|
+
`orjson.Fragment` itself: they go to `default`, as if
|
|
291
|
+
`OPT_PASSTHROUGH_DATACLASS` and `OPT_PASSTHROUGH_DATETIME` were set, and the
|
|
292
|
+
options that concern only these types are accepted and have no effect.
|
|
293
|
+
|
|
294
|
+
### `minify`: strip whitespace without parsing
|
|
295
|
+
|
|
296
|
+
```python
|
|
297
|
+
fastsimdjson.minify(b'{ "a" : [1, 2] }') # b'{"a":[1,2]}'
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
`minify(data)` removes insignificant whitespace (spaces, tabs, newlines and
|
|
301
|
+
carriage returns outside of strings) and returns `bytes`. It accepts `bytes`,
|
|
302
|
+
`bytearray`, `memoryview` and `str`. It does not parse the document and does
|
|
303
|
+
not validate it: numbers, escapes, duplicate keys and key order are left as
|
|
304
|
+
they are, and input that is not valid JSON is minified the same way. An
|
|
305
|
+
unterminated string raises `JSONDecodeError` (the minifier does not report
|
|
306
|
+
where the string started, so the position is 0). The call releases the GIL.
|
|
307
|
+
|
|
308
|
+
### `dumpb_many`: NDJSON, on several threads
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
data = fastsimdjson.dumpb_many(records) # b'{...}\n{...}\n'
|
|
312
|
+
data = fastsimdjson.dumpb_many(records, threads=16) # free-threaded Python
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
`dumpb_many(records, default=None, option=None, *, threads=None)` returns
|
|
316
|
+
the records (a list, a tuple or any iterable) as NDJSON `bytes`: each record
|
|
317
|
+
as `dumpb` writes it, followed by a newline, so that the output reads back
|
|
318
|
+
with `loads_many(data, format="lines")`. `default` and `option` are those of
|
|
319
|
+
`dumpb`, except `OPT_INDENT_2`, which would spread a record over several
|
|
320
|
+
lines (`ValueError`); `OPT_APPEND_NEWLINE` changes nothing.
|
|
321
|
+
|
|
322
|
+
On free-threaded Python, `threads=N` writes chunks of records on N threads at
|
|
323
|
+
once (`threads=0` or `None`: one per processor), with the worker threads of
|
|
324
|
+
`map_many`. The output and the errors are those of one thread: when records
|
|
325
|
+
cannot be serialized, the error of the first of them is raised (`default` may
|
|
326
|
+
then have been called on later records, in other threads, and it must be
|
|
327
|
+
safe to call from several threads at once). With the GIL, the calling thread
|
|
328
|
+
does all the work. Like `dumpb`, `dumpb_many` does not crash when other
|
|
329
|
+
threads change the records meanwhile, but the output then reflects some of
|
|
330
|
+
the changes and not others.
|
|
331
|
+
|
|
332
|
+
### Files
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
with open("data.json", "rb") as f:
|
|
336
|
+
doc = fastsimdjson.load(f) # like json.load: f.read(), then loads
|
|
337
|
+
doc = fastsimdjson.load_file("data.json")
|
|
338
|
+
view = fastsimdjson.parse_file("data.json") # lazy, like parse
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
`load_file(path)` and `parse_file(path)` accept a `str`, `bytes` or
|
|
342
|
+
`os.PathLike` path and raise `OSError` (e.g. `FileNotFoundError`) when the
|
|
343
|
+
file cannot be read.
|
|
344
|
+
|
|
345
|
+
### `loads_many` and `parse_many`: streams of documents
|
|
346
|
+
|
|
347
|
+
```python
|
|
348
|
+
for record in fastsimdjson.loads_many(open("log.ndjson", "rb").read()):
|
|
349
|
+
...
|
|
350
|
+
for view in fastsimdjson.parse_many(data): # lazy views, like parse
|
|
351
|
+
...
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
Both return an iterator over the documents of `data` (`bytes`, `bytearray`,
|
|
355
|
+
`memoryview` or `str`). The `format` keyword selects how documents are
|
|
356
|
+
separated:
|
|
357
|
+
|
|
358
|
+
| `format` | input |
|
|
359
|
+
|---|---|
|
|
360
|
+
| `"whitespace"` (default) | documents separated by white space, including NDJSON and JSON Lines |
|
|
361
|
+
| `"lines"` | one document per line (NDJSON, JSON Lines) |
|
|
362
|
+
| `"json_seq"` | RFC 7464 JSON text sequences (each document preceded by `\x1e`) |
|
|
363
|
+
| `"comma"` | documents separated by commas: `{...}, {...}` (simdjson also accepts white space between them) |
|
|
364
|
+
| `"array"` | the elements of one array: `[{...}, {...}]` |
|
|
365
|
+
|
|
366
|
+
To read a file without copying it, map it in memory:
|
|
367
|
+
|
|
368
|
+
```python
|
|
369
|
+
import mmap
|
|
370
|
+
with open("log.ndjson", "rb") as f, mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m:
|
|
371
|
+
names = fastsimdjson.map_many(fn, memoryview(m), threads=64)
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
A `memoryview` of `bytes`, or of a file mapped with `mmap.ACCESS_READ` (as
|
|
375
|
+
here), is read in place, and the mapped file must not change meanwhile;
|
|
376
|
+
other buffers (a `bytearray`, a view of a writable mmap) are copied first.
|
|
377
|
+
|
|
378
|
+
simdjson parses the input in batches (`batch_size`, 1 MB by default); a
|
|
379
|
+
larger document is handled automatically. In every format, documents that
|
|
380
|
+
simdjson rejects are handled as in `loads`: a document that `json` accepts is
|
|
381
|
+
returned, otherwise `JSONDecodeError` reports `json`'s message and the
|
|
382
|
+
position in the whole input. A truncated last document is an error. The
|
|
383
|
+
views returned by `parse_many` remain valid after the iterator moves on.
|
|
384
|
+
|
|
385
|
+
### Streams on several threads: `threads=N` and `map_many`
|
|
386
|
+
|
|
387
|
+
On free-threaded Python (3.14t and newer), a stream can be parsed by several
|
|
388
|
+
threads at once:
|
|
389
|
+
|
|
390
|
+
```python
|
|
391
|
+
docs = fastsimdjson.loads_many(data, threads=16) # a list, in order
|
|
392
|
+
views = fastsimdjson.parse_many(data, threads=16) # a list of views
|
|
393
|
+
# fn runs in the threads too, on each document; its results, in order:
|
|
394
|
+
scores = fastsimdjson.map_many(score, data, threads=16)
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
With `threads`, `loads_many` and `parse_many` return a list of all the
|
|
398
|
+
documents instead of an iterator. `map_many(fn, data, **kw)` returns
|
|
399
|
+
`[fn(doc) for doc in loads_many(data, **kw)]` (`parse_many` with
|
|
400
|
+
`lazy=True`), with `fn` called in the threads. `threads=0` (or `None`) uses
|
|
401
|
+
one thread per processor; small inputs use fewer threads (about one per
|
|
402
|
+
512 KB). The workers share one counter. Each claims the next 256 KB and
|
|
403
|
+
moves both ends forward to a newline (to the next `\x1e` for `json_seq`);
|
|
404
|
+
the pieces are adjacent. On `lines` and `json_seq` a document is never cut,
|
|
405
|
+
and one longer than 256 KB is parsed with the piece that started before it.
|
|
406
|
+
`threads` works with the formats `"whitespace"`, `"lines"` and `"json_seq"`.
|
|
407
|
+
|
|
408
|
+
The results, and the exceptions, are those of one thread: when a chunk
|
|
409
|
+
contains an error or a document that only `json` accepts, the documents
|
|
410
|
+
before it are kept and the rest is parsed in the calling thread, as without
|
|
411
|
+
threads. An exception raised by `fn` is raised again, for the first document
|
|
412
|
+
where it happened; `fn` may then have been called on later documents, in
|
|
413
|
+
other threads, and it must be safe to call from several threads at once.
|
|
414
|
+
|
|
415
|
+
The worker threads are started by the first call that needs them and kept
|
|
416
|
+
for later calls (they wait, detached from the interpreter, between calls,
|
|
417
|
+
and stop at exit). `map_many` scales best: each document is built, used and
|
|
418
|
+
freed in the same thread, and only `fn`'s results come back. `loads_many` must return every
|
|
419
|
+
document, and the Python objects of all of them, to the calling thread.
|
|
420
|
+
With the GIL (the default build of Python), the calling thread does all the
|
|
421
|
+
work: `threads` has no effect on speed.
|
|
422
|
+
|
|
423
|
+
With `drop_none=True`, `map_many` leaves out the results that are `None`:
|
|
424
|
+
when `fn` keeps only a few documents, the threads do not build a list
|
|
425
|
+
entry for each of the others, and the calling thread has fewer to collect.
|
|
426
|
+
|
|
427
|
+
```python
|
|
428
|
+
def starred(event):
|
|
429
|
+
kind, repo = event
|
|
430
|
+
return repo if kind == "WatchEvent" else None
|
|
431
|
+
|
|
432
|
+
names = fastsimdjson.map_many(starred, data, threads=64, drop_none=True,
|
|
433
|
+
fields=["/type", "/repo/name"])
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
### Only some fields: `fields=`
|
|
437
|
+
|
|
438
|
+
When you need a few values from each document, `fields` extracts them
|
|
439
|
+
without building the documents: only the values found become Python
|
|
440
|
+
objects.
|
|
441
|
+
|
|
442
|
+
```python
|
|
443
|
+
events = fastsimdjson.loads_many(data, fields=["/type", "/payload/action"])
|
|
444
|
+
# (type, action) for each document
|
|
445
|
+
|
|
446
|
+
def count(t):
|
|
447
|
+
kind, action, language = t
|
|
448
|
+
...
|
|
449
|
+
fastsimdjson.map_many(count, data, threads=64,
|
|
450
|
+
fields=["/type", "/payload/action",
|
|
451
|
+
"/payload/pull_request/base/repo/language"])
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
`loads_many` and `map_many` accept `fields`: a JSON Pointer (RFC 6901), or a
|
|
455
|
+
list or tuple of them. Each document then becomes the value at the pointer,
|
|
456
|
+
or a tuple of the values at the pointers, in order. A value that is an
|
|
457
|
+
object or an array is built in full. A pointer that leads nowhere (a
|
|
458
|
+
missing key, an index past the end, or a step into a scalar) gives
|
|
459
|
+
`default`, `None` unless you pass another value; a JSON `null` gives `None`.
|
|
460
|
+
As with `at_pointer`, a key that appears twice in an object refers to its
|
|
461
|
+
first occurrence. `fields` cannot be combined with `lazy=True`.
|
|
462
|
+
|
|
463
|
+
With the formats `"whitespace"` and `"lines"`, `fields` reads the documents
|
|
464
|
+
with simdjson's On-Demand API: the top level of each document, the keys on
|
|
465
|
+
the way to the pointers and the values found there are read and validated,
|
|
466
|
+
so a malformed or incomplete document is still an error. The values that no
|
|
467
|
+
pointer reaches are skipped without being checked, so an invalid value
|
|
468
|
+
elsewhere in a document (`{"a": 1, "b": tru}` with `fields="/a"`) goes
|
|
469
|
+
unnoticed. A value that is read is handled as without `fields`: an error
|
|
470
|
+
raises `JSONDecodeError`, and a document that only `json` accepts is decoded
|
|
471
|
+
by `json`. The whole documents are parsed (and validated) with the other
|
|
472
|
+
formats, and when a pointer is `""` or leads into the value of another
|
|
473
|
+
pointer (`["/a", "/a/b"]`).
|
|
474
|
+
|
|
475
|
+
On 7.6 GB of GitHub events, `map_many` with three fields ran at 2.2 GB/s on
|
|
476
|
+
one core of an Intel Xeon Gold 6548N, against 1.8 GB/s with lazy views
|
|
477
|
+
(`lazy=True`). With 64 threads (free-threaded Python 3.14.10t), it ran at
|
|
478
|
+
84 GB/s (lazy views: 76 GB/s).
|
|
479
|
+
|
|
480
|
+
### Arrow: `loads_arrow`, `load_arrow_file` and `dumpb_arrow`
|
|
481
|
+
|
|
482
|
+
```python
|
|
483
|
+
import pyarrow as pa
|
|
484
|
+
table = pa.table(fastsimdjson.loads_arrow(data)) # inferring the schema
|
|
485
|
+
schema = pa.schema([("id", pa.int64()), ("name", pa.string())])
|
|
486
|
+
table = pa.table(fastsimdjson.load_arrow_file("log.ndjson", schema, threads=8))
|
|
487
|
+
frame = polars.DataFrame(fastsimdjson.loads_arrow(data, schema))
|
|
488
|
+
data = fastsimdjson.dumpb_arrow(table) # b'{"id":1,"name":"x"}\n...'
|
|
489
|
+
data = fastsimdjson.dumpb_arrow(frame, threads=8) # a polars.DataFrame
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
`loads_arrow(data, schema=None, *, block_size=1048576,
|
|
493
|
+
error_on_unknown_fields=False, validate_skipped=True, threads=None)` reads
|
|
494
|
+
NDJSON (one object per record) into Arrow record batches. The input is
|
|
495
|
+
`bytes`, `bytearray`, `memoryview` or `str`, read in place.
|
|
496
|
+
`load_arrow_file(path, ...)` maps the file in memory and reads it the same way
|
|
497
|
+
(the file must not shrink meanwhile). Both return a `fastsimdjson.ArrowStream`,
|
|
498
|
+
which goes to any library that supports the [Arrow PyCapsule
|
|
499
|
+
interface](https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html),
|
|
500
|
+
without a copy: `pyarrow.table(s)`, `pyarrow.RecordBatchReader.from_stream(s)`,
|
|
501
|
+
`polars.DataFrame(s)`, `duckdb.from_arrow(s)`... Like a `RecordBatchReader`,
|
|
502
|
+
it hands over its batches once (`ValueError` afterwards); `pyarrow.schema(s)`
|
|
503
|
+
gives its schema at any time, and `s.num_rows` the number of records (`None`
|
|
504
|
+
once the batches are taken). fastsimdjson does not depend on pyarrow.
|
|
505
|
+
|
|
506
|
+
* The schema is a `pyarrow.Schema` (or any object with `__arrow_c_schema__`)
|
|
507
|
+
whose fields are booleans, 64-bit integers, unsigned 64-bit integers,
|
|
508
|
+
doubles, strings, timestamps, structs and lists of these, or null. A field absent from a
|
|
509
|
+
record is null; a key absent from the schema is skipped, or an error with
|
|
510
|
+
`error_on_unknown_fields=True`. Skipped values are checked to be valid
|
|
511
|
+
JSON, unless `validate_skipped=False` (then only their structure is).
|
|
512
|
+
* Without a schema, it is inferred as pyarrow's JSON reader does: `null`,
|
|
513
|
+
`true`/`false`, integers that fit in 64 bits (`int64`), other numbers
|
|
514
|
+
(`double`), strings, objects (structs, with the union of their fields in
|
|
515
|
+
order of first appearance) and arrays (lists). A field of nulls takes the
|
|
516
|
+
type of its other values and integers become doubles next to other
|
|
517
|
+
numbers; other mixes of types raise `ValueError`. Strings stay strings (no
|
|
518
|
+
timestamps).
|
|
519
|
+
* A timestamp field (`pa.timestamp("us", tz="UTC")`, any unit, with or
|
|
520
|
+
without a time zone) reads ISO 8601 strings, such as the `created_at` of
|
|
521
|
+
GH Archive events (`"2015-01-01T15:00:00Z"`): a date (a four-digit year), then optionally a
|
|
522
|
+
time (`T` or a space, then `15`, `15:00`, `15:00:00` or
|
|
523
|
+
`15:00:00.123456`), then optionally `Z` or an offset (`+05:00`). A time
|
|
524
|
+
without `Z` or an offset is UTC. An integer is a count of units since 1970.
|
|
525
|
+
As with `pyarrow.json.read_json` (which returns the same table from the
|
|
526
|
+
strings it accepts; a lowercase `t` or `z` and a comma before the fraction
|
|
527
|
+
are accepted here too), a fraction
|
|
528
|
+
with more digits than the unit raises `ValueError`, as does an invalid
|
|
529
|
+
date. Reading the timestamps costs next to nothing: GH Archive with
|
|
530
|
+
`created_at` as `timestamp[us, UTC]` reads at 2.11 GB/s on one core, as
|
|
531
|
+
with strings, against 0.45 GB/s for pyarrow and 0.21 GB/s for polars.
|
|
532
|
+
* A new batch starts every `block_size` bytes of input. With `threads=N`
|
|
533
|
+
(`threads=0` or `None`: one per processor), N threads read blocks of
|
|
534
|
+
`block_size` bytes, cut at line ends: a record must then fit on one line.
|
|
535
|
+
When that fails, the input is read again on one thread, so that the errors
|
|
536
|
+
(`ValueError`, with the record number and the field) are the same with any
|
|
537
|
+
number of threads.
|
|
538
|
+
|
|
539
|
+
`dumpb_arrow(data, *, write_nulls=True, threads=None)` writes Arrow data as
|
|
540
|
+
NDJSON `bytes`, one object per row: a `pyarrow.Table`, `RecordBatch`,
|
|
541
|
+
`RecordBatchReader` or `StructArray`, a `polars.DataFrame`, a DuckDB
|
|
542
|
+
relation, an `ArrowStream`, or any object with `__arrow_c_stream__` or
|
|
543
|
+
`__arrow_c_array__`. It writes integers of 8 to 64 bits, floats and doubles
|
|
544
|
+
(as `dumpb` writes them, NaN and infinities as `null`), booleans, strings
|
|
545
|
+
(including large strings and string views), timestamps (ISO 8601 in UTC,
|
|
546
|
+
`"2015-01-01T15:00:00.250000Z"`, with `Z` when the timestamp has a time zone
|
|
547
|
+
and the fraction when it is not zero), dates (`"2015-01-01"`), structs and
|
|
548
|
+
lists (including large and fixed-size lists); other types raise
|
|
549
|
+
`ValueError`. With
|
|
550
|
+
`write_nulls=False`, null fields are omitted. With `threads=N`, N threads
|
|
551
|
+
write ranges of rows into the result. The output reads back with
|
|
552
|
+
`loads_arrow`, and with `loads_many(data, format="lines")`, except for
|
|
553
|
+
timestamps and dates outside the years 0000 to 9999: their year is written
|
|
554
|
+
with more digits or a minus sign, which `loads_arrow` does not read (nor
|
|
555
|
+
does pyarrow).
|
|
556
|
+
|
|
557
|
+
These functions release the GIL and run their threads outside Python, with
|
|
558
|
+
or without free threading. They are built on
|
|
559
|
+
[simdjson_arrow](https://github.com/simdjson/simdjson_arrow), a C++ library
|
|
560
|
+
(vendored).
|
|
561
|
+
|
|
562
|
+
### MessagePack: `json_to_msgpack` and `msgpack_to_json`
|
|
563
|
+
|
|
564
|
+
```python
|
|
565
|
+
packed = fastsimdjson.json_to_msgpack(b'{"id": 1, "tags": ["a", "b"], "score": 2.5}')
|
|
566
|
+
# b'\x83\xa2id\x01\xa4tags\x92\xa1a\xa1b\xa5score\xcb@\x04\x00\x00\x00\x00\x00\x00'
|
|
567
|
+
msgpack.unpackb(packed) # {'id': 1, 'tags': ['a', 'b'], 'score': 2.5}
|
|
568
|
+
fastsimdjson.msgpack_to_json(packed) # b'{"id":1,"tags":["a","b"],"score":2.5}'
|
|
569
|
+
stream = fastsimdjson.json_to_msgpack(ndjson, ndjson=True, threads=8) # one value per document
|
|
570
|
+
lines = fastsimdjson.msgpack_to_json(stream, stream=True, threads=8) # NDJSON again
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
`json_to_msgpack(data, *, ndjson=False, block_size=1048576, threads=None)`
|
|
574
|
+
converts JSON (`bytes`, `bytearray`, `memoryview` or `str`, read in place) to
|
|
575
|
+
MessagePack `bytes` in the most compact encoding: the same bytes as
|
|
576
|
+
`msgpack.packb(json.loads(data))` (but for duplicate keys, which
|
|
577
|
+
`json.loads` drops, and integers beyond 64 bits). Integers take the smallest integer encoding
|
|
578
|
+
(integers beyond 64 bits become doubles), other numbers are doubles, and
|
|
579
|
+
strings, arrays and objects take the smallest header for their size; keys keep
|
|
580
|
+
their order, duplicates too. With `ndjson=True`, the input holds documents
|
|
581
|
+
separated by whitespace (NDJSON, JSON Lines), and each becomes one MessagePack
|
|
582
|
+
value, one after the other.
|
|
583
|
+
|
|
584
|
+
`msgpack_to_json(data, *, stream=False, validate_utf8=True,
|
|
585
|
+
binary_as_base64=True, ext_as_null=False, block_size=1048576, threads=None)`
|
|
586
|
+
converts MessagePack (any bytes-like object) to compact JSON `bytes`. Floats
|
|
587
|
+
get the shortest representation that gives them back, as Python's `repr`
|
|
588
|
+
(`1.0`, not `1`; `1e-05`; NaN and infinities as `null`), binary values as base64 strings, map keys that are not strings as
|
|
589
|
+
strings (`"1"`, `"true"`, `"null"`); extension values (timestamps too) raise
|
|
590
|
+
`ValueError`, or become `null` with `ext_as_null=True`. Strings must be valid
|
|
591
|
+
UTF-8 (unless `validate_utf8=False`). With `stream=True`, the input holds a
|
|
592
|
+
sequence of values (what `json_to_msgpack(..., ndjson=True)` or several
|
|
593
|
+
`msgpack.packb` calls give), and each becomes one line of NDJSON.
|
|
594
|
+
|
|
595
|
+
* Invalid input raises `ValueError`: `"TAPE_ERROR: ... (document 12)"` for
|
|
596
|
+
JSON, `"truncated string (byte 1234)"` for MessagePack.
|
|
597
|
+
* With `threads=N` (`threads=0` or `None`: one per processor), NDJSON and
|
|
598
|
+
streams are converted on N threads, in blocks of about `block_size` bytes.
|
|
599
|
+
A JSON document must then fit on one line; when that fails, the input is
|
|
600
|
+
converted again on one thread, so that errors are the same with any number
|
|
601
|
+
of threads. Single documents use one thread.
|
|
602
|
+
* The functions release the GIL and run their threads outside Python, with or
|
|
603
|
+
without free threading, and write their result straight into the `bytes`
|
|
604
|
+
object (no copy). No Python object is made from the data.
|
|
605
|
+
|
|
606
|
+
They are built on
|
|
607
|
+
[simdjson_msgpack](https://github.com/simdjson/simdjson_msgpack), a C++ library
|
|
608
|
+
(vendored).
|
|
609
|
+
|
|
610
|
+
### Threads and free-threaded Python
|
|
611
|
+
|
|
612
|
+
The module is marked free-threading compatible (`Py_MOD_GIL_NOT_USED` on
|
|
613
|
+
Python 3.13 and newer), so importing it on a free-threaded build does not
|
|
614
|
+
re-enable the GIL. It is tested with many threads parsing, serializing and
|
|
615
|
+
sharing views and stream iterators on free-threaded Python 3.14 and 3.15.
|
|
616
|
+
|
|
617
|
+
`bytes` and `str` inputs are read in place. A `bytearray` or `memoryview`
|
|
618
|
+
could be changed by another thread while we read it, so it is copied first:
|
|
619
|
+
always by `loads_many`, `parse_many` and `map_many` (the iterator outlives
|
|
620
|
+
the call) and by `loads_arrow`, `json_to_msgpack` and `msgpack_to_json`
|
|
621
|
+
(which release the GIL), and only on a free-threaded build by `loads` and
|
|
622
|
+
`parse`. The exception: `loads_many`, `parse_many` and `map_many` read a
|
|
623
|
+
read-only, contiguous `memoryview` of `bytes`, or of a file mapped with
|
|
624
|
+
`mmap.ACCESS_READ`, in place (the file must not change meanwhile). Other
|
|
625
|
+
read-only views (`memoryview(bytearray).toreadonly()`, a read-only view of a
|
|
626
|
+
writable mmap) are copied, since what they show can still change. Likewise, on a free-threaded build, `dumps` and `dumpb` hold
|
|
627
|
+
references to the items of the containers they serialize.
|
|
628
|
+
|
|
629
|
+
### `release`
|
|
630
|
+
|
|
631
|
+
`release()` frees the simdjson parser and the string caches kept by the
|
|
632
|
+
calling thread. On free-threaded Python, it also has the idle worker threads
|
|
633
|
+
of the parallel streams (`threads=N`) free theirs; a worker busy with a
|
|
634
|
+
stream of another thread does so when it is done. Views returned by `parse`
|
|
635
|
+
remain valid.
|
|
636
|
+
|
|
637
|
+
## Build and test
|
|
638
|
+
|
|
639
|
+
Python 3.10 or newer, and a C++17 compiler (clang, GCC or MSVC). The simdjson
|
|
640
|
+
5.1.1 and simdutf 9.2.1 amalgamations, simdjson_arrow 0.1.5 (NDJSON to Arrow
|
|
641
|
+
and back: `simdjson_arrow.h` and `simdjson_arrow.cpp`) and simdjson_msgpack
|
|
642
|
+
0.1.1 (JSON to MessagePack and back: `simdjson_msgpack.h` and
|
|
643
|
+
`simdjson_msgpack.cpp`), both under the Apache-2.0 license, are already in
|
|
644
|
+
`vendor/`. simdjson_arrow includes zmij 1.2 (shortest float formatting, MIT
|
|
645
|
+
license), which simdjson_msgpack, `dumps` and `dumpb` also use
|
|
646
|
+
(`vendor/zmij.h`).
|
|
647
|
+
The tests of the Arrow functions (`tests/test_arrow.py`) run when pyarrow is
|
|
648
|
+
installed (and polars, for one of them); otherwise they are skipped.
|
|
649
|
+
|
|
650
|
+
pip:
|
|
651
|
+
|
|
652
|
+
```sh
|
|
653
|
+
python3 -m venv .venv
|
|
654
|
+
. .venv/bin/activate
|
|
655
|
+
python -m pip install -U pip setuptools
|
|
656
|
+
python -m pip install -e ".[test]"
|
|
657
|
+
pytest tests
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
uv:
|
|
661
|
+
|
|
662
|
+
```sh
|
|
663
|
+
uv venv
|
|
664
|
+
. .venv/bin/activate
|
|
665
|
+
uv pip install -e ".[test]"
|
|
666
|
+
pytest tests
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
Either one builds the extension and makes `import fastsimdjson` work in the
|
|
670
|
+
virtualenv. uv uses the `setuptools` build requirement from `pyproject.toml`,
|
|
671
|
+
so it does not need a separate setuptools install for this path.
|
|
672
|
+
|
|
673
|
+
To compile the extension in the tree instead, install setuptools and pytest
|
|
674
|
+
into the same virtualenv, then:
|
|
675
|
+
|
|
676
|
+
```sh
|
|
677
|
+
python -m pip install setuptools pytest
|
|
678
|
+
python setup.py build_ext --inplace
|
|
679
|
+
PYTHONPATH=src python -m pytest tests
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
```sh
|
|
683
|
+
uv pip install setuptools pytest
|
|
684
|
+
python setup.py build_ext --inplace
|
|
685
|
+
PYTHONPATH=src python -m pytest tests
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
Recent setuptools copies the `.so` next to `src/fastsimdjson.cpp`, which is
|
|
689
|
+
why `PYTHONPATH=src` is required for the in-place build.
|
|
690
|
+
|
|
691
|
+
`tests/test_loads.py` compares `loads` with `json.loads` on types and key
|
|
692
|
+
order; `tests/test_lazy.py` checks the views returned by `parse` the same way,
|
|
693
|
+
`tests/test_dumps.py` compares `dumps` with `json.dumps` and `dumpb` with
|
|
694
|
+
orjson (output and exceptions; the orjson comparisons are skipped when orjson
|
|
695
|
+
is not installed, and recorded orjson results are checked either way), and
|
|
696
|
+
`tests/test_stream.py` and `tests/test_files.py` cover streams and files, and
|
|
697
|
+
`tests/test_minify.py` covers `minify`. The suite
|
|
698
|
+
covers scalars, integers past 64 bits, UTF-8 strings at every length from 0 to
|
|
699
|
+
199, the key cache, random documents, rejected input, deep
|
|
700
|
+
nesting, padding at a page boundary, a saturated array count, reference
|
|
701
|
+
counts, and release of a parser that has grown past 64 MB. The corpus test
|
|
702
|
+
is skipped until simdjson-data is checked out beside the project:
|
|
703
|
+
|
|
704
|
+
```sh
|
|
705
|
+
git clone --depth 1 https://github.com/simdjson/simdjson-data.git
|
|
706
|
+
pytest tests
|
|
707
|
+
# or: JSONDIR=/path/to/jsonexamples pytest tests
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
The suite builds an ~80 MB document and a list of 16,777,221 integers, so
|
|
711
|
+
give it some RAM.
|
|
712
|
+
|
|
713
|
+
The benchmark scripts need simdjson-data and the `bench` extra (orjson,
|
|
714
|
+
jiter, msgspec, pysimdjson, cysimdjson). `bench.py` times `loads` against
|
|
715
|
+
`json.loads`, orjson and jiter, `bench_lazy.py` times `parse` against pysimdjson and
|
|
716
|
+
cysimdjson, `bench_dumps.py` times `dumps` against `json.dumps` and `dumpb`
|
|
717
|
+
against orjson and msgspec, `bench_many.py` times `loads_many`, and
|
|
718
|
+
`bench_minify.py` times `minify` against parsing and serializing again.
|
|
719
|
+
|
|
720
|
+
pip:
|
|
721
|
+
|
|
722
|
+
```sh
|
|
723
|
+
python -m pip install -e ".[bench]"
|
|
724
|
+
python bench.py
|
|
725
|
+
python bench_lazy.py
|
|
726
|
+
python bench_dumps.py
|
|
727
|
+
python bench_many.py
|
|
728
|
+
python bench_minify.py
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
uv:
|
|
732
|
+
|
|
733
|
+
```sh
|
|
734
|
+
uv pip install -e ".[bench]"
|
|
735
|
+
python bench.py
|
|
736
|
+
python bench_lazy.py
|
|
737
|
+
python bench_dumps.py
|
|
738
|
+
python bench_many.py
|
|
739
|
+
python bench_minify.py
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
## Benchmarks
|
|
743
|
+
|
|
744
|
+
Intel Xeon Gold 6548N (Emerald Rapids), one core unless stated otherwise,
|
|
745
|
+
Python 3.14.10 (free-threaded Python 3.14.10t for the streams on several
|
|
746
|
+
threads), fastsimdjson 1.0.0 (simdjson 5.1.1) built with GCC 16.2, the 22
|
|
747
|
+
files of [simdjson-data](https://github.com/simdjson/simdjson-data). The scripts are in
|
|
748
|
+
this repository and in
|
|
749
|
+
[the blog repository](https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/tree/master/2026/09/pysimdjson).
|
|
750
|
+
|
|
751
|
+
### Whole documents
|
|
752
|
+
|
|
753
|
+
Each parser produces the whole document as Python objects. Speed is the
|
|
754
|
+
geometric mean over the 22 files (higher is better).
|
|
755
|
+
|
|
756
|
+
<picture>
|
|
757
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_loads_dark.png">
|
|
758
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_loads.png" width="80%" alt="Parsing JSON in Python: fastsimdjson loads 0.77 GB/s, orjson 0.60, jiter 0.54, msgspec 0.53, jiter without its string cache 0.44, pysimdjson 0.44, cysimdjson 0.43, ujson 0.37, python-rapidjson 0.24, simplejson 0.23, json 0.22">
|
|
759
|
+
</picture>
|
|
760
|
+
|
|
761
|
+
| parser | GB/s | vs `json.loads` |
|
|
762
|
+
|---|---:|---:|
|
|
763
|
+
| json (standard library) | 0.22 | 1.00× |
|
|
764
|
+
| simplejson 4.3.0 | 0.23 | 1.04× |
|
|
765
|
+
| python-rapidjson 1.26 | 0.24 | 1.08× |
|
|
766
|
+
| ujson 6.1.0 | 0.37 | 1.64× |
|
|
767
|
+
| cysimdjson 26.27 | 0.43 | 1.95× |
|
|
768
|
+
| pysimdjson 8.0.0 | 0.44 | 1.97× |
|
|
769
|
+
| jiter 0.19.0 (no string cache) | 0.44 | 1.98× |
|
|
770
|
+
| msgspec 0.22.0 | 0.53 | 2.38× |
|
|
771
|
+
| jiter 0.19.0 (string cache, the default) | 0.54 | 2.43× |
|
|
772
|
+
| orjson 3.13.0 | 0.60 | 2.70× |
|
|
773
|
+
| **fastsimdjson `loads`** | **0.77** | **3.46×** |
|
|
774
|
+
|
|
775
|
+
fastsimdjson is the fastest on 21 of the 22 files. orjson is 12% faster on
|
|
776
|
+
`numbers.json`, an array of floating-point numbers. On numbers, both spend
|
|
777
|
+
most of their time creating Python floats, which costs the same in both:
|
|
778
|
+
simdjson parses the numbers of these files faster than orjson's parser
|
|
779
|
+
(yyjson), but that is a small part of the total. Part of the gain comes from
|
|
780
|
+
pausing the garbage collector while the objects are built. fastsimdjson's
|
|
781
|
+
lead over orjson is 1.28× with the collector enabled and 1.18× with it
|
|
782
|
+
disabled (geometric means); without the collector, orjson is also 3 to 4%
|
|
783
|
+
faster on `canada.json` and `mesh.json`. yyjson 4.0.6 is left out: it
|
|
784
|
+
returns wrong strings for non-ASCII text. jiter (the parser of pydantic)
|
|
785
|
+
caches strings by default; turning the cache off (`cache_mode=False`) makes it
|
|
786
|
+
slower here.
|
|
787
|
+
|
|
788
|
+
Parsing is no longer the bottleneck. simdjson alone parses these files at
|
|
789
|
+
3.1 GB/s. It accounts for about a third of the time of `loads`; the rest goes
|
|
790
|
+
into creating Python objects. Freeing those objects later costs about a sixth
|
|
791
|
+
of the total. Even if parsing took no time at all, `loads` would be less than
|
|
792
|
+
1.5 times faster.
|
|
793
|
+
|
|
794
|
+
### Parts of documents with `parse`
|
|
795
|
+
|
|
796
|
+
If you only need a few values, `parse` creates only those. Extracting the id
|
|
797
|
+
and the screen name of the 100 statuses of `twitter.json`:
|
|
798
|
+
|
|
799
|
+
<picture>
|
|
800
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_parse_dark.png">
|
|
801
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_parse.png" width="80%" alt="Reading part of twitter.json: fastsimdjson parse 156 µs, pysimdjson 169, cysimdjson 235, msgspec 337, fastsimdjson loads 844, orjson 971, jiter 1156, jiter without its string cache 1602, json 3977">
|
|
802
|
+
</picture>
|
|
803
|
+
|
|
804
|
+
| method | µs |
|
|
805
|
+
|---|---:|
|
|
806
|
+
| `json.loads` | 3977 |
|
|
807
|
+
| jiter (no string cache) | 1602 |
|
|
808
|
+
| jiter (string cache) | 1156 |
|
|
809
|
+
| orjson | 971 |
|
|
810
|
+
| fastsimdjson `loads` | 844 |
|
|
811
|
+
| msgspec (typed `Struct`) | 337 |
|
|
812
|
+
| cysimdjson (lazy) | 235 |
|
|
813
|
+
| pysimdjson (lazy) | 169 |
|
|
814
|
+
| **fastsimdjson `parse`** | **156** |
|
|
815
|
+
|
|
816
|
+
Here `parse` is about 25 times faster than `json.loads` and 5.4 times faster
|
|
817
|
+
than `loads`. Most of its time is the simdjson parse itself.
|
|
818
|
+
|
|
819
|
+
### Typed decoding
|
|
820
|
+
|
|
821
|
+
`loads(data, type=T)` against msgspec (`msgspec.json.Decoder(T)` with the
|
|
822
|
+
same dataclasses, and with the equivalent `msgspec.Struct` classes), pydantic
|
|
823
|
+
(`TypeAdapter(...).validate_json`, with the equivalent models) and
|
|
824
|
+
`cattrs.structure(orjson.loads(data), T)`. Three cases:
|
|
825
|
+
|
|
826
|
+
* **twitter:** `twitter.json` as a dataclass of 100 statuses, six fields of
|
|
827
|
+
each status and four of its user, the rest skipped.
|
|
828
|
+
* **citm:** `citm_catalog.json` typed in full (dicts of dataclasses, lists of
|
|
829
|
+
nested dataclasses).
|
|
830
|
+
* **gh:** 64 MB of GH Archive events (NDJSON), six fields of each event
|
|
831
|
+
(`loads_many(data, type=T)`, one call per line for the others).
|
|
832
|
+
|
|
833
|
+
MB/s, one core of an Intel Xeon Gold 6548N, Python 3.14.10, best of 3 runs
|
|
834
|
+
of 7 (`python bench_typed.py simdjson-data/jsonexamples gharchive.ndjson`):
|
|
835
|
+
|
|
836
|
+
<picture>
|
|
837
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_typed_dark.png">
|
|
838
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_typed.png" width="80%" alt="JSON to dataclasses on one core, GB/s. twitter.json, a few fields: fastsimdjson 2.60, msgspec Struct 1.52, msgspec dataclasses 1.43, pydantic 0.66, orjson with cattrs 0.55. citm_catalog.json typed in full: fastsimdjson 0.98, msgspec Struct 0.63, msgspec dataclasses 0.47, pydantic 0.19, orjson with cattrs 0.19. GH Archive NDJSON, 6 fields: fastsimdjson 2.05, msgspec Struct 1.30, msgspec dataclasses 1.16, pydantic 0.51, orjson with cattrs 0.42">
|
|
839
|
+
</picture>
|
|
840
|
+
|
|
841
|
+
| | twitter | citm | gh |
|
|
842
|
+
|---|---:|---:|---:|
|
|
843
|
+
| **fastsimdjson `loads(type=)`** | **2597** | **981** | **2055** |
|
|
844
|
+
| msgspec (Struct) | 1520 | 632 | 1296 |
|
|
845
|
+
| msgspec (dataclass) | 1428 | 467 | 1156 |
|
|
846
|
+
| pydantic 2.14 | 659 | 192 | 514 |
|
|
847
|
+
| orjson + cattrs | 553 | 187 | 420 |
|
|
848
|
+
| fastsimdjson `loads` (dicts, untyped) | 933 | 1074 | 431 |
|
|
849
|
+
|
|
850
|
+
fastsimdjson is 1.5 to 1.7 times as fast as msgspec with Structs, 1.8 to 2.1
|
|
851
|
+
times as fast with the same dataclasses, and 3.9 to 5.1 times as fast as
|
|
852
|
+
pydantic. When most of the document is not needed (twitter, gh), typed
|
|
853
|
+
decoding is also 2.8 to 4.8 times as fast as building the dicts.
|
|
854
|
+
|
|
855
|
+
On free-threaded Python 3.14.10t, `loads_many(data, type=T, threads=N)` read
|
|
856
|
+
the 886 MB GH Archive file into dataclasses at 1.82 GB/s on one thread,
|
|
857
|
+
12.0 GB/s on 8 and 33.3 GB/s on 32.
|
|
858
|
+
|
|
859
|
+
### `dumps` against `json.dumps`
|
|
860
|
+
|
|
861
|
+
<picture>
|
|
862
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_dumps_dark.png">
|
|
863
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_dumps.png" width="80%" alt="Writing JSON as str: fastsimdjson dumps 4.21 times faster than json.dumps">
|
|
864
|
+
</picture>
|
|
865
|
+
|
|
866
|
+
`dumps` returns exactly the `str` of `json.dumps` and is 4.2 times faster
|
|
867
|
+
(geometric mean over the 22 files; from 2.7 times on `citm_catalog.json` to
|
|
868
|
+
10.5 times on `canada.json`).
|
|
869
|
+
|
|
870
|
+
### `dumpb` against orjson and msgspec
|
|
871
|
+
|
|
872
|
+
`dumpb`, `orjson.dumps` and `msgspec.json.encode` produce compact UTF-8
|
|
873
|
+
`bytes`; `dumpb` and orjson produce identical bytes on every file. With the
|
|
874
|
+
standard library, the same compact bytes come from
|
|
875
|
+
`json.dumps(obj, separators=(",", ":"), ensure_ascii=False).encode()`.
|
|
876
|
+
Each call encodes a fresh copy of the document (`bench_dumps.py`), so no
|
|
877
|
+
encoder gains from what it cached on the objects at a previous call. One
|
|
878
|
+
core of an Intel Xeon Gold 6548N, Python 3.14.10, GCC 16.2, fastsimdjson
|
|
879
|
+
1.0.0, orjson 3.13.0, msgspec 0.22.0. Microseconds:
|
|
880
|
+
|
|
881
|
+
<picture>
|
|
882
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_dumpb_dark.png">
|
|
883
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_dumpb.png" width="80%" alt="Writing JSON as bytes, speed relative to json.dumps(...).encode(): fastsimdjson dumpb 7.95x, orjson 7.38x, msgspec 5.26x">
|
|
884
|
+
</picture>
|
|
885
|
+
|
|
886
|
+
| file | `json.dumps(...).encode()` | fastsimdjson `dumpb` | orjson | msgspec |
|
|
887
|
+
|---|---:|---:|---:|---:|
|
|
888
|
+
| twitter | 1804 | 258 | 313 | 387 |
|
|
889
|
+
| citm_catalog | 3114 | 651 | 666 | 716 |
|
|
890
|
+
| github_events | 211 | 21 | 20 | 32 |
|
|
891
|
+
| gsoc-2018 | 12153 | 956 | 1165 | 2095 |
|
|
892
|
+
| update-center | 2647 | 309 | 298 | 514 |
|
|
893
|
+
| canada | 37821 | 2743 | 2480 | 3978 |
|
|
894
|
+
| mesh | 8903 | 876 | 1049 | 1401 |
|
|
895
|
+
| numbers | 2473 | 201 | 204 | 332 |
|
|
896
|
+
|
|
897
|
+
Over the 22 files, `dumpb` is 8% faster than orjson (geometric mean), from
|
|
898
|
+
10% slower on `canada.json` (mostly floating-point numbers) and up to 6%
|
|
899
|
+
slower on the smallest files (`twitter_api_response.json`,
|
|
900
|
+
`github_events.json`) to 22% faster on `gsoc-2018.json`, whose strings are
|
|
901
|
+
mostly not ASCII, and 35% faster on `semanticscholar-corpus.json`. `dumpb` is
|
|
902
|
+
1.5 times faster than msgspec (orjson 1.4 times) and 8 times faster than
|
|
903
|
+
`json`. The comparison was run on a processor
|
|
904
|
+
with AVX-512, which `dumpb` and orjson both use to escape strings; other x64
|
|
905
|
+
processors use SSE2 and ARM processors NEON.
|
|
906
|
+
|
|
907
|
+
### `minify`
|
|
908
|
+
|
|
909
|
+
`minify` only removes whitespace. The other columns parse the document and
|
|
910
|
+
serialize it again, which also validates it and rewrites numbers. Geometric
|
|
911
|
+
mean over the 22 files, one core of an Intel Xeon Gold 6548N, Python 3.14.10,
|
|
912
|
+
GCC 16.2, orjson 3.13.0 (`bench_minify.py`):
|
|
913
|
+
|
|
914
|
+
<picture>
|
|
915
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_minify_dark.png">
|
|
916
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_minify.png" width="80%" alt="Minifying JSON in Python: fastsimdjson minify 18.64 GB/s, fastsimdjson dumpb(loads(s)) 0.54, orjson dumps(loads(s)) 0.44, json dumps(loads(s)) 0.11">
|
|
917
|
+
</picture>
|
|
918
|
+
|
|
919
|
+
| method | GB/s |
|
|
920
|
+
|---|---:|
|
|
921
|
+
| **fastsimdjson `minify`** | **18.64** |
|
|
922
|
+
| `loads` + `dumpb` | 0.54 |
|
|
923
|
+
| orjson `loads` + `dumps` | 0.44 |
|
|
924
|
+
| `json.loads` + `json.dumps` | 0.11 |
|
|
925
|
+
|
|
926
|
+
Per file, `minify` runs from 15.4 GB/s (`canada.json`) to 20.7 GB/s
|
|
927
|
+
(`apache_builds.json`).
|
|
928
|
+
|
|
929
|
+
### Writing NDJSON with `dumpb_many`
|
|
930
|
+
|
|
931
|
+
The 593,945 events of two hours of [GH Archive](https://www.gharchive.org/)
|
|
932
|
+
(1.87 GB of NDJSON), written back as NDJSON from Python objects. With the
|
|
933
|
+
GIL (Python 3.14.10), one thread: `orjson.dumps` on each record, joined, runs
|
|
934
|
+
at 0.93 GB/s, `dumpb` on each record at 0.93 GB/s, and `dumpb_many` at
|
|
935
|
+
1.49 GB/s. On free-threaded Python 3.14.10t, GB/s (up to 32 threads on the
|
|
936
|
+
cores of one socket, 64 on both):
|
|
937
|
+
|
|
938
|
+
| threads | 1 | 8 | 32 | 64 |
|
|
939
|
+
|---|---:|---:|---:|---:|
|
|
940
|
+
| `dumpb_many(records, threads=N)` | 1.48 | 5.6 | 11.6 | 10.5 |
|
|
941
|
+
|
|
942
|
+
### NDJSON to Arrow and back
|
|
943
|
+
|
|
944
|
+
Two files: 4 million flat records (611 MB, 9 scalar fields), and 304,929 GH
|
|
945
|
+
Archive events (886 MB), read with a schema of 6 of their fields (two of them
|
|
946
|
+
structs; the rest of each event is skipped), or inferring the schema. Reading
|
|
947
|
+
gives a `pyarrow.Table` (`pyarrow.table(fastsimdjson.loads_arrow(data,
|
|
948
|
+
schema, threads=N))`); the time includes the import into pyarrow. Writing
|
|
949
|
+
starts from that table: fastsimdjson `dumpb_arrow`, polars `write_ndjson` and
|
|
950
|
+
DuckDB `COPY ... TO` (on tmpfs). Python 3.14.10, pyarrow 26.0, polars 2.1,
|
|
951
|
+
DuckDB 2.0, fastsimdjson 1.0.0, Intel Xeon Gold 6548N (up to 32 threads on
|
|
952
|
+
the 32 cores of one socket; 64 threads on both sockets).
|
|
953
|
+
|
|
954
|
+
<picture>
|
|
955
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_read_1_dark.png">
|
|
956
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_read_1.png" width="80%" alt="Reading NDJSON into a pyarrow.Table on one thread: flat, fastsimdjson 0.76 GB/s, pyarrow 0.18, polars 0.27; GH Archive, fastsimdjson 1.96, pyarrow 0.46, polars 0.54; flat with an inferred schema, fastsimdjson 0.79, pyarrow 0.18, polars 0.09">
|
|
957
|
+
</picture>
|
|
958
|
+
|
|
959
|
+
<picture>
|
|
960
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_read_32_dark.png">
|
|
961
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_read_32.png" width="80%" alt="Reading NDJSON into a pyarrow.Table on 32 threads: flat, fastsimdjson 15.0 GB/s, pyarrow 5.3, polars 4.3; GH Archive, fastsimdjson 27.5, pyarrow 12.1, polars 4.0; flat with an inferred schema, fastsimdjson 14.4, pyarrow 5.0, polars 0.12">
|
|
962
|
+
</picture>
|
|
963
|
+
|
|
964
|
+
<picture>
|
|
965
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_write_1_dark.png">
|
|
966
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_write_1.png" width="80%" alt="Writing a pyarrow.Table as NDJSON on one thread: flat, fastsimdjson 0.92 GB/s, polars 0.45, DuckDB 0.32; GH Archive, fastsimdjson 1.08, polars 0.46, DuckDB 0.32">
|
|
967
|
+
</picture>
|
|
968
|
+
|
|
969
|
+
<picture>
|
|
970
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_write_32_dark.png">
|
|
971
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_arrow_write_32.png" width="80%" alt="Writing a pyarrow.Table as NDJSON on 32 threads: flat, fastsimdjson 9.6 GB/s, polars 1.6, DuckDB 4.8; GH Archive, fastsimdjson 4.8, polars 0.86, DuckDB 1.1">
|
|
972
|
+
</picture>
|
|
973
|
+
|
|
974
|
+
All the numbers, in GB/s:
|
|
975
|
+
|
|
976
|
+
| | threads | 1 | 8 | 32 | 64 |
|
|
977
|
+
|---|---|---:|---:|---:|---:|
|
|
978
|
+
| flat, read | fastsimdjson `loads_arrow` | **0.76** | **5.7** | **15.0** | **18.0** |
|
|
979
|
+
| | pyarrow `read_json` | 0.18 | 1.4 | 5.3 | 7.8 |
|
|
980
|
+
| | polars `read_ndjson` | 0.27 | 2.0 | 4.3 | 3.1 |
|
|
981
|
+
| flat, read, inferring the schema | fastsimdjson `loads_arrow` | **0.79** | **5.6** | **14.4** | **12.5** |
|
|
982
|
+
| | pyarrow `read_json` | 0.18 | 1.4 | 5.0 | 6.8 |
|
|
983
|
+
| | polars `read_ndjson` | 0.09 | 0.12 | 0.12 | 0.12 |
|
|
984
|
+
| GH Archive, read | fastsimdjson `loads_arrow` | **1.96** | **12.9** | **27.5** | **24.3** |
|
|
985
|
+
| | pyarrow `read_json` | 0.46 | 3.5 | 12.1 | 14.5 |
|
|
986
|
+
| | polars `read_ndjson` | 0.54 | 3.5 | 4.0 | 3.1 |
|
|
987
|
+
| flat, write | fastsimdjson `dumpb_arrow` | **0.92** | **3.7** | **9.6** | **12.1** |
|
|
988
|
+
| | polars | 0.45 | 1.1 | 1.6 | 1.5 |
|
|
989
|
+
| | DuckDB | 0.32 | 2.3 | 4.8 | 3.4 |
|
|
990
|
+
| GH Archive, write | fastsimdjson `dumpb_arrow` | **1.08** | **3.0** | **4.8** | **4.2** |
|
|
991
|
+
| | polars | 0.46 | 0.52 | 0.86 | 1.06 |
|
|
992
|
+
| | DuckDB | 0.32 | 1.3 | 1.1 | 0.86 |
|
|
993
|
+
|
|
994
|
+
`load_arrow_file` on the same files, from the page cache, reads at 0.69 and
|
|
995
|
+
1.5 GB/s on one thread, 9.1 and 13.0 GB/s on 32 threads. The benchmark is
|
|
996
|
+
`THREADS=N python bench_arrow.py flat.ndjson:flat gharchive.ndjson:gh
|
|
997
|
+
flat.ndjson:infer`; `path:infer` times any NDJSON file without a schema. The
|
|
998
|
+
flat file comes from
|
|
999
|
+
[simdjson_arrow's `benchmarks/gendata.py`](https://github.com/simdjson/simdjson_arrow/blob/main/benchmarks/gendata.py)
|
|
1000
|
+
and the events from GH Archive
|
|
1001
|
+
(`curl -sSfL https://data.gharchive.org/2024-03-13-12.json.gz | gunzip > gharchive.ndjson`).
|
|
1002
|
+
The charts come from `python doc/make_charts.py arrow`.
|
|
1003
|
+
|
|
1004
|
+
### JSON to MessagePack and back
|
|
1005
|
+
|
|
1006
|
+
From JSON bytes to MessagePack bytes, and back, against the usual ways in
|
|
1007
|
+
Python, which decode into Python objects, then encode them:
|
|
1008
|
+
`ormsgpack.packb(orjson.loads(s))`, `msgspec.msgpack.encode(msgspec.json.decode(s))`
|
|
1009
|
+
and so on. The 22 files of simdjson-data, geometric means of the speeds, in
|
|
1010
|
+
GB/s of JSON (read when writing MessagePack, written when writing JSON).
|
|
1011
|
+
Python 3.14.10, msgpack 1.2, orjson 3.13, ormsgpack 1.13, msgspec 0.22, Intel
|
|
1012
|
+
Xeon Gold 6548N, one core.
|
|
1013
|
+
|
|
1014
|
+
<picture>
|
|
1015
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_msgpack_encode_dark.png">
|
|
1016
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_msgpack_encode.png" width="80%" alt="JSON to MessagePack in Python, geometric mean over the 22 files of simdjson-data: fastsimdjson json_to_msgpack 1.67 GB/s, orjson with ormsgpack 0.46, msgspec 0.42, fastsimdjson loads with msgpack 0.34, orjson with msgpack 0.32, json with msgpack 0.17">
|
|
1017
|
+
</picture>
|
|
1018
|
+
|
|
1019
|
+
<picture>
|
|
1020
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_msgpack_decode_dark.png">
|
|
1021
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_msgpack_decode.png" width="80%" alt="MessagePack to JSON in Python, geometric mean over the 22 files of simdjson-data: fastsimdjson msgpack_to_json 1.43 GB/s, ormsgpack with orjson 0.35, msgspec 0.32, msgpack with fastsimdjson dumpb 0.26, msgpack with orjson 0.26, msgpack with json 0.10">
|
|
1022
|
+
</picture>
|
|
1023
|
+
|
|
1024
|
+
Streams: the 304,929 events of an hour of GH Archive (886 MB of NDJSON), with
|
|
1025
|
+
`json_to_msgpack(data, ndjson=True, threads=N)` and
|
|
1026
|
+
`msgpack_to_json(packed, stream=True, threads=N)`, against orjson with
|
|
1027
|
+
ormsgpack (each document) and `msgpack.Unpacker` with orjson (each value), on
|
|
1028
|
+
one thread (one thread per core of one socket up to 32 threads, then both
|
|
1029
|
+
sockets):
|
|
1030
|
+
|
|
1031
|
+
<picture>
|
|
1032
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_msgpack_ndjson_dark.png">
|
|
1033
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_msgpack_ndjson.png" width="80%" alt="GH Archive NDJSON to MessagePack: fastsimdjson 22.7 GB/s on 64 threads, 16.0 on 32, 8.7 on 8, 1.31 on one thread, orjson with ormsgpack 0.36 on one thread; MessagePack to NDJSON: fastsimdjson 6.0 GB/s on 8 threads, 1.51 on one thread, msgpack with orjson 0.25">
|
|
1034
|
+
</picture>
|
|
1035
|
+
|
|
1036
|
+
| threads | 1 | 2 | 4 | 8 | 16 | 32 | 64 |
|
|
1037
|
+
|---|---:|---:|---:|---:|---:|---:|---:|
|
|
1038
|
+
| NDJSON to MessagePack | 1.31 | 2.45 | 4.7 | 8.7 | 14.0 | 16.0 | 22.7 |
|
|
1039
|
+
| MessagePack to NDJSON | 1.51 | 2.26 | 4.4 | 6.0 | 6.0 | 6.0 | 5.9 |
|
|
1040
|
+
|
|
1041
|
+
MessagePack to NDJSON stops scaling at about eight threads: one thread must
|
|
1042
|
+
find where the values end (MessagePack has no line ends) before the others
|
|
1043
|
+
can convert them. The benchmark is `python bench_msgpack.py` (the files of
|
|
1044
|
+
`JSONDIR`, by default `simdjson-data/jsonexamples`) and
|
|
1045
|
+
`python bench_msgpack.py --ndjson gharchive.ndjson 1 8 32`; the charts come
|
|
1046
|
+
from `python doc/make_charts.py msgpack`.
|
|
1047
|
+
|
|
1048
|
+
### Streams with `loads_many`
|
|
1049
|
+
|
|
1050
|
+
20 MB of NDJSON (5268 objects and arrays, one per line), made from the same
|
|
1051
|
+
files; best of three runs:
|
|
1052
|
+
|
|
1053
|
+
<picture>
|
|
1054
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_ndjson_dark.png">
|
|
1055
|
+
<img src="https://raw.githubusercontent.com/simdjson/fastpysimdjson/main/doc/perf_ndjson.png" width="80%" alt="Reading NDJSON: fastsimdjson parse_many 1.13 GB/s, loads_many 0.40, msgspec decode_lines 0.36, fastsimdjson loads per line 0.34, orjson per line 0.26, msgspec decode per line 0.26, jiter per line 0.23, jiter without its string cache per line 0.21, json per line 0.12">
|
|
1056
|
+
</picture>
|
|
1057
|
+
|
|
1058
|
+
| method | ms | GB/s |
|
|
1059
|
+
|---|---:|---:|
|
|
1060
|
+
| `json.loads` on each line | 164 | 0.12 |
|
|
1061
|
+
| jiter on each line (no string cache) | 94 | 0.21 |
|
|
1062
|
+
| jiter on each line (string cache) | 84 | 0.23 |
|
|
1063
|
+
| msgspec `decode` on each line | 75 | 0.26 |
|
|
1064
|
+
| orjson on each line | 74 | 0.26 |
|
|
1065
|
+
| fastsimdjson `loads` on each line | 58 | 0.34 |
|
|
1066
|
+
| msgspec `decode_lines` | 55 | 0.36 |
|
|
1067
|
+
| fastsimdjson `loads_many` | 49 | 0.40 |
|
|
1068
|
+
| fastsimdjson `parse_many` (views only) | 17 | 1.13 |
|
|
1069
|
+
|
|
1070
|
+
`loads_many` is 13% faster than msgspec's `decode_lines`. `parse_many` only creates the views; reading values from them adds to
|
|
1071
|
+
its time.
|
|
1072
|
+
|
|
1073
|
+
### Streams on several threads
|
|
1074
|
+
|
|
1075
|
+
300 MB of NDJSON (146,624 records: tweets, GitHub events, citm performances,
|
|
1076
|
+
gsoc and update-center entries), free-threaded Python 3.14.10t, collector
|
|
1077
|
+
disabled (see below), Intel Xeon Gold 6548N (64 cores); GB/s:
|
|
1078
|
+
|
|
1079
|
+
| threads | 1 | 2 | 4 | 8 | 16 | 32 | 64 |
|
|
1080
|
+
|---|---:|---:|---:|---:|---:|---:|---:|
|
|
1081
|
+
| `map_many(len, data, threads=N)` | 0.90 | 1.64 | 2.08 | 3.53 | 5.85 | 8.70 | 16.8 |
|
|
1082
|
+
| `parse_many(data, threads=N)` | 1.59 | 2.78 | 3.75 | 4.79 | 9.01 | 13.5 | **20.6** |
|
|
1083
|
+
| `loads_many(data, threads=N)` | 0.77 | 1.52 | 2.29 | 4.08 | 6.87 | 9.99 | 11.8 |
|
|
1084
|
+
|
|
1085
|
+
`map_many` is 19 times faster on 64 threads than on one, and `parse_many` 13
|
|
1086
|
+
times. `loads_many` builds every document for the calling thread and is 15
|
|
1087
|
+
times faster. On 64 threads, these numbers vary by up to a third from one
|
|
1088
|
+
run to the next. With the collector enabled,
|
|
1089
|
+
Python collects after the call returns: it examines the millions of objects
|
|
1090
|
+
just created, which takes about 0.1 s here, with or without threads.
|
|
1091
|
+
|
|
1092
|
+
## Limitations
|
|
1093
|
+
|
|
1094
|
+
* The simdjson parser and the key and string caches are thread-local.
|
|
1095
|
+
`release()` frees the parser and the cached strings retained by the calling
|
|
1096
|
+
thread and by the worker threads of the parallel streams, and they are
|
|
1097
|
+
freed when a Python thread exits. A parser that grows past 64 MB is freed
|
|
1098
|
+
on its own at the end of that call; its caches stay.
|
|
1099
|
+
* On free-threaded Python, the worker threads of the parallel streams
|
|
1100
|
+
(`threads=N`) are started at the first call that needs them and kept until
|
|
1101
|
+
the interpreter exits: starting threads costs about as much as the parsing.
|
|
1102
|
+
Idle workers wait without using the processor. Each one keeps a parser
|
|
1103
|
+
(about a megabyte) and its caches until `release()`. Free-threaded CPython
|
|
1104
|
+
also keeps the memory of freed objects for reuse, in the thread that
|
|
1105
|
+
allocated it, rather than returning it to the system (the standard `json`
|
|
1106
|
+
module shows the same): after a large threaded call, the process stays
|
|
1107
|
+
larger, and the next calls reuse that memory.
|
|
1108
|
+
* Subinterpreters are not supported: the module keeps per-process state (its
|
|
1109
|
+
types, and per-thread parsers and string caches).
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
fastsimdjson.cpython-315-wasm32-emscripten.so,sha256=5qwW85YlrJaSyoNgta1fqWt2GauBkYSjXxSB8Bdd050,696453
|
|
2
|
+
fastsimdjson-2.0.0.dist-info/METADATA,sha256=3sEXF8R0fW0htyOlJDuBeAHVI1tBOcGfh2FmmfQC1A0,57167
|
|
3
|
+
fastsimdjson-2.0.0.dist-info/WHEEL,sha256=TJ3k3K-PeXUcrQvMlGGRUYx5T4wAHh9QC7lF4fidREE,118
|
|
4
|
+
fastsimdjson-2.0.0.dist-info/top_level.txt,sha256=Jlm9cb3c2dfwwmf2rdX3Y7He8ty1I1eV52HBR4ZC2Mw,13
|
|
5
|
+
fastsimdjson-2.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fastsimdjson
|
|
Binary file
|