Skip to content

Exponential slowdown in catching an overflow error. #91949

Closed

Description

@steffahn

This code is not expected to compile successfully:

fn main() {
    recurse(std::iter::empty());
}

struct Wrapped<T>(T);

struct IteratorOfWrapped<T, I: Iterator<Item = T>>(I);

impl<T, I: Iterator<Item = T>> Iterator for IteratorOfWrapped<T, I> {
    type Item = Wrapped<T>;
    fn next(&mut self) -> Option<Wrapped<T>> {
        self.0.next().map(Wrapped)
    }
}

fn recurse<T>(elements: T) -> Vec<char>
where
    T: Iterator<Item = ()>,
{
    recurse(IteratorOfWrapped(elements).map(|t| t.0))
}

This can't compile successfully because recurse<T> calls recurse<Map<IteratorOfWrapped<(), T>, [closure@<source>:22:51: 22:58]>> recursively, which then calls recurse<Map<IteratorOfWrapped<(), Map<IteratorOfWrapped<(), T>, [closure@<source>:22:51: 22:58]>>, [closure@<source>:22:51: 22:58]>>, and so on...

However up to stable 1.55, the code failed to compile quickly with the default recursion limit. On the compiler explorer, you can see it even handling #![recursion_limit = "800"] without timeout. However, go to 1.56 and it no longer reaches a compilation error in time with #![recursion_limit = "30"]. Still just as slow on nightly (rustc 1.59.0-nightly (6bda5b331 2021-12-12)).

@rustbot label: regression-from-stable-to-stable, perf-regression, I-compiletime, T-compiler, E-needs-bisection

Comes from: https://users.rust-lang.org/t/compiler-hanging/68804

Rough estimate on my machine: In the area of recursion_limit being 20-30, the time more-than doubles for each increase of the recursion limit by 2 or 3, so it really is exponential AFAICT.

Activity

  1. added
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    I-compiletimeIssue: Problems and improvements with respect to compile times.
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Dec 15, 2021
  2. parasyte commented on Dec 15, 2021

    @parasyte

    Possibly related to #89195?

  3. hkratz commented on Dec 15, 2021

    @hkratz
    Contributor

    Bisected to #85868, cc @Aaron1011

    bisected with cargo-bisect-rustc v0.6.1 searched nightlies: from nightly-2021-06-01 to nightly-2021-12-08 regressed nightly: nightly-2021-09-03 searched commit range: https://github.com/rust-lang/rust/compare/50171c310cd15e1b2d3723766ce64e2e4d6696fc...371f3cd regressed commit: https://github.com/rust-lang/rust/commit/371f3cd3fe523d0b398ed1db1620667c53ba7d02

    Host triple: x86_64-unknown-linux-gnu
    Reproduce with:

    cargo-bisect-rustc -t 3 --start 2021-06-01 --script ./test.sh

    test.sh:

    #!/bin/bash
    cargo build
    exit 0

    @rustbot label -E-needs-bisection

  4. removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Dec 15, 2021
  5. steffahn commented on Dec 15, 2021

    @steffahn
    MemberAuthor

    That PR seems incremental-compilation related. This leaves the question of whether the regression also happens without incremental compilation turned on, and whether or not it happens at the same point.

  6. wesleywiser commented on Dec 15, 2021

    @wesleywiser
    Member

    This is almost certainly the same issue as #89195 then.

  7. steffahn commented on Dec 15, 2021

    @steffahn
    MemberAuthor

    Hard to know for certain until #89195 has a mvce. Also note that, as far as I understand, #89195 is observable with cargo check whereas this bug only appears with cargo build. (I somehow forgot to mention that...)

  8. apiraino commented on Dec 23, 2021

    @apiraino
    Contributor

    Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

    @rustbot label -I-prioritize +P-high

  9. added
    P-highHigh priority
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Dec 23, 2021
  10. wesleywiser commented on Dec 27, 2021

    @wesleywiser
    Member

    Retested since #90423 landed on nightly:

    • rustc 1.55.0 (c8dfcfe 2021-09-06): 13.1 seconds
    • rustc 1.59.0-nightly (f8abed9 2021-12-26): 10.6 seconds
  11. steffahn commented on Dec 27, 2021

    @steffahn
    MemberAuthor

    Seems to have fixed the problem, indeed. @rustbot label E-needs-test.

  12. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Dec 27, 2021
  13. added a commit that references this issue on May 23, 2022
  14. added a commit that references this issue on May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-compiletimeIssue: Problems and improvements with respect to compile times.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.perf-regressionPerformance regression.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions