Repository navigation
Fast paths for integer numbers in print_number and parse_number - #1144
Open
kentambolt wants to merge 1 commit into
Open
kentambolt wants to merge 1 commit into
kentambolt wants to merge 1 commit into
Conversation
Author
|
Verification programs and benchmark kit referred to in the description: https://gist.github.com/kentambolt/91a9f3ff59a111d853623ffef605584d |
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.
What
Two small, self-contained fast paths. Every other case goes through the existing code unchanged.
cjson_fast_i32toa, two digits at a time from a 200-byte table) instead ofsprintf("%d"). It produces the same bytes and length assprintf("%d")for everyint, includingINT_MIN. Its buffer size is derived fromsizeof(int) * CHAR_BIT, so it does not assume a 32-bitint.-?[0-9]{1,15}, no.,e,Eor+) is handled byparse_integer_fast_path, which accumulates the digits in adoubleinstead ofmalloc+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 roundedstrtod.-0keeps its sign.The code is C89 and builds with the project's default CMake flags (
-std=c89 -pedantic -Werrorand 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-collectoncJSON_Parseand oncJSON_PrintUnformatted), GCC 13.3-O2, glibc 2.39, x86_64. Fixtures come from a deterministic generator (attached), one parse and one print per run.The float-heavy case shows the cost of the fallback: with GCC 13 the token-scanning loop in
parse_numberis 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
sprintf("%d")for all 2^32intvalues: 0 differences.-?[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, viacJSON_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, withvaluedoublecompared bytewise. 0 differences, also under ASan + UBSan.valueintsaturation and the sign of-0. They pass on both the old and the new code. Like the existing tests, they assume a 32-bitint.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.