PROJECT NAME: simdjson-zig
simdjson-zig is a Zig package I’ve created that provides bindings to simdjson for Zig projects.
As far as I know, this is the first Zig binding to this library (previously some Zig ports existed). A few interesting things this library does:
- Attempts to provide a drop-in replacement for std.json functionality like
parseFromSlice(),parseFromTokenSource(), etc, to make it easy to swap in higher performance JSON parsing for existing code (at the expense of memory use). It was interesting to learn how these standard library APIs worked at a deeper level and to implement a performant backend that uses simdjson (I also had to learn a lot about how simdjson works). - Minimizes FFI overhead to improve performance by making a single parse call in C++ and passing back an internal format that I walk on the Zig side on demand. As long as I can emulate what std.json.Scanner does for each method call in my own TapeTokenSource type, I can substitute my own type and get the std library’s “walk to fill out a struct” implementation for free, just providing tokens on demand as it asks for them.
- Wraps C++, as simdjson doesn’t provide a C interface. I wrote a C shim layer and used translate_c to access it from Zig.
- Tons of unit tests and fuzzing to increase the quality and reduce the likelihood of regressions in the future.
Wishlist for future std library JSON implementations:
- Allow token sources to provide already-parsed numbers instead of forcing them to pass strings (i.e. updating the grammar in Zig Documentation). simdjson parses numbers as part of its implementation, so having to re-stringify them just to pass them through to the Zig parser is inefficient and makes it hard to round-trip numbers correctly.
- Make it possible to call into C code at comptime. I’m sure there are technical reasons this is not allowed, but it would be cool to parse JSON to structs at comptime using simdjson.
Open to questions or feedback about my use of Zig, simdjson, or anything else! I’m new to Zig so trying to learn as much as I can!
Supported Zig versions
0.17