Skip to content

optimize: speed up string and separator handling - #132

Open
sys9kdr wants to merge 1 commit into
openresty:masterfrom
sys9kdr:optimize/strings-separators
Open

sys9kdr wants to merge 1 commit into
openresty:masterfrom
sys9kdr:optimize/strings-separators

Conversation

@sys9kdr

@sys9kdr sys9kdr commented Oct 25, 2026

Copy link
Copy Markdown

Summary

Strings dominate typical JSON payloads, and lua-cjson does avoidable work on
them in both directions:

  • the encoder appends every byte and escape through the strbuf API even
    though the worst-case space is already reserved
  • the decoder copies every string through json->tmp, even when there is
    nothing to unescape
  • the parser runs a full token dispatch for the : and , separators

Changes

  • encode: write the quoted string straight into the reserved output
    space. An escape is either two bytes or \u00XX, so the loop needs no
    length bookkeeping; strbuf_extend_length() publishes the result.
  • decode: scan for the closing quote first. If the string has no escape
    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 input
    buffer outlives the decode call. Strings with escapes keep the copying
    path, now writing into the buffer with a cursor.
  • decode: after a value, consume an immediate : or , in place. Any
    other 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.

case base (µs/op) patched (µs/op) ratio
strings encode 159 140 0.88
strings decode 188 130 0.69
escapes encode 80 62 0.77
escapes decode 71 56 0.79
lsp decode 740 666 0.90
pretty decode 756 656 0.87

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, which
touches 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.lua passes on LuaJIT 2.1 and Lua 5.4 (gcc)
  • New tests cover empty strings, whitespace around : / ,, and comments
    before object/array commas
  • clang -Wall -Wextra -pedantic -Werror clean
  • Differential test against master: 140,000 generated string/JSON cases
    per interpreter (all byte values, escapes, unicode, malformed input,
    with and without comments), results and error messages match exactly
  • CI (Lua 5.1–5.4, LuaJIT, valgrind, cppcheck)

- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant