simdjson/simdjson
GitHub: simdjson/simdjson
一个利用 SIMD 向量指令实现每秒千兆字节级 JSON 解析速度的高性能 C++ 库,兼顾完整验证与零性能损耗。
Stars: 23571 | Forks: 1232
[][license] [][licensemit]
[](https://simdjson.github.io/simdjson/)
# simdjson : 每秒解析千兆字节的 JSON
JSON 在互联网上无处不在。服务器花费*大量*时间来解析它。我们需要一种全新的
方法。simdjson 库利用通用的 SIMD 指令和微并行算法,
解析 JSON 的速度比 RapidJSON 快 4 倍,比 JSON for Modern C++ 快 25 倍。
* **快速:** 比常用的生产级 JSON 解析器快 4 倍以上。
* **破纪录的特性:** 以 6 GB/s 的速度压缩(Minify)JSON,以 13 GB/s 的速度验证 UTF-8,以 3.5 GB/s 的速度处理 NDJSON。
* **易用:** 一流的、易于使用且文档齐全的 API。
* **严格:** 完整的 JSON 和 UTF-8 验证,无损解析。性能不打折。
* **自动:** 在运行时选择针对 CPU 优化的解析器。无需配置。
* **可靠:** 从内存分配到错误处理,simdjson 的设计避免了意外。
* **经过同行评审:** 我们的研究发表在 VLDB Journal, Software: Practice and Experience 等期刊上。
本库是 [Awesome Modern C++](https://awesomecpp.com) 列表的一部分。
## 目录
* [实际应用](#real-world-usage)
* [快速开始](#quick-start)
* [文档](#documentation)
* [Godbolt](#godbolt)
* [性能结果](#performance-results)
* [软件包](#packages)
* [simdjson 的绑定与移植](#bindings-and-ports-of-simdjson)
* [关于 simdjson](#about-simdjson)
* [资助](#funding)
* [为 simdjson 做贡献](#contributing-to-simdjson)
* [许可证](#license)
## 实际应用
- [Node.js](https://nodejs.org/)
- [ClickHouse](https://github.com/ClickHouse/ClickHouse)
- [Meta Velox](https://velox-lib.io)
- [Google Pax](https://github.com/google/paxml)
- [milvus](https://github.com/milvus-io/milvus)
- [QuestDB](https://questdb.io/blog/questdb-release-8-0-3/)
- [Clang Build Analyzer](https://github.com/aras-p/ClangBuildAnalyzer)
- [Shopify HeapProfiler](https://github.com/Shopify/heap-profiler)
- [StarRocks](https://github.com/StarRocks/starrocks)
- [Microsoft FishStore](https://github.com/microsoft/FishStore)
- [Intel PCM](https://github.com/intel/pcm)
- [WatermelonDB](https://github.com/Nozbe/WatermelonDB)
- [Apache Doris](https://github.com/apache/doris)
- [Dgraph](https://github.com/dgraph-io/dgraph)
- [UJRPC](https://github.com/unum-cloud/ujrpc)
- [fastgltf](https://github.com/spnda/fastgltf)
- [vast](https://github.com/tenzir/vast)
- [ada-url](https://github.com/ada-url/ada)
- [fastgron](https://github.com/adamritter/fastgron)
- [WasmEdge](https://wasmedge.org)
- [RonDB](https://github.com/logicalclocks/rondb)
- [GreptimeDB](https://github.com/GreptimeTeam/greptimedb)
- [mamba](https://github.com/mamba-org/mamba)
- [Ladybird Browser](https://ladybird.org)
- [SereneDB](https://github.com/serenedb/serenedb)
如果您计划在产品中使用 simdjson,请基于我们的某个发布版本进行开发。
## 快速开始
simdjson 库只需一个 .h 和一个 .cpp 文件即可轻松使用。
0. 前置条件:`g++` (版本 7 或更高) 或 `clang++` (版本 6 或更高),以及带有命令行 shell 的 64 位系统 (例如 Linux, macOS, freeBSD)。我们也支持 Visual Studio 和 Xcode 等编程环境,但步骤有所不同。clang++ 用户可能需要指定 C++ 版本 (例如 `c++ -std=c++17`),因为 clang++ 默认倾向于 C++98。
1. 将 [simdjson.h](singleheader/simdjson.h) 和 [simdjson.cpp](singleheader/simdjson.cpp) 拉取到一个目录中,同时下载示例文件 [twitter.json](jsonexamples/twitter.json)。您可以使用 `wget` 工具下载它们:
wget https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.h https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.cpp https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json
2. 创建 `quickstart.cpp`:
```
#include
#include "simdjson.h"
using namespace simdjson;
int main(void) {
ondemand::parser parser;
padded_string json = padded_string::load("twitter.json");
ondemand::document tweets = parser.iterate(json);
std::cout << uint64_t(tweets["search_metadata"]["count"]) << " results." << std::endl;
}
```
3. `c++ -o quickstart quickstart.cpp simdjson.cpp`
4. `./quickstart`
```
100 results.
```
## 文档
使用文档可用:
* [基础](doc/basics.md) 是关于如何使用 simdjson 及其 API 的概述。
* [构建器](doc/builder.md) 是关于如何使用 simdjson 高效编写 JSON 字符串的概述。
* [性能](doc/performance.md) 展示了一些更高级的场景以及如何针对它们进行调优。
* [实现选择](doc/implementation-selection.md) 描述了运行时 CPU 检测以及您如何使用它。
* [API](https://simdjson.github.io/simdjson/) 包含自动生成的 API 文档。
* [编译时解析](doc/compile_time.md) 介绍了我们的编译时解析功能 (仅限 C++26)。
## Godbolt
有些用户可能希望浏览代码以及编译后的汇编。您需要查看以下示例列表:
* [C++26 反射示例](https://godbolt.org/z/K3Px64TqK)
* [通过异常处理错误的 simdjson 示例](https://godbolt.org/z/7G5qE4sr9)
* [不使用异常处理错误的 simdjson 示例](https://godbolt.org/z/e9dWb9E4v)
## 性能结果
simdjson 库使用的指令比最先进的解析器 [RapidJSON](https://rapidjson.org) 少四分之三。据我们所知,simdjson 是第一个在商用处理器上达到[每秒千兆字节](https://en.wikipedia.org/wiki/Gigabyte) (GB/s) 速度的全验证 JSON 解析器。它可以在单核上每秒解析数百万个 JSON 文档。
下图展示了在使用 GNU GCC 10 编译器 (带 -O3 标志) 的 Intel Skylake 处理器 (3.4 GHz) 上解析各种文件的解析速度 (GB/s)。
我们与在加载和处理数据的基准测试中表现最好、最快的 C++ 库进行了比较。
simdjson 库提供完整的 Unicode ([UTF-8](https://en.wikipedia.org/wiki/UTF-8)) 验证和精确的数字解析。
无论是处理小文件 (例如 300 字节) 还是大文件 (例如 3MB),simdjson 库都能提供高速度。下图展示了在 3.4 GHz Skylake 处理器 (GNU GCC 9, -O3) 上,针对[通过脚本生成的各种大小的合成文件](https://github.com/simdjson/simdjson_experiments_vldb2019/blob/master/experiments/growing/gen.py)的解析速度。
[我们所有的实验均可重现](https://github.com/simdjson/simdjson_experiments_vldb2019)。
对于 NDJSON 文件,借助[我们的多线程解析函数](https://github.com/simdjson/simdjson/blob/master/doc/parse_many.md),速度可以超过 3 GB/s。
## 软件包
[](https://repology.org/project/simdjson/versions)
## simdjson 的绑定与移植
我们区分“绑定” (仅封装 C++ 代码) 和移植到另一种编程语言 (重新实现所有内容)。
- [ZippyJSON](https://github.com/michaeleisel/zippyjson): simdjson 项目的 Swift 绑定。
- [libpy_simdjson](https://github.com/gerrymanoim/libpy_simdjson/): 使用 [libpy](https://github.com/quantopian/libpy) 的 simdjson 高速 Python 绑定。
- [pysimdjson](https://github.com/TkTech/pysimdjson): simdjson 项目的 Python 绑定。
- [cysimdjson](https://github.com/TeskaLabs/cysimdjson): simdjson 项目的高速 Python 绑定。
- [simdjson-rs](https://github.com/simd-lite): Rust 移植。
- [simdjson-rust](https://github.com/SunDoge/simdjson-rust): Rust 封装 (绑定)。
- [SimdJsonSharp](https://github.com/EgorBo/SimdJsonSharp): .NET Core 的 C# 版本 (绑定和完整移植)。
- [simdjson_nodejs](https://github.com/luizperes/simdjson_nodejs): simdjson 项目的 Node.js 绑定。
- [simdjson_php](https://github.com/crazyxman/simdjson_php): simdjson 项目的 PHP 绑定。
- [simdjson_ruby](https://github.com/saka1/simdjson_ruby): simdjson 项目的 Ruby 绑定。
- [fast_jsonparser](https://github.com/anilmaurya/fast_jsonparser): simdjson 项目的 Ruby 绑定。
- [simdjson-go](https://github.com/minio/simdjson-go): 使用 Golang 汇编的 Go 移植。
- [rcppsimdjson](https://github.com/eddelbuettel/rcppsimdjson): R 绑定。
- [simdjson_erlang](https://github.com/ChomperT/simdjson_erlang): erlang 绑定。
- [simdjsone](https://github.com/saleyn/simdjsone): erlang 绑定。
- [lua-simdjson](https://github.com/FourierTransformer/lua-simdjson): lua 绑定。
- [hermes-json](https://hackage.haskell.org/package/hermes-json): haskell 绑定。
- [zimdjson](https://github.com/EzequielRamis/zimdjson): Zig 移植。
- [simdjzon](https://github.com/travisstaloch/simdjzon): Zig 移植。
- [JSON-Simd](https://github.com/rawleyfowler/JSON-simd): Raku 绑定。
- [JSON::SIMD](https://metacpan.org/pod/JSON::SIMD): Perl 绑定;功能齐全的 JSON 模块,使用 simdjson 进行解码。
- [gemmaJSON](https://github.com/sainttttt/gemmaJSON): 基于 simdjson 绑定的 Nim JSON 解析器。
- [simdjson-java](https://github.com/simdjson/simdjson-java): Java 移植。
- [mruby-fast-json](https://github.com/Asmod4n/mruby-fast-json): 具有 high API 覆盖率的 mruby 绑定。
## 关于 simdjson
simdjson 库利用现代微架构,通过 SIMD 向量指令进行并行化,减少分支预测错误,并减少数据依赖性,以充分利用每个 CPU 的多个执行核心。
我们的默认前端称为 On-Demand,我们就此写了一篇论文:
- John Keiser, Daniel Lemire, [On-Demand JSON: A Better Way to Parse Documents?](http://arxiv.org/abs/2312.17149), Software: Practice and Experience 54 (6), 2024.
有些人[喜欢阅读第一篇 (2019) simdjson 论文](https://arxiv.org/abs/1902.08318):关于 simdjson 设计和实现的描述在我们的研究文章中:
- Geoff Langdale, Daniel Lemire, [Parsing Gigabytes of JSON per Second](https://arxiv.org/abs/1902.08318), VLDB Journal 28 (6), 2019.
我们有一篇专注于 UTF-8 验证的深入论文:
- John Keiser, Daniel Lemire, [Validating UTF-8 In Less Than One Instruction Per Byte](https://arxiv.org/abs/2010.03090), Software: Practice & Experience 51 (5), 2021.
我们还有一篇非正式的[博文提供了一些背景和上下文](https://branchfree.org/2019/02/25/paper-parsing-gigabytes-of-json-per-second/)。
对于喜欢视频的人,我们在 2019 年旧金山 QCon 大会上有一个演讲
[](http://www.youtube.com/watch?v=wlvKAT7SZIQ)
(这是投票最佳的演讲,我们有点自豪。) 我们还有一个 CppCon 2025 演讲。我们展示了 C++26 反射如何允许单行序列化 (to_json(player)) 或反序列化——无需侵入式宏或手动映射——仅使用 C++ 标准库。无论您是性能发烧友还是仅仅对 C++ 未来十年的路线图感兴趣,请观看我们的完整演讲! [](http://www.youtube.com/watch?v=Mcgk3CxHYMs)
## 引用此工作 如果您在发表的研究中使用 simdjson,请引用该软件库。一个合适的 BibTeX 条目是: ``` @misc{simdjson, title={{The simdjson library: Parsing Gigabytes of JSON per Second}}, author={Daniel Lemire and Geoff Langdale and John Keiser and Paul Dreik and Francisco Thiesen and others}, year={2019}, howpublished={Software library}, note={https://github.com/simdjson/simdjson} } ``` ## 资助 这项工作得到了加拿大自然科学与工程研究委员会在 RGPIN-2017-03910 和 RGPIN-2024-03787 拨款下的支持。 ## 许可证 本代码根据 [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) 以及 MIT License 提供。作为用户,您可以选择您偏好的许可证。 在 Windows 下,我们使用 windows/dirent_portable.h 文件构建一些工具 (位于我们的库代码之外):它基于宽松的 (商业友好的) MIT 许可证。 对于不支持 [C++17](https://en.wikipedia.org/wiki/C%2B%2B17) 的编译器,我们捆绑了在 [Boost license](http://www.boost.org/LICENSE_1_0.txt) 下发布的 string-view 库。与 Apache 许可证一样,Boost 许可证是一个允许商业再分发的宽松许可证。 为了实现高效的数字序列化,我们捆绑了 Florian Loitsch 的 Grisu2 算法实现,用于二进制到十进制浮点数的转换。该实现由 JSON for Modern C++ 库稍作修改。Florian Loitsch 的实现和 JSON for Modern C++ 均在 MIT 许可证下提供。 为了进行运行时分发,我们使用了 PyTorch 项目中的一些代码,这些代码根据 3-clause BSD 许可证授权。
无论是处理小文件 (例如 300 字节) 还是大文件 (例如 3MB),simdjson 库都能提供高速度。下图展示了在 3.4 GHz Skylake 处理器 (GNU GCC 9, -O3) 上,针对[通过脚本生成的各种大小的合成文件](https://github.com/simdjson/simdjson_experiments_vldb2019/blob/master/experiments/growing/gen.py)的解析速度。
[我们所有的实验均可重现](https://github.com/simdjson/simdjson_experiments_vldb2019)。
对于 NDJSON 文件,借助[我们的多线程解析函数](https://github.com/simdjson/simdjson/blob/master/doc/parse_many.md),速度可以超过 3 GB/s。
## 软件包
[](https://repology.org/project/simdjson/versions)
## simdjson 的绑定与移植
我们区分“绑定” (仅封装 C++ 代码) 和移植到另一种编程语言 (重新实现所有内容)。
- [ZippyJSON](https://github.com/michaeleisel/zippyjson): simdjson 项目的 Swift 绑定。
- [libpy_simdjson](https://github.com/gerrymanoim/libpy_simdjson/): 使用 [libpy](https://github.com/quantopian/libpy) 的 simdjson 高速 Python 绑定。
- [pysimdjson](https://github.com/TkTech/pysimdjson): simdjson 项目的 Python 绑定。
- [cysimdjson](https://github.com/TeskaLabs/cysimdjson): simdjson 项目的高速 Python 绑定。
- [simdjson-rs](https://github.com/simd-lite): Rust 移植。
- [simdjson-rust](https://github.com/SunDoge/simdjson-rust): Rust 封装 (绑定)。
- [SimdJsonSharp](https://github.com/EgorBo/SimdJsonSharp): .NET Core 的 C# 版本 (绑定和完整移植)。
- [simdjson_nodejs](https://github.com/luizperes/simdjson_nodejs): simdjson 项目的 Node.js 绑定。
- [simdjson_php](https://github.com/crazyxman/simdjson_php): simdjson 项目的 PHP 绑定。
- [simdjson_ruby](https://github.com/saka1/simdjson_ruby): simdjson 项目的 Ruby 绑定。
- [fast_jsonparser](https://github.com/anilmaurya/fast_jsonparser): simdjson 项目的 Ruby 绑定。
- [simdjson-go](https://github.com/minio/simdjson-go): 使用 Golang 汇编的 Go 移植。
- [rcppsimdjson](https://github.com/eddelbuettel/rcppsimdjson): R 绑定。
- [simdjson_erlang](https://github.com/ChomperT/simdjson_erlang): erlang 绑定。
- [simdjsone](https://github.com/saleyn/simdjsone): erlang 绑定。
- [lua-simdjson](https://github.com/FourierTransformer/lua-simdjson): lua 绑定。
- [hermes-json](https://hackage.haskell.org/package/hermes-json): haskell 绑定。
- [zimdjson](https://github.com/EzequielRamis/zimdjson): Zig 移植。
- [simdjzon](https://github.com/travisstaloch/simdjzon): Zig 移植。
- [JSON-Simd](https://github.com/rawleyfowler/JSON-simd): Raku 绑定。
- [JSON::SIMD](https://metacpan.org/pod/JSON::SIMD): Perl 绑定;功能齐全的 JSON 模块,使用 simdjson 进行解码。
- [gemmaJSON](https://github.com/sainttttt/gemmaJSON): 基于 simdjson 绑定的 Nim JSON 解析器。
- [simdjson-java](https://github.com/simdjson/simdjson-java): Java 移植。
- [mruby-fast-json](https://github.com/Asmod4n/mruby-fast-json): 具有 high API 覆盖率的 mruby 绑定。
## 关于 simdjson
simdjson 库利用现代微架构,通过 SIMD 向量指令进行并行化,减少分支预测错误,并减少数据依赖性,以充分利用每个 CPU 的多个执行核心。
我们的默认前端称为 On-Demand,我们就此写了一篇论文:
- John Keiser, Daniel Lemire, [On-Demand JSON: A Better Way to Parse Documents?](http://arxiv.org/abs/2312.17149), Software: Practice and Experience 54 (6), 2024.
有些人[喜欢阅读第一篇 (2019) simdjson 论文](https://arxiv.org/abs/1902.08318):关于 simdjson 设计和实现的描述在我们的研究文章中:
- Geoff Langdale, Daniel Lemire, [Parsing Gigabytes of JSON per Second](https://arxiv.org/abs/1902.08318), VLDB Journal 28 (6), 2019.
我们有一篇专注于 UTF-8 验证的深入论文:
- John Keiser, Daniel Lemire, [Validating UTF-8 In Less Than One Instruction Per Byte](https://arxiv.org/abs/2010.03090), Software: Practice & Experience 51 (5), 2021.
我们还有一篇非正式的[博文提供了一些背景和上下文](https://branchfree.org/2019/02/25/paper-parsing-gigabytes-of-json-per-second/)。
对于喜欢视频的人,我们在 2019 年旧金山 QCon 大会上有一个演讲[](http://www.youtube.com/watch?v=wlvKAT7SZIQ)
(这是投票最佳的演讲,我们有点自豪。) 我们还有一个 CppCon 2025 演讲。我们展示了 C++26 反射如何允许单行序列化 (to_json(player)) 或反序列化——无需侵入式宏或手动映射——仅使用 C++ 标准库。无论您是性能发烧友还是仅仅对 C++ 未来十年的路线图感兴趣,请观看我们的完整演讲! [](http://www.youtube.com/watch?v=Mcgk3CxHYMs)
## 引用此工作 如果您在发表的研究中使用 simdjson,请引用该软件库。一个合适的 BibTeX 条目是: ``` @misc{simdjson, title={{The simdjson library: Parsing Gigabytes of JSON per Second}}, author={Daniel Lemire and Geoff Langdale and John Keiser and Paul Dreik and Francisco Thiesen and others}, year={2019}, howpublished={Software library}, note={https://github.com/simdjson/simdjson} } ``` ## 资助 这项工作得到了加拿大自然科学与工程研究委员会在 RGPIN-2017-03910 和 RGPIN-2024-03787 拨款下的支持。 ## 许可证 本代码根据 [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) 以及 MIT License 提供。作为用户,您可以选择您偏好的许可证。 在 Windows 下,我们使用 windows/dirent_portable.h 文件构建一些工具 (位于我们的库代码之外):它基于宽松的 (商业友好的) MIT 许可证。 对于不支持 [C++17](https://en.wikipedia.org/wiki/C%2B%2B17) 的编译器,我们捆绑了在 [Boost license](http://www.boost.org/LICENSE_1_0.txt) 下发布的 string-view 库。与 Apache 许可证一样,Boost 许可证是一个允许商业再分发的宽松许可证。 为了实现高效的数字序列化,我们捆绑了 Florian Loitsch 的 Grisu2 算法实现,用于二进制到十进制浮点数的转换。该实现由 JSON for Modern C++ 库稍作修改。Florian Loitsch 的实现和 JSON for Modern C++ 均在 MIT 许可证下提供。 为了进行运行时分发,我们使用了 PyTorch 项目中的一些代码,这些代码根据 3-clause BSD 许可证授权。
标签:C++, ClickHouse, GNU通用公共许可证, JSON解析器, Node.js, SIMD, UTF-8校验, 反序列化, 可视化界面, 后端开发, 大数据, 序列化, 开源库, 性能优化, 搜索引擎爬虫, 数据库, 数据擦除, 数据格式, 日志审计, 检测绕过, 目录扫描, 算法, 解析器, 计算机科学, 逆向工具, 高性能计算