Skip to content

Use wrapping arithmetic in from_str_radix - #163099

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
maxdexh:int-from-str-loop-unchecked
Sep 23, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
maxdexh:int-from-str-loop-unchecked

Conversation

@maxdexh

@maxdexh maxdexh commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

View all comments

Uses wrapping arithmetic in the fast/unchecked loop of from_str_radix.

Originally this PR was written using unchecked arithmetic:
This generates identical assembly when compiling with -O: https://godbolt.org/z/Gvb7G9YEM
However, it significantly simplifies the asm of -O -Coverflow-checks (e.g. RfL): https://godbolt.org/z/Pah9fMfrd
More precisely, the -O -Coverflow-checks output becomes identical to the -O one: https://godbolt.org/z/WoYMThP8e
In conclusion, it makes more sense to use wrapping operations here.

@Darksonn

Copy link
Copy Markdown
Member

@rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 21, 2026
@maxdexh

maxdexh commented Sep 21, 2026 •

Copy link
Copy Markdown
Member Author

You need to run bors (@bors try is enough)

@Darksonn

Copy link
Copy Markdown
Member

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
[EXPERIMENT] Use unchecked arithmetic in `from_str_radix`
@maxdexh

maxdexh commented Sep 21, 2026 •

Copy link
Copy Markdown
Member Author

Is rustbot broken? @rustbot label T-libs

@rustbot

rustbot commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

Error: Label waiting-on-author can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 21, 2026
@Darksonn

Copy link
Copy Markdown
Member

The waiting-on-author label has an S- prefix

@Darksonn Darksonn closed this Sep 21, 2026
@Darksonn Darksonn reopened this Sep 21, 2026
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 21, 2026
@maxdexh

maxdexh commented Sep 21, 2026 •

Copy link
Copy Markdown
Member Author

Alright so godbolt reveals that they generate the same optimized IR with overflow checks (for u32). This PR causes the overflow checked version to become identical to that as well: https://godbolt.org/z/1xTf3Y3j8

i.e. this should not change runtime perf when running without overflow checks, but with overflow checks it should cause a decent amount of improvement (and get rid of the panics)

@rust-bors

rust-bors Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 1d6776a (1d6776a3cda0d0b6962859a966a20cfe82558cab)
Base parent: 220b36c (220b36c420c49c59923f54cd4a76634fac98a067)

@rust-timer

This comment has been minimized.

@maxdexh
maxdexh force-pushed the int-from-str-loop-unchecked branch from eca2552 to 2e0c4ed Compare September 21, 2026 10:36
@maxdexh

maxdexh commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

Since this makes the correctness of can_not_overflow soundness critical (and that function is underdocumented), I added a correctness proof as a comment. Shouldn't affect the perf run

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (1d6776a): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -2.4%, secondary 2.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.8% [2.8%, 2.8%] 1
Improvements ✅
(primary)
-2.4% [-2.9%, -1.9%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.4% [-2.9%, -1.9%] 2

Cycles

Results (primary -2.0%, secondary -4.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
-4.4% [-4.4%, -4.4%] 1
All ❌✅ (primary) -2.0% [-2.0%, -2.0%] 1

Binary size

Results (primary 0.0%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.3%] 5
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 4
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-0.2%, 0.3%] 6

Bootstrap: 499.144s -> 497.776s (-0.27%)
Artifact size: 408.93 MiB -> 409.71 MiB (0.19%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 21, 2026
@maxdexh maxdexh changed the title [EXPERIMENT] Use unchecked arithmetic in from_str_radix Use unchecked arithmetic in from_str_radix Sep 21, 2026
@maxdexh

maxdexh commented Sep 21, 2026 •

Copy link
Copy Markdown
Member Author

r? libs

edit: lol

@maxdexh
maxdexh marked this pull request as ready for review September 21, 2026 11:11
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 21, 2026
@Darksonn

Copy link
Copy Markdown
Member

@rustbot label I-libs-nominated

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 21, 2026
@Darksonn Darksonn removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 21, 2026
Comment thread library/core/src/num/mod.rs Outdated
@maxdexh
maxdexh force-pushed the int-from-str-loop-unchecked branch from 2e0c4ed to 75f50bf Compare September 21, 2026 11:25
@clarfonthey

Copy link
Copy Markdown
Contributor

If this produces the same assembly for wrapping we should use wrapping instead of unchecked, since just adding unsafe code is a higher bar than changing an operation to always wrap.

@maxdexh

maxdexh commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

Agreed. I'll add a comment mentioning this though

@maxdexh maxdexh changed the title Use unchecked arithmetic in from_str_radix Use wrapping arithmetic in from_str_radix Sep 22, 2026
@maxdexh
maxdexh force-pushed the int-from-str-loop-unchecked branch from 75f50bf to 3e43dc9 Compare September 22, 2026 17:16
@clarfonthey clarfonthey added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed I-libs-nominated Nominated for discussion during a libs team meeting. S-waiting-on-t-libs Status: Awaiting decision from T-libs labels Sep 22, 2026
Comment thread library/core/src/num/mod.rs Outdated
@maxdexh
maxdexh force-pushed the int-from-str-loop-unchecked branch from 3e43dc9 to 84a341b Compare September 22, 2026 18:25
@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot ready

Actually handing review back to Alice

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 22, 2026

@Darksonn Darksonn left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 84a341b has been approved by Darksonn

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 22, 2026
…d, r=Darksonn

Use wrapping arithmetic in `from_str_radix`

Uses wrapping arithmetic in the fast/unchecked loop of `from_str_radix`.

Originally this PR was written using unchecked arithmetic:
This generates identical assembly when compiling with `-O`: https://godbolt.org/z/Gvb7G9YEM
However, it significantly simplifies the asm of `-O -Coverflow-checks` (e.g. RfL): https://godbolt.org/z/Pah9fMfrd
More precisely, the `-O -Coverflow-checks` output becomes identical to the `-O` one: https://godbolt.org/z/WoYMThP8e
In conclusion, it makes more sense to use wrapping operations here.

@tgross35 tgross35 Sep 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is to actually remove panics and keep them gone then why no codegen test?

View changes since the review

@maxdexh maxdexh Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function can already panic from the radix check. This change just brings the loop in line with the no-overflow-check version, which is the default. Adding a test feels more like an obligation that we will keep it this way. Idk if that is what we want, but I'm open to discussion on zulip ^^

rust-bors Bot pushed a commit that referenced this pull request Sep 22, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #156949 (Detect missing else in let statement)
 - #160436 (stabilize `Box::take`)
 - #160570 (macro_metavar_expr_concat: support concatenating into string literals)
 - #162837 (Dedicated Display type for CStr::display)
 - #163099 (Use wrapping arithmetic in `from_str_radix`)
 - #163166 (Tiny cleanups to deferred liveness)
 - #161667 (Add `f16` inline ASM support for `nvptx64-nvidia-cuda`)
 - #163063 (Restore `Send` and `Sync` for `BorrowedCursor`)
 - #163097 (OpenBSD/sparc64 has switched from GCC to Clang)
 - #163126 (Skip redundant storage-conflict updates during coroutine layout)
 - #163135 (librustdoc: remove stale dep on base64)
 - #163146 (tests: Update `f16b` codegen test for LoongArch and RISC-V)
 - #163159 (treat inductive cycles as ambig)
@rust-bors
rust-bors Bot merged commit fa0d183 into rust-lang:main Sep 23, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 23, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 23, 2026
Rollup merge of #163099 - maxdexh:int-from-str-loop-unchecked, r=Darksonn

Use wrapping arithmetic in `from_str_radix`

Uses wrapping arithmetic in the fast/unchecked loop of `from_str_radix`.

Originally this PR was written using unchecked arithmetic:
This generates identical assembly when compiling with `-O`: https://godbolt.org/z/Gvb7G9YEM
However, it significantly simplifies the asm of `-O -Coverflow-checks` (e.g. RfL): https://godbolt.org/z/Pah9fMfrd
More precisely, the `-O -Coverflow-checks` output becomes identical to the `-O` one: https://godbolt.org/z/WoYMThP8e
In conclusion, it makes more sense to use wrapping operations here.
@maxdexh
maxdexh deleted the int-from-str-loop-unchecked branch September 23, 2026 06:35
github-actions Bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Oct 2, 2026
…d, r=Darksonn

Use wrapping arithmetic in `from_str_radix`

Uses wrapping arithmetic in the fast/unchecked loop of `from_str_radix`.

Originally this PR was written using unchecked arithmetic:
This generates identical assembly when compiling with `-O`: https://godbolt.org/z/Gvb7G9YEM
However, it significantly simplifies the asm of `-O -Coverflow-checks` (e.g. RfL): https://godbolt.org/z/Pah9fMfrd
More precisely, the `-O -Coverflow-checks` output becomes identical to the `-O` one: https://godbolt.org/z/WoYMThP8e
In conclusion, it makes more sense to use wrapping operations here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants