rust-lang / rust-lang/rust

Very bad performance of `cargo check` on generic code

Open
#125,994 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug I-compiletime T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm writing a parser crate. Yesterday as I fixed some code I ran into a serious performance issue with type checking in the rust compiler. The code linked below takes 50 seconds to run cargo check on my Macbook Pro (M2 Pro).

I've tried to make a minimal reproduction by reducing the code a lot and simplifying it a little. It's still a little big at 575 lines (sorry), but the problematic code is at the start. The function at the beginning is a test function from my crate that tests functionality of combining certain parsers. This function is responsible for the slowness. The two trait implementations following that are traits that implement the functionality that is tested by that function, that I wrote yesterday. The rest of the code is dependencies.

I have a function that tests the same thing with simpler kind of parser that lacks the State associated type (not included). That one compiles in a fraction of a second.

I've hosted the example code on the rust playground, but compile fails there (I assume it times out). To reproduce, make a new library crate, copy the code to lib.rs and run cargo check to see the problem. (The compiled code doesn't work because everything is stubbed out. The issue is the time to check/compile)

Here's the code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1c6bb86548e817430c28ce83d51bc65d

% cargo clean && cargo check
     Removed 14 files, 2.9MiB total
    Checking parse v0.0.2 (/Users/maia/Dev/parse)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 50.10s

% cargo clean && cargo +nightly check
     Removed 13 files, 3.4MiB total
    Checking parse v0.0.2 (/Users/maia/Dev/parse)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 52.29s

rustc --version --verbose:

% rustc --version --verbose
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: aarch64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2

% cargo +nightly rustc -- --version --verbose
   Compiling parse v0.0.2 (/Users/maia/Dev/parse)
rustc 1.80.0-nightly (c987ad527 2024-05-01)
binary: rustc
commit-hash: c987ad527540e8f1565f57c31204bde33f63df76
commit-date: 2024-05-01
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.4
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s

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

Copy the linked reproduction into a new library crate's lib.rs and run cargo clean && cargo check, comparing stable and nightly as shown. Start with the test function and the two following trait implementations; done means identifying the type-checking cause of the reported delay and demonstrating improved check time or a confirmed compiler diagnosis.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.