Skip to content

Specialize Iterator::eq{_by} for TrustedLen iterators #137122

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yotamofek
Copy link
Contributor

I'm sure I got some stuff wrong here, but opening this to get feedback and make sure it's a viable idea at all.

Motivation

I had a piece of code that open-coded Iterator::eq, something like:

if current.len() != other.len()
    || current.iter().zip(other.iter()).any(|(a, b)| a != b) { ... }

... where both current and other are slices of the same type.
Changing the code to use current.iter().eq(other) made it a lot slower, since it wasn't checking the length of the two slices beforehand anymore, which in this instance made a big difference in perf. So I thought I'd see if I can improve Iterator::eq.

Questions

  1. I can't specialize for ExactSizeIterator, I think it's a limitation of min_specialization but not sure exactly why. Is specializing for TrustedLen good enough?
  2. Should I make a codegen test for this? If so, then how? (I manually checked the assembly to make sure it works as expected)
  3. Where should I put SpecIterCompare?
  4. Can I get a perf run for this, please? I think the compiler uses this in a few places, so it might have an affect.

@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 2025

r? @Amanieu

rustbot has assigned @Amanieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Feb 16, 2025
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#21 exporting to docker image format
#21 sending tarball 27.8s done
#21 DONE 42.0s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
  Downloaded boml v0.3.1
   Compiling boml v0.3.1
   Compiling y v0.1.0 (/checkout/compiler/rustc_codegen_gcc/build_system)
    Finished `release` profile [optimized] target(s) in 4.02s
     Running `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-codegen/x86_64-unknown-linux-gnu/release/y test --use-system-gcc --use-backend gcc --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/cg_gcc --release --mini-tests --std-tests`
Using system GCC
warning: target feature `x87` must be enabled to ensure that the ABI of the current target can be implemented correctly
  |
  = note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
---
failures:

---- iter::test_multi_iter stdout ----

thread 'iter::test_multi_iter' panicked at library/coretests/tests/iter/mod.rs:33:5:
assertion failed: xs.iter().lt(xs.iter().skip(2))
---- iter::traits::iterator::test_cmp_by stdout ----


thread 'iter::traits::iterator::test_cmp_by' panicked at library/coretests/tests/iter/traits/iterator.rs:82:5:
  left: Greater
 right: Less

---- iter::traits::iterator::test_partial_cmp_by stdout ----
---- iter::traits::iterator::test_partial_cmp_by stdout ----

thread 'iter::traits::iterator::test_partial_cmp_by' panicked at library/coretests/tests/iter/traits/iterator.rs:98:5:
  left: Some(Greater)
 right: Some(Less)


@yotamofek

This comment was marked as resolved.

@yotamofek yotamofek closed this Feb 16, 2025
@yotamofek
Copy link
Contributor Author

Ok, sorry for the noise, but I got confused and then re-confused and then finally (hopefully) un-confused. This optimization is ok on Iterator::eq{_by}, but not on cmp and friends.
Fixed.

@yotamofek yotamofek reopened this Feb 16, 2025
@yotamofek yotamofek changed the title Specialize Iterator::{eq|cmp|partial_cmp}_by for TrustedLen iterators Specialize Iterator::eq{_by} for TrustedLen iterators Feb 16, 2025
@lqd
Copy link
Member

lqd commented Feb 17, 2025

@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 Feb 17, 2025
@bors
Copy link
Collaborator

bors commented Feb 17, 2025

⌛ Trying commit d9f58d4 with merge 61b77a2...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2025
… r=<try>

Specialize `Iterator::eq{_by}` for `TrustedLen` iterators

I'm sure I got some stuff wrong here, but opening this to get feedback and make sure it's a viable idea at all.

### Motivation
I had a piece of code that open-coded `Iterator::eq`, something like:
```rust
if current.len() != other.len()
    || current.iter().zip(other.iter()).any(|(a, b)| a != b) { ... }
```
... where both `current` and `other` are slices of the same type.
Changing the code to use `current.iter().eq(other)` made it a lot slower, since it wasn't checking the length of the two slices beforehand anymore, which in this instance made a big difference in perf. So I thought I'd see if I can improve `Iterator::eq`.

### Questions
1. I can't specialize for `ExactSizeIterator`, I think it's a limitation of `min_specialization` but not sure exactly why. Is specializing for `TrustedLen` good enough?
2. Should I make a codegen test for this? If so, then how? (I manually checked the assembly to make sure it works as expected)
3. Where should I put `SpecIterCompare`?
4. Can I get a perf run for this, please? I think the compiler uses this in a few places, so it might have an affect.
@bors
Copy link
Collaborator

bors commented Feb 17, 2025

☀️ Try build successful - checks-actions
Build commit: 61b77a2 (61b77a2c3cd2d5520be1f24e0ebf0fc672982a72)

@rust-timer

This comment has been minimized.

where
F: FnMut(Self::Item, <B as Iterator>::Item) -> ControlFlow<T>,
{
if let (_, Some(a)) = self.size_hint()
Copy link
Member

Choose a reason for hiding this comment

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

pondering: What if, instead of specialization, iter_compare just always checked if the size_hints? That way it can work even for things that are neither exact nor trusted.

Something with a size hint of (2, Some(10)) can't possibly be equal to one with (14, None), for example.

And for ESIs, which almost always return let len = self.len(); (len, Some(len)), the compiler can probably optimize the two checks into one. And for the default (0, None) hint the compiler will optimize away the check because obviously the other one can't be shorter than zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thing is - I'm not sure it would be acceptable if Iterator::eq started returning wrong results due to "wrong" size hints. It won't be UB, but I don't think any other iterator combinators can return wrong results because of a buggy size_hint(). IMHO even ESI's guarantee (which is less than TrustedLen's) might not be strong enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be equivalent to, say, for_each not being run for the last elements of an iterator if its upper bound hint is incorrectly smaller than the number of elements it can yield.

Copy link
Member

Choose a reason for hiding this comment

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

An incorrect size_hint is absolutely enough to trigger garbage-in-garbage-out. The default size_hint of (0, None) is always correct, so this would only be an issue if someone explicitly overrides it incorrectly, and that's not allowed.

Of course it's not allowed to be UB if someone implements size_hint wrong, but implementing size_hint wrong is no different from implementing fold wrong, for example: it's 100% allowed to make other things misbehave.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a really good point. Hadn't looked at it that way.
But I do think that as a user, I would be much more surprised if an incorrect size_hint implementation caused anything other than wrong estimations for with_capacity or something, rather than that an incorrect fold will just blow everything up. Maybe the word "hint" makes it sound less consequential. 🤷

Anyways, @the8472 's concern about eliding side effects might be a deal breaker, so I'll wait for the libs team decision on that before trying out different approaches for implementations.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, part of the problem is that, today, the size_hint is really only used by collect, which doesn't even use the upper part of it, just the bottom part.

I often wish there was instead just a suggested_reserve() -> usize or something that was more obviously both 1) just for the collect case, and 2) explicitly documented as allowed to be garbage.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (61b77a2): comparison URL.

Overall result: no relevant changes - 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 benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 7.7%)

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)
7.7% [7.2%, 8.1%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 7.7% [7.2%, 8.1%] 2

Cycles

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

Binary size

Results (primary 0.1%, secondary 0.0%)

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.1% [0.0%, 0.2%] 5
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.0%, 0.2%] 5

Bootstrap: 774.698s -> 773.335s (-0.18%)
Artifact size: 362.37 MiB -> 362.39 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 17, 2025
@Amanieu
Copy link
Member

Amanieu commented Feb 18, 2025

r? @the8472

@rustbot rustbot assigned the8472 and unassigned Amanieu Feb 18, 2025
@the8472
Copy link
Member

the8472 commented Feb 18, 2025

Personally I think this would be great from a performance perspective but it has the consequence of eliding side-effects of the iterators. Last time I proposed a change that would elide some sideeffects of iterators it got NACK'd by the team.

Nominating for discussion since perhaps this case is different since the eq impl involves some non-trivial variability in the amount of items that would be consumed before iteration stops.

@the8472 the8472 added the I-libs-nominated Nominated for discussion during a libs team meeting. label Feb 18, 2025
@yotamofek
Copy link
Contributor Author

Personally I think this would be great from a performance perspective but it has the consequence of eliding side-effects of the iterators.

Thanks, that's a good point, hadn't occurred to me. I guess one way to get around that is to add a MAY_HAVE_SIDE_EFFECT associated const to TrustedLen, but that sounds like a pretty huge change just for this PR, no?
Anyways, waiting to hear what the team thinks about this 😁

@the8472
Copy link
Member

the8472 commented Feb 19, 2025

We discussed this during today's libs meeting, albeit with limited attendance.
Those present didn't object to either approach (TrustedLen or sized_hint), but this PR we want to go with the TrustedLen approach since that should be the least controversial one.
It could be expanded to size_hint in a followup PR and be discussed separately there, if there's appetite for that.

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Feb 19, 2025

Team member @the8472 has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@cuviper
Copy link
Member

cuviper commented Feb 19, 2025

Let's formally register that...

@rfcbot concern side-effects

Can we get a crater-test run to gauge this?

@Amanieu Amanieu removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Feb 26, 2025
@Amanieu
Copy link
Member

Amanieu commented Feb 26, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 26, 2025
… r=<try>

Specialize `Iterator::eq{_by}` for `TrustedLen` iterators

I'm sure I got some stuff wrong here, but opening this to get feedback and make sure it's a viable idea at all.

### Motivation
I had a piece of code that open-coded `Iterator::eq`, something like:
```rust
if current.len() != other.len()
    || current.iter().zip(other.iter()).any(|(a, b)| a != b) { ... }
```
... where both `current` and `other` are slices of the same type.
Changing the code to use `current.iter().eq(other)` made it a lot slower, since it wasn't checking the length of the two slices beforehand anymore, which in this instance made a big difference in perf. So I thought I'd see if I can improve `Iterator::eq`.

### Questions
1. I can't specialize for `ExactSizeIterator`, I think it's a limitation of `min_specialization` but not sure exactly why. Is specializing for `TrustedLen` good enough?
2. Should I make a codegen test for this? If so, then how? (I manually checked the assembly to make sure it works as expected)
3. Where should I put `SpecIterCompare`?
4. Can I get a perf run for this, please? I think the compiler uses this in a few places, so it might have an affect.
@bors
Copy link
Collaborator

bors commented Feb 26, 2025

⌛ Trying commit d9f58d4 with merge 7db5569...

@bors
Copy link
Collaborator

bors commented Feb 26, 2025

☀️ Try build successful - checks-actions
Build commit: 7db5569 (7db556953920f7bb7fd6e6abcc47f8878a5c90de)

@Amanieu
Copy link
Member

Amanieu commented Feb 26, 2025

@craterbot run mode=build-and-test

@craterbot
Copy link
Collaborator

👌 Experiment pr-137122 created and queued.
🤖 Automatically detected try build 7db5569
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 26, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-137122 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-137122 is completed!
📊 178 regressed and 108 fixed (589170 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Feb 28, 2025
@yotamofek
Copy link
Contributor Author

yotamofek commented Mar 1, 2025

I'm not sure I know how to parse crater's results, but it does seem like a lot of the errors are due to lack of disk space, a few look like flakey tests, and I couldn't find any errors that look related to this PR (haven't found a crate that actually calls Iterator::eq, though I haven't gone through all of them yet).

Does anyone have any pointers?

@yotamofek
Copy link
Contributor Author

yotamofek commented Apr 10, 2025

I'm not sure I know how to parse crater's results, but it does seem like a lot of the errors are due to lack of disk space, a few look like flakey tests, and I couldn't find any errors that look related to this PR (haven't found a crate that actually calls Iterator::eq, though I haven't gone through all of them yet).

Does anyone have any pointers?

bump

still looking for help with deciphering the crater results here

@m-ou-se
Copy link
Member

m-ou-se commented Apr 11, 2025

it does seem like a lot of the errors are due to lack of disk space, a few look like flakey tests, and I couldn't find any errors that look related to this PR (haven't found a crate that actually calls Iterator::eq, though I haven't gone through all of them yet).

Yeah that is normal and expected.

If you went through all the failed 'root results' and didn't find any failure relating to this change: congrats, you have a clean crater run. :)

yotamofek added a commit to yotamofek/crater that referenced this pull request Apr 12, 2025
Found in this crater run: rust-lang/rust#137122

Certain randomness causes tests to fail
@yotamofek
Copy link
Contributor Author

I've been slowly going through all the failures crater found, and so far have not found a single true-positive one that broke because of this PR.
But.. I've looked at other crater run results, and it seems like this one has a order-of-magnitude more of these spurious failures (esp. the disk full ones).
So I would love another crater run here, which will hopefully reduce the number of failures to a number that I can actually thoroughly go through. 🙏

@the8472
Copy link
Member

the8472 commented Jun 26, 2025

@bors2 try

@rust-bors
Copy link

rust-bors bot commented Jun 26, 2025

⌛ Trying commit d9f58d4 with merge 0936b3d

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 26, 2025
Specialize `Iterator::eq{_by}` for `TrustedLen` iterators

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->
I'm sure I got some stuff wrong here, but opening this to get feedback and make sure it's a viable idea at all.

### Motivation
I had a piece of code that open-coded `Iterator::eq`, something like:
```rust
if current.len() != other.len()
    || current.iter().zip(other.iter()).any(|(a, b)| a != b) { ... }
```
... where both `current` and `other` are slices of the same type.
Changing the code to use `current.iter().eq(other)` made it a lot slower, since it wasn't checking the length of the two slices beforehand anymore, which in this instance made a big difference in perf. So I thought I'd see if I can improve `Iterator::eq`.

### Questions
1. I can't specialize for `ExactSizeIterator`, I think it's a limitation of `min_specialization` but not sure exactly why. Is specializing for `TrustedLen` good enough?
2. Should I make a codegen test for this? If so, then how? (I manually checked the assembly to make sure it works as expected)
3. Where should I put `SpecIterCompare`?
4. Can I get a perf run for this, please? I think the compiler uses this in a few places, so it might have an affect.
@the8472
Copy link
Member

the8472 commented Jun 26, 2025

crater gained a new category "prepare-fail" which should now filter out some of those failures.

@rust-bors
Copy link

rust-bors bot commented Jun 26, 2025

☀️ Try build successful (CI)
Build commit: 0936b3d (0936b3db0ae648ba2d5b60eb08cf4faf2ddab0c6, parent: b03b3a7ec92682be2917540b679478d41c95a30c)

@the8472
Copy link
Member

the8472 commented Jun 27, 2025

@craterbot run mode=build-and-test

@craterbot
Copy link
Collaborator

👌 Experiment pr-137122-1 created and queued.
🤖 Automatically detected try build 0936b3d
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 27, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-137122-1 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-crater Status: Waiting on a crater run to be completed. 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.