pjson-rs 0.1.0__tar.gz
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.
- pjson_rs-0.1.0/.github/workflows/ci.yml +112 -0
- pjson_rs-0.1.0/.gitignore +10 -0
- pjson_rs-0.1.0/Cargo.lock +133 -0
- pjson_rs-0.1.0/Cargo.toml +19 -0
- pjson_rs-0.1.0/LICENSE +21 -0
- pjson_rs-0.1.0/PKG-INFO +90 -0
- pjson_rs-0.1.0/README.md +73 -0
- pjson_rs-0.1.0/benchmarks/bench.py +111 -0
- pjson_rs-0.1.0/pyproject.toml +27 -0
- pjson_rs-0.1.0/python/pjson_rs/__init__.py +42 -0
- pjson_rs-0.1.0/python/pjson_rs/__init__.pyi +53 -0
- pjson_rs-0.1.0/python/pjson_rs/py.typed +0 -0
- pjson_rs-0.1.0/src/lib.rs +260 -0
- pjson_rs-0.1.0/src/parser.rs +667 -0
- pjson_rs-0.1.0/src/simd.rs +189 -0
- pjson_rs-0.1.0/src/stream.rs +560 -0
- pjson_rs-0.1.0/tests/test_pjson_rs.py +245 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
tags: ["v*"]
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
lint-and-test-rust:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: dtolnay/rust-toolchain@stable
|
|
18
|
+
with:
|
|
19
|
+
components: rustfmt, clippy
|
|
20
|
+
- run: cargo fmt --check
|
|
21
|
+
- run: cargo clippy --all-targets -- -D warnings
|
|
22
|
+
- run: cargo test --lib
|
|
23
|
+
|
|
24
|
+
build-wheels:
|
|
25
|
+
strategy:
|
|
26
|
+
fail-fast: false
|
|
27
|
+
matrix:
|
|
28
|
+
include:
|
|
29
|
+
- os: ubuntu-latest
|
|
30
|
+
target: x86_64
|
|
31
|
+
args: --release --compatibility manylinux2014
|
|
32
|
+
run_tests: true
|
|
33
|
+
- os: ubuntu-24.04-arm
|
|
34
|
+
target: aarch64
|
|
35
|
+
args: --release --compatibility manylinux2014
|
|
36
|
+
run_tests: true
|
|
37
|
+
- os: macos-15
|
|
38
|
+
target: aarch64
|
|
39
|
+
args: --release
|
|
40
|
+
run_tests: true
|
|
41
|
+
- os: macos-15
|
|
42
|
+
target: x86_64
|
|
43
|
+
args: --release --target x86_64-apple-darwin
|
|
44
|
+
run_tests: false
|
|
45
|
+
- os: windows-latest
|
|
46
|
+
target: x64
|
|
47
|
+
args: --release
|
|
48
|
+
run_tests: true
|
|
49
|
+
runs-on: ${{ matrix.os }}
|
|
50
|
+
steps:
|
|
51
|
+
- uses: actions/checkout@v4
|
|
52
|
+
- uses: actions/setup-python@v5
|
|
53
|
+
with:
|
|
54
|
+
python-version: "3.12"
|
|
55
|
+
- uses: PyO3/maturin-action@v1
|
|
56
|
+
with:
|
|
57
|
+
command: build
|
|
58
|
+
args: ${{ matrix.args }} -o dist
|
|
59
|
+
manylinux: auto
|
|
60
|
+
- name: Check wheel metadata
|
|
61
|
+
shell: bash
|
|
62
|
+
run: |
|
|
63
|
+
python -m pip install --upgrade pip twine
|
|
64
|
+
twine check dist/*.whl
|
|
65
|
+
- name: Install and test abi3 wheel
|
|
66
|
+
if: matrix.run_tests
|
|
67
|
+
shell: bash
|
|
68
|
+
run: |
|
|
69
|
+
python -m pip install pytest hypothesis partial-json-parser mypy ruff abi3audit
|
|
70
|
+
python -m pip install dist/*.whl
|
|
71
|
+
pytest -v
|
|
72
|
+
- name: Static typing, stubtest, and abi3audit (Linux)
|
|
73
|
+
if: runner.os == 'Linux'
|
|
74
|
+
run: |
|
|
75
|
+
ruff format --check python tests
|
|
76
|
+
ruff check python tests
|
|
77
|
+
mypy --strict python/pjson_rs
|
|
78
|
+
stubtest --ignore-disjoint-base pjson_rs
|
|
79
|
+
abi3audit --strict dist/*.whl
|
|
80
|
+
- uses: actions/upload-artifact@v4
|
|
81
|
+
with:
|
|
82
|
+
name: wheels-${{ matrix.os }}-${{ matrix.target }}
|
|
83
|
+
path: dist/*.whl
|
|
84
|
+
|
|
85
|
+
build-sdist:
|
|
86
|
+
runs-on: ubuntu-latest
|
|
87
|
+
steps:
|
|
88
|
+
- uses: actions/checkout@v4
|
|
89
|
+
- uses: PyO3/maturin-action@v1
|
|
90
|
+
with:
|
|
91
|
+
command: sdist
|
|
92
|
+
args: -o dist
|
|
93
|
+
- uses: actions/upload-artifact@v4
|
|
94
|
+
with:
|
|
95
|
+
name: sdist
|
|
96
|
+
path: dist/*.tar.gz
|
|
97
|
+
|
|
98
|
+
publish:
|
|
99
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
100
|
+
needs: [lint-and-test-rust, build-wheels, build-sdist]
|
|
101
|
+
runs-on: ubuntu-latest
|
|
102
|
+
environment: pypi
|
|
103
|
+
permissions:
|
|
104
|
+
id-token: write
|
|
105
|
+
steps:
|
|
106
|
+
- uses: actions/download-artifact@v4
|
|
107
|
+
with:
|
|
108
|
+
path: dist
|
|
109
|
+
merge-multiple: true
|
|
110
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
111
|
+
with:
|
|
112
|
+
packages-dir: dist/
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "heck"
|
|
7
|
+
version = "0.5.0"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "libc"
|
|
13
|
+
version = "0.2.191"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
16
|
+
|
|
17
|
+
[[package]]
|
|
18
|
+
name = "once_cell"
|
|
19
|
+
version = "1.21.4"
|
|
20
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
21
|
+
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
22
|
+
|
|
23
|
+
[[package]]
|
|
24
|
+
name = "pjson-rs"
|
|
25
|
+
version = "0.1.0"
|
|
26
|
+
dependencies = [
|
|
27
|
+
"pyo3",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[[package]]
|
|
31
|
+
name = "portable-atomic"
|
|
32
|
+
version = "1.15.0"
|
|
33
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
34
|
+
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
|
|
35
|
+
|
|
36
|
+
[[package]]
|
|
37
|
+
name = "proc-macro2"
|
|
38
|
+
version = "1.0.107"
|
|
39
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
40
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
41
|
+
dependencies = [
|
|
42
|
+
"unicode-ident",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[[package]]
|
|
46
|
+
name = "pyo3"
|
|
47
|
+
version = "0.28.3"
|
|
48
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
49
|
+
checksum = "91fd8e38a3b50ed1167fb981cd6fd60147e091784c427b8f7183a7ee32c31c12"
|
|
50
|
+
dependencies = [
|
|
51
|
+
"libc",
|
|
52
|
+
"once_cell",
|
|
53
|
+
"portable-atomic",
|
|
54
|
+
"pyo3-build-config",
|
|
55
|
+
"pyo3-ffi",
|
|
56
|
+
"pyo3-macros",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[[package]]
|
|
60
|
+
name = "pyo3-build-config"
|
|
61
|
+
version = "0.28.3"
|
|
62
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
63
|
+
checksum = "e368e7ddfdeb98c9bca7f8383be1648fd84ab466bf2bc015e94008db6d35611e"
|
|
64
|
+
dependencies = [
|
|
65
|
+
"target-lexicon",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[[package]]
|
|
69
|
+
name = "pyo3-ffi"
|
|
70
|
+
version = "0.28.3"
|
|
71
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
72
|
+
checksum = "7f29e10af80b1f7ccaf7f69eace800a03ecd13e883acfacc1e5d0988605f651e"
|
|
73
|
+
dependencies = [
|
|
74
|
+
"libc",
|
|
75
|
+
"pyo3-build-config",
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
[[package]]
|
|
79
|
+
name = "pyo3-macros"
|
|
80
|
+
version = "0.28.3"
|
|
81
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
82
|
+
checksum = "df6e520eff47c45997d2fc7dd8214b25dd1310918bbb2642156ef66a67f29813"
|
|
83
|
+
dependencies = [
|
|
84
|
+
"proc-macro2",
|
|
85
|
+
"pyo3-macros-backend",
|
|
86
|
+
"quote",
|
|
87
|
+
"syn",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
[[package]]
|
|
91
|
+
name = "pyo3-macros-backend"
|
|
92
|
+
version = "0.28.3"
|
|
93
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
94
|
+
checksum = "c4cdc218d835738f81c2338f822078af45b4afdf8b2e33cbb5916f108b813acb"
|
|
95
|
+
dependencies = [
|
|
96
|
+
"heck",
|
|
97
|
+
"proc-macro2",
|
|
98
|
+
"pyo3-build-config",
|
|
99
|
+
"quote",
|
|
100
|
+
"syn",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
[[package]]
|
|
104
|
+
name = "quote"
|
|
105
|
+
version = "1.0.47"
|
|
106
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
107
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
108
|
+
dependencies = [
|
|
109
|
+
"proc-macro2",
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
[[package]]
|
|
113
|
+
name = "syn"
|
|
114
|
+
version = "2.0.119"
|
|
115
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
116
|
+
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
|
|
117
|
+
dependencies = [
|
|
118
|
+
"proc-macro2",
|
|
119
|
+
"quote",
|
|
120
|
+
"unicode-ident",
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
[[package]]
|
|
124
|
+
name = "target-lexicon"
|
|
125
|
+
version = "0.13.6"
|
|
126
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
127
|
+
checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da"
|
|
128
|
+
|
|
129
|
+
[[package]]
|
|
130
|
+
name = "unicode-ident"
|
|
131
|
+
version = "1.0.27"
|
|
132
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
133
|
+
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "pjson-rs"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
description = "Zero-dependency SIMD-accelerated streaming partial JSON parser for Python (drop-in partial-json-parser replacement + O(N) StreamParser)"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
name = "_pjson_rs"
|
|
11
|
+
crate-type = ["cdylib", "rlib"]
|
|
12
|
+
|
|
13
|
+
[dependencies]
|
|
14
|
+
pyo3 = { version = "0.28", features = ["abi3-py39"] }
|
|
15
|
+
|
|
16
|
+
[profile.release]
|
|
17
|
+
lto = "thin"
|
|
18
|
+
codegen-units = 1
|
|
19
|
+
opt-level = 3
|
pjson_rs-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gateway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pjson_rs-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pjson-rs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Summary: SIMD-accelerated streaming partial JSON parser for Python — drop-in partial-json-parser replacement + O(N) StreamParser for vLLM, SGLang, and LLM agents
|
|
10
|
+
License: MIT
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
13
|
+
Project-URL: Homepage, https://github.com/gatewaytofredom/pjson-rs
|
|
14
|
+
Project-URL: Issues, https://github.com/gatewaytofredom/pjson-rs/issues
|
|
15
|
+
Project-URL: Repository, https://github.com/gatewaytofredom/pjson-rs
|
|
16
|
+
|
|
17
|
+
# pjson-rs
|
|
18
|
+
|
|
19
|
+
SIMD-accelerated (AVX2 / SSE2 / ARM64 NEON) streaming partial JSON parser for Python — a zero-dependency Rust/PyO3 drop-in replacement for `partial-json-parser` plus a stateful $O(N)$ `StreamParser`, built for LLM tool-call streaming (`vLLM`, `SGLang`, `LangChain`, and AI agents).
|
|
20
|
+
|
|
21
|
+
- **14×–16× faster** single-shot `loads` / `raw_decode` vs `partial-json-parser`
|
|
22
|
+
- **100×–330× faster** stateful $O(N)$ streaming (`StreamParser.complete()`) on 1 KB–8 KB chunked streams
|
|
23
|
+
- **Drop-in `Allow` bitflags** (1:1 with `partial_json_parser.core.options.Allow`)
|
|
24
|
+
- **Single-pass `raw_decode(s, allow) -> (obj, end_char_idx)`** — handles trailing data & Unicode char offsets without catching `JSONDecodeError`
|
|
25
|
+
- **Pythonic `cp39-abi3` wheel** (~270 KB, works on Python 3.9–3.14+, free-threaded ready with `gil_used = false`)
|
|
26
|
+
- **Accepts `str | bytes | bytearray`** (including multi-byte UTF-8 split across `bytes` chunks in `StreamParser.feed`)
|
|
27
|
+
- **Uniform `json.JSONDecodeError`** — never leaks `AssertionError`, `IndexError`, or lone UTF-16 surrogates
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install pjson-rs
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
### Drop-in `partial-json-parser` & vLLM/SGLang `raw_decode`
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import pjson_rs
|
|
41
|
+
from pjson_rs import Allow
|
|
42
|
+
|
|
43
|
+
# Drop-in replacement for partial_json_parser.loads
|
|
44
|
+
obj = pjson_rs.loads('{"tool": "write_file", "path": "src/main.rs", "content": "fn main', Allow.ALL)
|
|
45
|
+
# {'tool': 'write_file', 'path': 'src/main.rs', 'content': 'fn main'}
|
|
46
|
+
|
|
47
|
+
# Single-pass raw_decode with trailing text & exact Python char offset
|
|
48
|
+
text = '{"emoji": "🚀", "n": 42} trailing text'
|
|
49
|
+
obj, end_idx = pjson_rs.raw_decode(text, Allow.ALL)
|
|
50
|
+
assert text[end_idx:] == " trailing text"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Stateful $O(N)$ `StreamParser`
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import pjson_rs
|
|
57
|
+
|
|
58
|
+
sp = pjson_rs.StreamParser()
|
|
59
|
+
for chunk in [b'{"cmd": "echo ', b'\xf0\x9f', b'\x9a\x80", "ok": tr', b'ue}']:
|
|
60
|
+
sp.feed(chunk)
|
|
61
|
+
healed_json = sp.complete() # O(1) close, always valid UTF-8 JSON
|
|
62
|
+
|
|
63
|
+
assert sp.done()
|
|
64
|
+
assert sp.loads() == {"cmd": "echo 🚀", "ok": True}
|
|
65
|
+
sp.reset() # reuse internal buffers across tool calls
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Benchmarks
|
|
69
|
+
|
|
70
|
+
8-byte chunk streaming and 75%-truncated single-shot loads (`benchmarks/bench.py`):
|
|
71
|
+
|
|
72
|
+
| Workload | `partial-json-parser` | `pjson_rs` (Intel i5-12400 AVX2) | `pjson_rs` (Apple M4 NEON) |
|
|
73
|
+
|---|---:|---:|---:|
|
|
74
|
+
| Single-shot `1 KB` | `30.0 µs` (x86) / `26.5 µs` (M4) | **`2.1 µs`** (**14.4×**) | **`1.7 µs`** (**15.3×**) |
|
|
75
|
+
| Single-shot `8 KB` | `200.8 µs` (x86) / `129.9 µs` (M4) | **`14.5 µs`** (**13.9×**) | **`10.9 µs`** (**11.9×**) |
|
|
76
|
+
| Single-shot `32 KB` | `817.2 µs` (x86) / `605.1 µs` (M4) | **`49.5 µs`** (**16.5×**) | **`43.3 µs`** (**14.0×**) |
|
|
77
|
+
| Streaming `1 KB` (139 × 8B) | `3.56 ms` (x86) / `2.16 ms` (M4) | `0.25 ms` (`raw_decode`, **14.2×**) · **`0.02 ms`** (`StreamParser`, **162×**) | `0.18 ms` (**12.0×**) · **`0.02 ms`** (**108×**) |
|
|
78
|
+
| Streaming `8 KB` (1,041 × 8B) | `130.5 ms` (x86) / `91.8 ms` (M4) | `9.46 ms` (`raw_decode`, **13.8×**) · **`0.39 ms`** (`StreamParser`, **333×**) | `8.25 ms` (**11.1×**) · **`0.31 ms`** (**298×**) |
|
|
79
|
+
|
|
80
|
+
## ABI & Verification
|
|
81
|
+
|
|
82
|
+
- `cp39-abi3` Limited API verified with `abi3audit --strict`
|
|
83
|
+
- PEP 561 typed (`py.typed` + `__init__.pyi`) verified with `mypy --strict` and `stubtest`
|
|
84
|
+
- Rust SIMD alignment tests (`cargo test --lib`) across AVX2, SSE2, and ARM64 NEON
|
|
85
|
+
- Differential property-based testing (`hypothesis`), split-UTF-8, 8-thread concurrency, and `tracemalloc` leak tests
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT
|
|
90
|
+
|
pjson_rs-0.1.0/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# pjson-rs
|
|
2
|
+
|
|
3
|
+
SIMD-accelerated (AVX2 / SSE2 / ARM64 NEON) streaming partial JSON parser for Python — a zero-dependency Rust/PyO3 drop-in replacement for `partial-json-parser` plus a stateful $O(N)$ `StreamParser`, built for LLM tool-call streaming (`vLLM`, `SGLang`, `LangChain`, and AI agents).
|
|
4
|
+
|
|
5
|
+
- **14×–16× faster** single-shot `loads` / `raw_decode` vs `partial-json-parser`
|
|
6
|
+
- **100×–330× faster** stateful $O(N)$ streaming (`StreamParser.complete()`) on 1 KB–8 KB chunked streams
|
|
7
|
+
- **Drop-in `Allow` bitflags** (1:1 with `partial_json_parser.core.options.Allow`)
|
|
8
|
+
- **Single-pass `raw_decode(s, allow) -> (obj, end_char_idx)`** — handles trailing data & Unicode char offsets without catching `JSONDecodeError`
|
|
9
|
+
- **Pythonic `cp39-abi3` wheel** (~270 KB, works on Python 3.9–3.14+, free-threaded ready with `gil_used = false`)
|
|
10
|
+
- **Accepts `str | bytes | bytearray`** (including multi-byte UTF-8 split across `bytes` chunks in `StreamParser.feed`)
|
|
11
|
+
- **Uniform `json.JSONDecodeError`** — never leaks `AssertionError`, `IndexError`, or lone UTF-16 surrogates
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install pjson-rs
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### Drop-in `partial-json-parser` & vLLM/SGLang `raw_decode`
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
import pjson_rs
|
|
25
|
+
from pjson_rs import Allow
|
|
26
|
+
|
|
27
|
+
# Drop-in replacement for partial_json_parser.loads
|
|
28
|
+
obj = pjson_rs.loads('{"tool": "write_file", "path": "src/main.rs", "content": "fn main', Allow.ALL)
|
|
29
|
+
# {'tool': 'write_file', 'path': 'src/main.rs', 'content': 'fn main'}
|
|
30
|
+
|
|
31
|
+
# Single-pass raw_decode with trailing text & exact Python char offset
|
|
32
|
+
text = '{"emoji": "🚀", "n": 42} trailing text'
|
|
33
|
+
obj, end_idx = pjson_rs.raw_decode(text, Allow.ALL)
|
|
34
|
+
assert text[end_idx:] == " trailing text"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Stateful $O(N)$ `StreamParser`
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import pjson_rs
|
|
41
|
+
|
|
42
|
+
sp = pjson_rs.StreamParser()
|
|
43
|
+
for chunk in [b'{"cmd": "echo ', b'\xf0\x9f', b'\x9a\x80", "ok": tr', b'ue}']:
|
|
44
|
+
sp.feed(chunk)
|
|
45
|
+
healed_json = sp.complete() # O(1) close, always valid UTF-8 JSON
|
|
46
|
+
|
|
47
|
+
assert sp.done()
|
|
48
|
+
assert sp.loads() == {"cmd": "echo 🚀", "ok": True}
|
|
49
|
+
sp.reset() # reuse internal buffers across tool calls
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Benchmarks
|
|
53
|
+
|
|
54
|
+
8-byte chunk streaming and 75%-truncated single-shot loads (`benchmarks/bench.py`):
|
|
55
|
+
|
|
56
|
+
| Workload | `partial-json-parser` | `pjson_rs` (Intel i5-12400 AVX2) | `pjson_rs` (Apple M4 NEON) |
|
|
57
|
+
|---|---:|---:|---:|
|
|
58
|
+
| Single-shot `1 KB` | `30.0 µs` (x86) / `26.5 µs` (M4) | **`2.1 µs`** (**14.4×**) | **`1.7 µs`** (**15.3×**) |
|
|
59
|
+
| Single-shot `8 KB` | `200.8 µs` (x86) / `129.9 µs` (M4) | **`14.5 µs`** (**13.9×**) | **`10.9 µs`** (**11.9×**) |
|
|
60
|
+
| Single-shot `32 KB` | `817.2 µs` (x86) / `605.1 µs` (M4) | **`49.5 µs`** (**16.5×**) | **`43.3 µs`** (**14.0×**) |
|
|
61
|
+
| Streaming `1 KB` (139 × 8B) | `3.56 ms` (x86) / `2.16 ms` (M4) | `0.25 ms` (`raw_decode`, **14.2×**) · **`0.02 ms`** (`StreamParser`, **162×**) | `0.18 ms` (**12.0×**) · **`0.02 ms`** (**108×**) |
|
|
62
|
+
| Streaming `8 KB` (1,041 × 8B) | `130.5 ms` (x86) / `91.8 ms` (M4) | `9.46 ms` (`raw_decode`, **13.8×**) · **`0.39 ms`** (`StreamParser`, **333×**) | `8.25 ms` (**11.1×**) · **`0.31 ms`** (**298×**) |
|
|
63
|
+
|
|
64
|
+
## ABI & Verification
|
|
65
|
+
|
|
66
|
+
- `cp39-abi3` Limited API verified with `abi3audit --strict`
|
|
67
|
+
- PEP 561 typed (`py.typed` + `__init__.pyi`) verified with `mypy --strict` and `stubtest`
|
|
68
|
+
- Rust SIMD alignment tests (`cargo test --lib`) across AVX2, SSE2, and ARM64 NEON
|
|
69
|
+
- Differential property-based testing (`hypothesis`), split-UTF-8, 8-thread concurrency, and `tracemalloc` leak tests
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
import jiter
|
|
5
|
+
import partial_json_parser
|
|
6
|
+
from partial_json_parser.core.options import Allow as RefAllow
|
|
7
|
+
|
|
8
|
+
import pjson_rs
|
|
9
|
+
from pjson_rs import Allow
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def build_payload(target_bytes: int) -> str:
|
|
13
|
+
items = []
|
|
14
|
+
i = 0
|
|
15
|
+
while len(json.dumps({"tool": "batch_edit", "items": items})) < target_bytes:
|
|
16
|
+
items.append(
|
|
17
|
+
{
|
|
18
|
+
"id": i,
|
|
19
|
+
"path": f"src/module_{i}/handler.py",
|
|
20
|
+
"active": True,
|
|
21
|
+
"score": i + 0.5,
|
|
22
|
+
"content": "def process(x):\n return x * 2 # realistic tool call text\n",
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
i += 1
|
|
26
|
+
return json.dumps({"tool": "batch_edit", "items": items})
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def bench(fn, iters: int) -> float:
|
|
30
|
+
fn() # warmup
|
|
31
|
+
t0 = time.perf_counter()
|
|
32
|
+
for _ in range(iters):
|
|
33
|
+
fn()
|
|
34
|
+
return (time.perf_counter() - t0) / iters
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def main():
|
|
38
|
+
print("=== Single-shot partial JSON loads (truncated at 75%) ===")
|
|
39
|
+
for label, sz, iters in [
|
|
40
|
+
("1KB", 1024, 2000),
|
|
41
|
+
("8KB", 8192, 500),
|
|
42
|
+
("32KB", 32768, 150),
|
|
43
|
+
]:
|
|
44
|
+
full = build_payload(sz)
|
|
45
|
+
part = full[: len(full) * 3 // 4]
|
|
46
|
+
part_bytes = part.encode()
|
|
47
|
+
|
|
48
|
+
t_ref = bench(lambda: partial_json_parser.loads(part, RefAllow.ALL), iters)
|
|
49
|
+
t_jiter = bench(
|
|
50
|
+
lambda: jiter.from_json(part_bytes, partial_mode="trailing-strings"), iters
|
|
51
|
+
)
|
|
52
|
+
t_pjson = bench(lambda: pjson_rs.loads(part, Allow.ALL), iters)
|
|
53
|
+
t_raw = bench(lambda: pjson_rs.raw_decode(part, Allow.ALL), iters)
|
|
54
|
+
|
|
55
|
+
print(
|
|
56
|
+
f"{label:4s} | partial-json-parser: {t_ref * 1e6:8.1f} µs | "
|
|
57
|
+
f"jiter: {t_jiter * 1e6:7.1f} µs | "
|
|
58
|
+
f"pjson_rs.loads: {t_pjson * 1e6:7.1f} µs ({t_ref / t_pjson:5.1f}x vs ref) | "
|
|
59
|
+
f"raw_decode: {t_raw * 1e6:7.1f} µs"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
print("\n=== Streaming 8-byte chunks (vLLM / SGLang tool-call loop) ===")
|
|
63
|
+
for label, sz, iters in [("1KB", 1024, 30), ("8KB", 8192, 5)]:
|
|
64
|
+
full = build_payload(sz)
|
|
65
|
+
chunks = [full[i : i + 8] for i in range(0, len(full), 8)]
|
|
66
|
+
|
|
67
|
+
def run_ref():
|
|
68
|
+
acc = ""
|
|
69
|
+
for ch in chunks:
|
|
70
|
+
acc += ch
|
|
71
|
+
try:
|
|
72
|
+
partial_json_parser.loads(acc, RefAllow.ALL)
|
|
73
|
+
except Exception:
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
def run_pjson_loads():
|
|
77
|
+
acc = ""
|
|
78
|
+
for ch in chunks:
|
|
79
|
+
acc += ch
|
|
80
|
+
try:
|
|
81
|
+
pjson_rs.raw_decode(acc, Allow.ALL)
|
|
82
|
+
except Exception:
|
|
83
|
+
pass
|
|
84
|
+
|
|
85
|
+
def run_pjson_stream_complete():
|
|
86
|
+
sp = pjson_rs.StreamParser()
|
|
87
|
+
for ch in chunks:
|
|
88
|
+
sp.feed(ch)
|
|
89
|
+
sp.complete()
|
|
90
|
+
|
|
91
|
+
def run_pjson_stream_loads():
|
|
92
|
+
sp = pjson_rs.StreamParser()
|
|
93
|
+
for ch in chunks:
|
|
94
|
+
sp.feed(ch)
|
|
95
|
+
sp.loads()
|
|
96
|
+
|
|
97
|
+
t_ref = bench(run_ref, iters)
|
|
98
|
+
t_raw = bench(run_pjson_loads, iters)
|
|
99
|
+
t_sc = bench(run_pjson_stream_complete, iters)
|
|
100
|
+
t_sl = bench(run_pjson_stream_loads, iters)
|
|
101
|
+
print(
|
|
102
|
+
f"{label:4s} ({len(chunks)} chunks) | "
|
|
103
|
+
f"partial-json-parser: {t_ref * 1e3:7.2f} ms | "
|
|
104
|
+
f"pjson_rs.raw_decode: {t_raw * 1e3:6.2f} ms ({t_ref / t_raw:5.1f}x) | "
|
|
105
|
+
f"StreamParser.complete: {t_sc * 1e3:5.2f} ms ({t_ref / t_sc:6.1f}x) | "
|
|
106
|
+
f"StreamParser.loads: {t_sl * 1e3:6.2f} ms ({t_ref / t_sl:5.1f}x)"
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
if __name__ == "__main__":
|
|
111
|
+
main()
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.7,<2.0"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pjson-rs"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "SIMD-accelerated streaming partial JSON parser for Python — drop-in partial-json-parser replacement + O(N) StreamParser for vLLM, SGLang, and LLM agents"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Rust",
|
|
14
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/gatewaytofredom/pjson-rs"
|
|
21
|
+
Repository = "https://github.com/gatewaytofredom/pjson-rs"
|
|
22
|
+
Issues = "https://github.com/gatewaytofredom/pjson-rs/issues"
|
|
23
|
+
|
|
24
|
+
[tool.maturin]
|
|
25
|
+
python-source = "python"
|
|
26
|
+
module-name = "pjson_rs._pjson_rs"
|
|
27
|
+
features = ["pyo3/extension-module"]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""SIMD-accelerated streaming partial JSON parser for Python."""
|
|
2
|
+
|
|
3
|
+
from enum import IntFlag
|
|
4
|
+
|
|
5
|
+
from ._pjson_rs import (
|
|
6
|
+
StreamParser,
|
|
7
|
+
complete,
|
|
8
|
+
extract_intermediate_diff,
|
|
9
|
+
find_common_prefix,
|
|
10
|
+
loads,
|
|
11
|
+
raw_decode,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Allow(IntFlag):
|
|
16
|
+
"""Bitflags controlling which truncated JSON types are allowed (1:1 with partial-json-parser)."""
|
|
17
|
+
|
|
18
|
+
STR = 1
|
|
19
|
+
NUM = 2
|
|
20
|
+
ARR = 4
|
|
21
|
+
OBJ = 8
|
|
22
|
+
NULL = 16
|
|
23
|
+
BOOL = 32
|
|
24
|
+
NAN = 64
|
|
25
|
+
INFINITY = 128
|
|
26
|
+
_INFINITY = 256
|
|
27
|
+
INF = INFINITY | _INFINITY
|
|
28
|
+
SPECIAL = NAN | INF
|
|
29
|
+
ATOM = STR | NUM | NULL | BOOL
|
|
30
|
+
COLLECTION = ARR | OBJ
|
|
31
|
+
ALL = ATOM | COLLECTION | SPECIAL
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"Allow",
|
|
36
|
+
"StreamParser",
|
|
37
|
+
"complete",
|
|
38
|
+
"extract_intermediate_diff",
|
|
39
|
+
"find_common_prefix",
|
|
40
|
+
"loads",
|
|
41
|
+
"raw_decode",
|
|
42
|
+
]
|