Skip to content

Improve parseInt throughput for short numbers#285

Open
trevorprater wants to merge 1 commit into
buger:masterfrom
trevorprater:codex/parseint-short-fast-path
Open

Improve parseInt throughput for short numbers#285
trevorprater wants to merge 1 commit into
buger:masterfrom
trevorprater:codex/parseint-short-fast-path

Conversation

@trevorprater

Copy link
Copy Markdown

This adds a fast path for parseInt when the numeric portion is shorter than
19 decimal digits. In that case the value cannot overflow int64, so the loop
only needs to validate digits and accumulate into the named int64 return
value. Longer inputs continue through the existing uint64 overflow-checked
path.

Validation:

go test -count=1 ./...
ok  	github.com/buger/jsonparser	0.163s

Benchmark command:

go test -run '^$' -bench 'BenchmarkParseInt$' -benchmem -count=20 .
benchstat baseline-bench.txt after-bench.txt

Result on my machine (darwin/arm64, Apple M4, Go 1.25.1):

            │ baseline-bench.txt │ after-bench.txt │
            │       sec/op       │     sec/op      vs base
ParseInt-10       3.129n ± 4%       2.163n ± 5%  -30.90% (p=0.000 n=20)

            │ baseline-bench.txt │ after-bench.txt │
            │        B/op        │      B/op       vs base
ParseInt-10       0.000 ± 0%        0.000 ± 0%    ~ (p=1.000 n=20)

            │ baseline-bench.txt │ after-bench.txt │
            │      allocs/op     │   allocs/op     vs base
ParseInt-10       0.000 ± 0%        0.000 ± 0%    ~ (p=1.000 n=20)

I also checked the final implementation against the previous implementation
with a temporary differential test over fixed edge cases and deterministic
random byte slices, including boundaries and malformed inputs.

I found the candidate direction while experimenting with Sleepy -
https://sleepy.run/mcp, then manually simplified and validated it before
submitting.

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