Skip to content

Fast paths for integer numbers in print_number and parse_number - #1144

Open
kentambolt wants to merge 1 commit into
DaveGamble:masterfrom
kentambolt:fast-integer-paths
Open

kentambolt wants to merge 1 commit into
DaveGamble:masterfrom
kentambolt:fast-integer-paths

Conversation

@kentambolt

@kentambolt kentambolt commented Nov 30, 2026 •

Copy link
Copy Markdown

What

Two small, self-contained fast paths. Every other case goes through the existing code unchanged.

  • print_number: in the existing integer branch, write the digits with a small helper (cjson_fast_i32toa, two digits at a time from a 200-byte table) instead of sprintf("%d"). It produces the same bytes and length as sprintf("%d") for every int, including INT_MIN. Its buffer size is derived from sizeof(int) * CHAR_BIT, so it does not assume a 32-bit int.
  • parse_number: after the existing token scan, a plain integer token (-?[0-9]{1,15}, no ., e, E or +) is handled by parse_integer_fast_path, which accumulates the digits in a double instead of malloc + strtod + free. Every intermediate value has at most 15 digits and is below 2^53, so the accumulation is exact and equals the result of a correctly rounded strtod. -0 keeps its sign.

The code is C89 and builds with the project's default CMake flags (-std=c89 -pedantic -Werror and the full warning set).

Behaviour

Given enough memory, results are identical to before: same success or failure, same parse end position (also on failure), same tree. One intended difference: the integer fast path no longer allocates a temporary buffer, so with an allocator that fails on purpose, an integer that previously failed to parse because the temporary allocation failed now parses.

Performance

Instructions counted with callgrind (--toggle-collect on cJSON_Parse and on cJSON_PrintUnformatted), GCC 13.3 -O2, glibc 2.39, x86_64. Fixtures come from a deterministic generator (attached), one parse and one print per run.

Fixture Parse before Parse after Change Print before Print after Change
Integer-heavy (2.3 MB) 302.6 M 180.3 M -40 % 188.7 M 85.1 M -55 %
Mixed (1.9 MB) 159.8 M 132.3 M -17 % 204.7 M 180.0 M -12 %
Float-heavy (2.3 MB) 213.8 M 221.6 M +3.6 % 802.4 M 798.8 M -0.4 %

The float-heavy case shows the cost of the fallback: with GCC 13 the token-scanning loop in parse_number is compiled less efficiently once the fast-path call follows it, although the loop itself is unchanged. Moving the fast path into a non-inlined function did not remove this.

How it was checked

  • Print fast path: exhaustively compared with sprintf("%d") for all 2^32 int values: 0 differences.
  • Parse fast path: differential test against the unmodified library on 9.5 M parses. Inputs: all tokens -?[0-9]{1,5} incl. leading zeros, alone and in arrays; random 7-18 digit tokens with various trailing characters; edge cases incl. 15/16-digit boundaries and nested structures. Each input is parsed NUL-terminated and, via cJSON_ParseWithLengthOpts, from exact-size buffers without a NUL for every truncation of the input. Compared: success, end position (also on failure) and the whole tree recursively, with valuedouble compared bytewise. 0 differences, also under ASan + UBSan.
  • New unit tests for digit boundaries, the 15/16-digit limit, valueint saturation and the sign of -0. They pass on both the old and the new code. Like the existing tests, they assume a 32-bit int.
  • cmake .. && make && ctest: 100 % of 19 tests pass.

The verification programs and the fixture generator are attached to this PR as a comment and are not part of the change.

This contribution was developed with AI assistance (Claude). It is offered under the project's MIT license, as is and without warranty.

@kentambolt

Copy link
Copy Markdown
Author

Verification programs and benchmark kit referred to in the description: https://gist.github.com/kentambolt/91a9f3ff59a111d853623ffef605584d

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