Repository navigation
Conversation
- encode: write strings straight into the reserved buffer - decode: hand unescaped strings to Lua as a slice of the input, skipping the temporary buffer - decode: consume ':' and ',' directly instead of a token 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
Strings dominate typical JSON payloads, and lua-cjson does avoidable work on
them in both directions:
though the worst-case space is already reserved
json->tmp, even when there isnothing to unescape
:and,separatorsChanges
space. An escape is either two bytes or
\u00XX, so the loop needs nolength bookkeeping;
strbuf_extend_length()publishes the result.sequences, hand Lua a pointer/length slice of the input instead of copying
through
json->tmp. Lua copies when it pushes the string, and the inputbuffer outlives the decode call. Strings with escapes keep the copying
path, now writing into the buffer with a cursor.
:or,in place. Anyother byte (whitespace, comments, malformed input) still goes through the
existing token path, so error messages and character positions are
unchanged.
No API or configuration changes; encoded output and decoded values are
identical.
Benchmarks
Payloads: 5,000 plain 24-character words (135 KB JSON), 2,000 strings with
escapes (78 KB), and the LSP-style payload (2,000 objects, 180 KB). LuaJIT
2.1, AMD Ryzen AI MAX+ 395 (WSL2), pinned to one core. Fixed iteration counts;
gcc 15 rounds, clang 9 rounds; base and patched processes interleaved;
medians reported.
clang gives 0.88 / 0.65 / 0.79 / 0.91 / 0.85 / 0.85 for the same rows, and
Lua 5.4 gives 0.90 / 0.78 / 0.74 / 0.87 / 0.92 / 0.89.
ints-encode, whichtouches neither strings nor separators, is 1.00x as a control.
perf stat -e instructions:u(slope of two iteration counts):strings encode 3.77M → 3.41M (-9.6%), strings decode 3.62M → 2.45M (-32.2%),
lsp decode 14.76M → 13.48M (-8.7%).
Test plan
tests/test.luapasses on LuaJIT 2.1 and Lua 5.4 (gcc):/,, and commentsbefore object/array commas
clang -Wall -Wextra -pedantic -Werrorcleanper interpreter (all byte values, escapes, unicode, malformed input,
with and without comments), results and error messages match exactly