-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Range* should overrride more methods of Iterator #39975
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I don't see the use case for nth, last, max, min. Sum and product might be good to have. I'll try to implement them. |
@crypto-universe The use case is in generic code. Sometimes a range is used in generic code that use these methods, it they are implemented efficiently it is better. |
All ranges that can provide iterator implementations do so today; this excludes |
@Mark-Simulacrum This issue is about overriding methods of the iterator trait. For example, max can be implemented efficient for Range, but it is not, it uses the default implementation. |
Oh, I misinterpreted then. Reopening. |
This is more important now that #41439 deperecated |
#43077 does |
Add iterator method specialisations to Range* Add specialised implementations of `max` for `Range`, and `last`, `min` and `max` for `RangeInclusive`, all of which lead to significant advantages in the generated assembly on x86. Note that adding specialisations of `min` and `last` for `Range` led to no benefit, and adding `sum` for `Range` and `RangeInclusive` led to type inference issues (though this is possibly still worthwhile considering the performance gain). This addresses some of the concerns in #39975.
Add iterator method specialisations to Range* Add specialised implementations of `max` for `Range`, and `last`, `min` and `max` for `RangeInclusive`, all of which lead to significant advantages in the generated assembly on x86. Note that adding specialisations of `min` and `last` for `Range` led to no benefit, and adding `sum` for `Range` and `RangeInclusive` led to type inference issues (though this is possibly still worthwhile considering the performance gain). This addresses some of the concerns in #39975.
Add iterator method specialisations to Range* Add specialised implementations of `max` for `Range`, and `last`, `min` and `max` for `RangeInclusive`, all of which lead to significant advantages in the generated assembly on x86. Note that adding specialisations of `min` and `last` for `Range` led to no benefit, and adding `sum` for `Range` and `RangeInclusive` led to type inference issues (though this is possibly still worthwhile considering the performance gain). This addresses some of the concerns in #39975.
Add iterator method specialisations to Range* Add specialised implementations of `max` for `Range`, and `last`, `min` and `max` for `RangeInclusive`, all of which lead to significant advantages in the generated assembly on x86. Note that adding specialisations of `min` and `last` for `Range` led to no benefit, and adding `sum` for `Range` and `RangeInclusive` led to type inference issues (though this is possibly still worthwhile considering the performance gain). This addresses some of the concerns in #39975.
#47180 adds Edit: |
For anyone implementing something here, consider overriding Edit: |
|
Is there any use to calling |
Why, yes, I've used triangle numbers 3 times on Project Euler. For |
Just to mention: I ran into type inference issues when I tried specialising for |
Note that LLVM already optimizes Range::sum to a closed-form solution, so there really isn't a need to specialize: https://play.rust-lang.org/?gist=631cf2e39b07d8d8e4f80013c8c235aa&mode=release You can tell by the lack of back-edges, and the 33-bit multiply and divide-by-two: %10 = mul i33 %6, %9
%11 = lshr i33 %10, 1 |
One possible way to implement pub fn range_count<A: Step>(range: std::ops::Range<A>) -> usize {
if let Some(steps) = Step::steps_between(&range.start, &range.end) {
steps
} else {
range.fold(0, |x, _| x + 1)
}
} With inlining, this works out nicely for integer types. And consider that Optimally, the |
Like nth, last, count, max, min,... maybe even sum and product (using a direct formula).
The text was updated successfully, but these errors were encountered: