rust-lang / rust-lang/rust

exponential compile times for simple function compositions

Open
#119,503 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement I-compiletime T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Each additional statement let x = Apply(x, drop); surprisingly increases the compilation time by a factor of two:

struct Apply<X, F: Fn(X)>(X, F);

pub fn main() {
    let x = &();
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let x = Apply(x, drop);
    let _ = x;
}

The reason behind this exponential blowup is that the function drop has an "early-bound" generic parameter (drop::<_>), making the types of x grow exponentially with each level of nesting type X2 = Apply<X1, drop::<X1>>;

This should be fixed by removing the distinction between early- and late-bound function generics.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the exponential compile-time growth with the nested Apply and drop example, then investigate how Rust handles early- and late-bound function generics. Done means the composition no longer causes exponential type growth or compilation time, with a regression test or benchmark covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.