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

Specialize count for range iterators #112229

Merged
merged 1 commit into from
Sep 16, 2023

Conversation

clarfonthey
Copy link
Contributor

Since size_hint is already specialized, it feels apt to specialize count as well. Without any specialized version of ExactSizeIterator::len or Step::steps_between, this feels like a more reliable way of accessing this without having to rely on knowing that size_hint is correct.

In my case, this is particularly useful to access the steps_between implementation for char from the standard library without having to compute it manually.

I didn't think it was worth modifying the Step trait to add a version of steps_between that used native overflow checks since this is just doing one subtraction in most cases anyway, and so I decided to make the inclusive version use checked_add so it didn't have this lopsided overflow-checks-but-only-sometimes logic.

@rustbot
Copy link
Collaborator

rustbot commented Jun 3, 2023

r? @scottmcm

(rustbot has picked a reviewer for you, use r? to override)

@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 Jun 3, 2023
@scottmcm
Copy link
Member

scottmcm commented Jun 3, 2023

Since this is an observable change, I'm going to flip it over to
r? rust-lang/libs-api

For example, today

pub fn range_count_demo() -> usize { (0..u128::MAX).count() }

just returns usize::MAX, rather than panics

; playground::range_count_demo
; Function Attrs: mustprogress nofree norecurse nosync nounwind nonlazybind willreturn memory(none) uwtable
define noundef i64 @_ZN10playground16range_count_demo17h49ff333a76b29162E() unnamed_addr #0 personality ptr @rust_eh_personality {
start:
  ret i64 -1
}

https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=7202fd68c70d7dcd0f467880c2cbd48d

@rustbot rustbot assigned BurntSushi and unassigned scottmcm Jun 3, 2023
@scottmcm
Copy link
Member

scottmcm commented Jun 3, 2023

Hmm, it should be someone on the review rotation, so let's say
r? m-ou-se

@rustbot rustbot assigned m-ou-se and unassigned BurntSushi Jun 3, 2023
@BurntSushi
Copy link
Member

It looks like this change is explicitly allowed by the docs in Iterator::count:

The method does no guarding against overflows, so counting elements of an iterator with more than usize::MAX elements either produces the wrong result or panics. If debug assertions are enabled, a panic is guaranteed.

So this seems fine to me.

@rfcbot fcp merge

@clarfonthey
Copy link
Contributor Author

It looks like this change is explicitly allowed by the docs in Iterator::count:

I should have included this in the description too, but I was also taking this into consideration when deciding to implement this. The main justification for not using the default overflow behaviour (hence making observable changes) is because to do so would require augmenting the Step trait, and I didn't think it'd be worth it.

@BurntSushi
Copy link
Member

Trying again...

@rfcbot fcp merge

@ChrisDenton
Copy link
Member

You may also need to flip the label to libs-api

@scottmcm scottmcm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 5, 2023
@tgross35
Copy link
Contributor

tgross35 commented Aug 4, 2023

@BurntSushi just to move this thing - want to retry your fcp?

@BurntSushi
Copy link
Member

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Aug 4, 2023

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

No concerns currently listed.

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.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 4, 2023
@tgross35
Copy link
Contributor

tgross35 commented Aug 4, 2023

Third time's the charm :)

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Aug 7, 2023
@rfcbot
Copy link

rfcbot commented Aug 7, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Aug 7, 2023
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Aug 17, 2023
@rfcbot
Copy link

rfcbot commented Aug 17, 2023

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 24, 2023
@clarfonthey
Copy link
Contributor Author

Gentle nudge to @m-ou-se to merge when you have the chance, since the FCP has ended.

@dtolnay dtolnay assigned dtolnay and unassigned m-ou-se Sep 16, 2023
@dtolnay
Copy link
Member

dtolnay commented Sep 16, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Sep 16, 2023

📌 Commit 08aa6c9 has been approved by dtolnay

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 Sep 16, 2023
@bors
Copy link
Contributor

bors commented Sep 16, 2023

⌛ Testing commit 08aa6c9 with merge 4514fb9...

@bors
Copy link
Contributor

bors commented Sep 16, 2023

☀️ Test successful - checks-actions
Approved by: dtolnay
Pushing 4514fb9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 16, 2023
@bors bors merged commit 4514fb9 into rust-lang:master Sep 16, 2023
12 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 16, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4514fb9): 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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.7% [-2.7%, -2.7%] 1
All ❌✅ (primary) - - 0

Cycles

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

Binary size

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

Bootstrap: 633.405s -> 632.183s (-0.19%)
Artifact size: 318.16 MiB -> 318.14 MiB (-0.01%)

@clarfonthey clarfonthey deleted the range-iter-count branch September 16, 2023 20:31
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. finished-final-comment-period The final comment period is finished for this PR / Issue. 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-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.