Repository navigation
Conversation
- decode: consume '}' and ']' directly instead of a full token dispatch
- encode: write "{}" directly instead of a second lua_next() scan
AI-assisted
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.
Summary
Empty objects and arrays are common in JSON-RPC/LSP traffic and in configuration
data, but lua-cjson does redundant work for them in both directions:
}/]lua_next()when encoding it asan object (once in
lua_array_length(), once injson_append_object())Changes
{/[, consume}/]directly when it is the nextbyte. Whitespace, comments,
decode_max_depthaccounting and error messagesare unchanged; anything other than the immediate closing bracket still uses
the existing token path.
lua_array_length()returns 0 the table has no entries (anyentry makes it return -1), so write
{}directly. This runs after theexisting
empty_array_mtcheck, socjson.empty_arraystill encodes as[].No API or configuration changes; encoded output is unchanged.
Benchmarks
Payload: 20,000 empty tables (
[{},...], 60 KB JSON). LuaJIT 2.1, AMD RyzenAI MAX+ 395 (WSL2), pinned to one core. Fixed iteration counts; gcc 15 rounds,
clang 9 rounds; base and patched processes interleaved; medians reported.
perf stat -e instructions:u(slope of two iteration counts):encode 14.42M → 11.32M (-21.5%), decode 13.39M → 12.55M (-6.3%).
An identical-binary control run is within 0.5%, and
pretty-decode(a payloadwith no empty containers) is unchanged (0.99–1.01), as expected.
Test plan
tests/test.luapasses on LuaJIT 2.1 and Lua 5.4 (gcc)decode_max_depthboundary, and indented encodingclang -Wall -Wextra -pedantic -Werrorcleanper interpreter, no output differences