Skip to content

[Perf Experiment] Introduce NeverShortCircuit to see if it helps fold-via-try_fold #90886

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions library/core/src/iter/traits/double_ended.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ops::{ControlFlow, Try};
use crate::ops::{ControlFlow, NeverShortCircuit, Try};

/// An iterator able to yield elements from both ends.
///
Expand Down Expand Up @@ -292,16 +292,17 @@ pub trait DoubleEndedIterator: Iterator {
#[doc(alias = "foldr")]
#[inline]
#[stable(feature = "iter_rfold", since = "1.27.0")]
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
fn rfold<B, F>(mut self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let mut accum = init;
while let Some(x) = self.next_back() {
accum = f(accum, x);
#[inline]
fn call<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> NeverShortCircuit<B> {
move |accum, item| NeverShortCircuit(f(accum, item))
}
accum

self.try_rfold(init, call(f)).0
}

/// Searches for an element of an iterator from the back that satisfies a predicate.
Expand Down
13 changes: 7 additions & 6 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cmp::{self, Ordering};
use crate::ops::{ControlFlow, Try};
use crate::ops::{ControlFlow, NeverShortCircuit, Try};

use super::super::TrustedRandomAccessNoCoerce;
use super::super::{Chain, Cloned, Copied, Cycle, Enumerate, Filter, FilterMap, Fuse};
Expand Down Expand Up @@ -2161,16 +2161,17 @@ pub trait Iterator {
#[doc(alias = "inject", alias = "foldl")]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn fold<B, F>(mut self, init: B, mut f: F) -> B
fn fold<B, F>(mut self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let mut accum = init;
while let Some(x) = self.next() {
accum = f(accum, x);
#[inline]
fn call<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> NeverShortCircuit<B> {
move |accum, item| NeverShortCircuit(f(accum, item))
}
accum

self.try_fold(init, call(f)).0
}

/// Reduces the elements to a single one, by repeatedly applying a reducing
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ pub use self::range::{Bound, RangeBounds, RangeInclusive, RangeToInclusive};
#[unstable(feature = "try_trait_v2", issue = "84277")]
pub use self::try_trait::{FromResidual, Try};

pub(crate) use self::try_trait::NeverShortCircuit;

#[unstable(feature = "generator_trait", issue = "43122")]
pub use self::generator::{Generator, GeneratorState};

Expand Down
34 changes: 34 additions & 0 deletions library/core/src/ops/try_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,37 @@ pub trait FromResidual<R = <Self as Try>::Residual> {
#[unstable(feature = "try_trait_v2", issue = "84277")]
fn from_residual(residual: R) -> Self;
}

/// An adapter for implementing non-try methods via the `Try` implementation.
///
/// Conceptually the same as `Result<T, !>`, but requiring less work in trait
/// solving and inhabited-ness checking and such, by being an obvious newtype
/// and not having `From` bounds lying around.
///
/// Not currently planned to be exposed publicly, so just `pub(crate)`.
#[repr(transparent)]
pub(crate) struct NeverShortCircuit<T>(pub T);

pub(crate) enum NeverShortCircuitResidual {}

impl<T> Try for NeverShortCircuit<T> {
type Output = T;
type Residual = NeverShortCircuitResidual;

#[inline]
fn branch(self) -> ControlFlow<NeverShortCircuitResidual, T> {
ControlFlow::Continue(self.0)
}

#[inline]
fn from_output(x: T) -> Self {
NeverShortCircuit(x)
}
}

impl<T> FromResidual for NeverShortCircuit<T> {
#[inline]
fn from_residual(never: NeverShortCircuitResidual) -> Self {
match never {}
}
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3044.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | | });
note: associated function defined here
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
LL | fn fold<B, F>(mut self, init: B, mut f: F) -> B
LL | fn fold<B, F>(mut self, init: B, f: F) -> B
| ^^^^

error: aborting due to previous error
Expand Down