Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the implementation of iterators over slices of ZSTs #111395

Merged
merged 1 commit into from
May 12, 2023

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented May 9, 2023

Currently, slice iterators over ZSTs store end = start.wrapping_byte_add(len).

That's slightly convenient for is_empty, but kinda annoying for pretty much everything else -- see bugs like #42789, for example.

This PR instead changes it to just end = ptr::invalid(len) instead.

That's easier to think about (IMHO, at least) as well as easier to represent.

next is still to big to get inlined into the mir-opt/pre-codegen/ tests, but if I bump the inline threshold to force it to show the whole thing, this implementation is also less MIR:

> git diff --numstat
241     370     tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir
255     329     tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir
184     216     tests/mir-opt/pre-codegen/slice_iter.slice_iter_mut_next_back.PreCodegen.after.mir
182     254     tests/mir-opt/pre-codegen/slice_iter.slice_iter_next.PreCodegen.after.mir

(That's ≈70 lines less for Iter::next, for example.)

r? @ghost

Built atop #111282, so draft until that lands.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 9, 2023
@scottmcm
Copy link
Member Author

scottmcm commented May 9, 2023

@bors try @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 May 9, 2023
@bors
Copy link
Contributor

bors commented May 9, 2023

⌛ Trying commit b3389f2eb53d001e94ff67b177cb3bc41f7bbc71 with merge 84b202a37f44bc97a1f2bec83a95928739984869...

@bors
Copy link
Contributor

bors commented May 9, 2023

☀️ Try build successful - checks-actions
Build commit: 84b202a37f44bc97a1f2bec83a95928739984869 (84b202a37f44bc97a1f2bec83a95928739984869)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (84b202a37f44bc97a1f2bec83a95928739984869): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

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

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.4% [-5.2%, -3.6%] 2
Improvements ✅
(secondary)
-2.7% [-2.9%, -2.3%] 3
All ❌✅ (primary) -4.4% [-5.2%, -3.6%] 2

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

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

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.3%, 0.3%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-1.3%, -0.0%] 66
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.0%] 8
All ❌✅ (primary) -0.5% [-1.3%, 0.3%] 67

Bootstrap: 658.646s -> 657.694s (-0.14%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 9, 2023
@scottmcm scottmcm marked this pull request as ready for review May 10, 2023 03:59
@rustbot
Copy link
Collaborator

rustbot commented May 10, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@scottmcm
Copy link
Member Author

r? @the8472

library/core/src/slice/iter.rs Show resolved Hide resolved
library/core/src/slice/iter/macros.rs Outdated Show resolved Hide resolved
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 10, 2023
Currently, slice iterators over ZSTs store `end = start.wrapping_byte_add(len)`.

That's slightly convenient for `is_empty`, but kinda annoying for pretty much everything else -- see bugs like 42789, for example.

This PR instead changes it to just `end = ptr::invalid(len)` instead.

That's easier to think about (IMHO, at least) as well as easier to represent.
@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 May 10, 2023
@the8472
Copy link
Member

the8472 commented May 10, 2023

Interesting, some debug binary size wins. I guess debug doesn't even eliminate the IS_ZST branches?

@bors r+

@bors
Copy link
Contributor

bors commented May 10, 2023

📌 Commit 15aa7fa has been approved by the8472

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented May 10, 2023

🌲 The tree is currently closed for pull requests below priority 50. This pull request will be tested once the tree is reopened.

@bors bors 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 May 10, 2023
@scottmcm
Copy link
Member Author

I guess debug doesn't even eliminate the IS_ZST branches?

Looks like it doesn't, yeah: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=5c4cd54a7b07a07ccecea6a7a092c57e

  br i1 true, label %bb1, label %bb2, !dbg !149

And wrapping_byte_sub is more complicated than it seems https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b45460e41f9c20b10c49223f49be0f4d as it needs some extra stuff like

  %_12.i = alloca %"core::ptr::metadata::PtrComponents<u32>", align 8
  %_11.i = alloca %"core::ptr::metadata::PtrRepr<u32>", align 8
  %_10.i = alloca %"core::ptr::metadata::PtrRepr<u32>", align 8

that isn't there in just the addr+invalid version.

@bors
Copy link
Contributor

bors commented May 11, 2023

⌛ Testing commit 15aa7fa with merge 41c9748da663a483561273aa0ae71d47dba10c61...

@bors
Copy link
Contributor

bors commented May 11, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 11, 2023
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@scottmcm
Copy link
Member Author

@bors retry

@bors bors 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 May 11, 2023
@scottmcm
Copy link
Member Author

Well, https://www.githubstatus.com/incidents/nf7s6933tnn8 says things are supposed to be working, and #111475 hit a legit problem, not an infrastructure problem, so let's give another one a shot 🤞

@bors p=101 (not important, just getting through the tree, feel free to interrupt)

@bors
Copy link
Contributor

bors commented May 11, 2023

⌛ Testing commit 15aa7fa with merge 5b24e12...

@bors
Copy link
Contributor

bors commented May 12, 2023

☀️ Test successful - checks-actions
Approved by: the8472
Pushing 5b24e12 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 12, 2023
@bors bors merged commit 5b24e12 into rust-lang:master May 12, 2023
11 checks passed
@rustbot rustbot added this to the 1.71.0 milestone May 12, 2023
@scottmcm scottmcm deleted the slice-iter-zst-experiment branch May 12, 2023 02:56
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (5b24e12): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.3% [2.3%, 2.3%] 1
Regressions ❌
(secondary)
3.2% [3.2%, 3.2%] 1
Improvements ✅
(primary)
-6.3% [-8.7%, -4.0%] 2
Improvements ✅
(secondary)
-2.9% [-2.9%, -2.8%] 2
All ❌✅ (primary) -3.5% [-8.7%, 2.3%] 3

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-1.1%, -0.0%] 67
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.0%] 8
All ❌✅ (primary) -0.5% [-1.1%, -0.0%] 67

Bootstrap: 659.726s -> 661.63s (0.29%)

bors added a commit to rust-lang-ci/rust that referenced this pull request May 13, 2023
Remove useless `assume`s from `slice::iter(_mut)`

You were right in rust-lang#111395 (comment),
r? `@the8472`

LLVM already removes these assumes while optimizing, as can be seen in <https://rust.godbolt.org/z/KTfWKbdEM>.
RalfJung pushed a commit to RalfJung/miri that referenced this pull request May 13, 2023
Remove useless `assume`s from `slice::iter(_mut)`

You were right in rust-lang/rust#111395 (comment),
r? `@the8472`

LLVM already removes these assumes while optimizing, as can be seen in <https://rust.godbolt.org/z/KTfWKbdEM>.
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request Jul 18, 2023
Remove useless `assume`s from `slice::iter(_mut)`

You were right in rust-lang/rust#111395 (comment),
r? `@the8472`

LLVM already removes these assumes while optimizing, as can be seen in <https://rust.godbolt.org/z/KTfWKbdEM>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

None yet

6 participants