Speed up decimal integer parsing with SWAR - #161019
Open
kiana1kaslana wants to merge 2 commits into
Open
Conversation
Use SIMD-within-a-register to process 8 ASCII digits at once in from_str_radix when radix == 10 and the result is guaranteed not to overflow. Falls back to the existing per-digit loop for the remaining 0-7 digits. The fast path uses two helper functions: - is_8digits: branch-free check that all 8 bytes are b'0'..=b'9' - parse_8digits: 3 multiplications to pack 8 digits into a u64 Benchmark on 16-20 digit decimal strings (5000 iterations, stage 1): bench_u64_from_str_radix_10_long 98818 -> 73194 ns (-25.9%) bench_i64_from_str_radix_10_long 149705 -> 120089 ns (-19.8%) Also add LONG_ASCII_NUMBERS and from_str_radix_long_bench macro to exercise the fast path with strings that trigger 2+ SWAR iterations.
Collaborator
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @JohnTitor (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
Align trailing `//` comments vertically in the new benchmark data constant. rustfmt in nightly runs during the tidy CI job flags the misaligned comments.
tgross35
reviewed
Aug 13, 2026
Comment on lines
+1844
to
+1859
| if radix == 10 { | ||
| while let [a, b, c, d, e, f, g, h, rest @ ..] = digits { | ||
| let chunk = u64::from_le_bytes([*a, *b, *c, *d, *e, *f, *g, *h]); | ||
| if !is_8digits(chunk) { | ||
| return Err(PIE { kind: InvalidDigit }); | ||
| } | ||
| let parsed = parse_8digits(chunk) as $int_ty; | ||
| result = result * (100_000_000u32 as $int_ty); | ||
| if is_positive { | ||
| result = result + parsed; | ||
| } else { | ||
| result = result - parsed; | ||
| } | ||
| digits = rest; | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
There should probably be a 32- and (maybe) 16-bit version so this doesn't wind up slower on those platforms
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.
Speed up from_str_radix for base 10 with SWAR
Closes #87249
Process 8 decimal digits at a time using SIMD-within-a-register
instead of one digit per loop iteration. Only applies when radix
is 10 and can_not_overflow guarantees the result fits, so the
fast path needs no overflow checks.
The SWAR technique was suggested by @bormand and @Alexhuszagh in
the issue thread. Two helpers:
is_8digits(v: u64) -> bool — branch-free check that all 8
bytes are b'0'..=b'9'
parse_8digits(v: u64) -> u64 — 3 multiplications to pack 8
digits into a single value
Remaining <8 digits fall through to the existing per-digit loop.
Benchmark (stage 1, x86_64, 16-20 digit strings × 5000):
bench_u64_from_str_radix_10_long 98818 → 73194 ns (−25.9%)
bench_i64_from_str_radix_10_long 149705 → 120089 ns (−19.8%)
The mixed-input benchmark barely moves because only 2 of 19 inputs
are long enough to trigger the fast path. Added LONG_ASCII_NUMBERS
and from_str_radix_long_bench! to cover it properly.