Skip to content

Conversation

@nnethercote
Copy link
Contributor

There are numerous unnecessary into_iter calls.

Also add a comment explaining why the code looks like this, because it's non-obvious at first glance.

r? @saethlin

There are numerous unnecessary `into_iter` calls.

Also add a comment explaining why the code looks like this, because it's
non-obvious at first glance.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 23, 2025
@saethlin
Copy link
Member

Huh. Neat.

I agree that the iterator chaining thing is subtle, I was very confused by the type the first time I worked on this module.

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Oct 23, 2025

📌 Commit 3d95159 has been approved by saethlin

It is now in the queue for this repository.

@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 Oct 23, 2025
bors added a commit that referenced this pull request Oct 24, 2025
Rollup of 5 pull requests

Successful merges:

 - #148016 (Revert constification of `Borrow` and `Deref for Cow` due to inference failure)
 - #148021 ([rustdoc] Simplify module rendering and HTML tags handling)
 - #148039 (Add myself to the review rotation)
 - #148042 (test(frontmatter): Cover spaces between infostring parts)
 - #148054 (Streamline iterator chaining when computing successors.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7141a0f into rust-lang:master Oct 24, 2025
11 checks passed
rust-timer added a commit that referenced this pull request Oct 24, 2025
Rollup merge of #148054 - nnethercote:chain, r=saethlin

Streamline iterator chaining when computing successors.

There are numerous unnecessary `into_iter` calls.

Also add a comment explaining why the code looks like this, because it's non-obvious at first glance.

r? `@saethlin`
@rustbot rustbot added this to the 1.92.0 milestone Oct 24, 2025
@Zalathar
Copy link
Contributor

Bors hasn't noticed that this was merged.

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 24, 2025
@Zalathar
Copy link
Contributor

This appears to have somehow caused a bunch of perf regressions: #148059 (comment)

Assuming the regression is real and not some weird noise, the only possible explanation I can think of is that this PR does slightly change the shape of the iterator, and maybe that's enough to cause measurably worse codegen despite giving equivalent results?

@nnethercote
Copy link
Contributor Author

Huh, weird about the perf. I'll take a look on Monday, and I will revert it if there's no simple solution.

@Zalathar
Copy link
Contributor

Huh, weird about the perf. I'll take a look on Monday, and I will revert it if there's no simple solution.

I was considering a revert myself, but after seeing this message I'll just keep my hands off and leave things up to you. 👍

Note that a version bump (#148077) will probably land before any fix/revert, so the fix/revert might want a beta backport.

@Zalathar
Copy link
Contributor

Here’s a perf run confirming that a revert would resolve the regression: #148086 (comment)

pub fn successors_for_value(&self, value: u128) -> Successors<'_> {
let target = self.target_for_value(value);
(&[]).into_iter().copied().chain(Some(target).into_iter().chain(None))
(&[]).into_iter().copied().chain(Some(target)).chain(None)
Copy link
Member

Choose a reason for hiding this comment

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

Note that the iterator type changed here, something like Chain<Copied<slice::Iter>, Chain<OptIter, OptIter>> to Chain<Chain<Copied<slice::Iter>, OptIter>, OptIter>.

This slice is empty anyway, but for the rest of the methods returning the same type, it probably hurts access to the slice items to go through two Chain layers.

pub fn successors_for_value(&self, value: u128) -> Successors<'_> {
let target = self.target_for_value(value);
(&[]).into_iter().copied().chain(Some(target).into_iter().chain(None))
(&[]).into_iter().copied().chain(Some(target)).chain(None)
Copy link
Member

Choose a reason for hiding this comment

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

Some of the original explicit into_iter() could still be avoided with std::iter::chain, plus iter() for slices:

Suggested change
(&[]).into_iter().copied().chain(Some(target)).chain(None)
[].iter().copied().chain(chain(Some(target), None))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler 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