Add JSON encoding with simdjson 4.6.4#112
Open
jlambatl wants to merge 3 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This supersedes [#109] with a focused implementation based directly on the current master branch. It incorporates the review feedback from the original PR while removing unrelated formatting, benchmark, security-test, and CI changes. This updated PR includes changes uncovered through internal reviews.
Summary
This adds JSON encoding using simdjson’s string_builder:
The following configuration functions are also provided:
The optional argument is an options table, so additional encoding options can be introduced later without changing the function signature.
Encoding behaviour
Strings,numbers,booleans,tables,nil, andsimdjson.nullare supported.integersare preserved onLua 5.3and newer.NaNandinfinityare rejected because they are not valid JSON numbers.128to protect the native stack.Thread-local state and buffer ownership
The reusable simdjson parser, padded parse buffer, and encoder string builder are now thread-local. This prevents mutable parser and builder state from being shared across threads.
The encoder buffer is owned by a thread-local
std::unique_ptr, ensuring it is released when the thread exits rather than leaking a raw allocation.Lua-owned strings don't guarantee the trailing padding required by simdjson. The previous page-boundary heuristic could therefore allow simdjson to read beyond the Lua allocation. Parsing now copies input into a reusable thread-local padded buffer with explicit overflow and allocation checks. The allocation is retained and reused by subsequent parses on the same thread.
simdjson 4.6.4 and C++17
The vendored simdjson amalgamation has been updated from
4.2.4to the official simdjson 4.6.4 release. This version includes the string-builder overflow fix used by the encoder. The vendored files were verified against the official release artifacts.Unix, MinGW, and MSVC builds now consistently require C++17.
Previous PR Review feedback addressed
lua_encoder.cppandlua_encoder.h.Tests and CI
Focused tests cover:
The complete 35-job GitHub Actions matrix passes across:
Lua 5.1–5.5.
LuaJIT 2.0 and 2.1.
Linux.
macOS Intel and ARM64.
Windows with MinGW and MSVC.
ASan and UBSan.
The tests were authored as part of this implementation. Copilot was used only to assist with Busted boilerplate. Codex was used to review the code one more time before we moved it from our fork and committed it back to the community.