Skip to content
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

Compiling #[derive(Debug)] enum + dbg! is quadratic with enum variants #133945

Open
theemathas opened this issue Dec 6, 2024 · 1 comment
Open
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. I-compiletime Issue: Problems and improvements with respect to compile times. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

I tried compiling this code: lib.rs.zip

The code above was generated with the following python script:

print(
    """#[derive(Debug)]
pub enum Sprites {"""
)
for i in range(10000):
    print(f"    Sprite{i},")
print(
    """}
pub fn foo() {
   println!("{:?}", Sprites::Sprite1);
}"""
)

I then compiled the code with cargo build --release. It takes 21.00s to compile on my machine, which is rather slow. It would be nice if it compiled faster.

Note that reducing the number of variants from 10000 to 1000 makes the compilation take only 0.20s to compile on my machine. I suspect that something in the Debug trait impl is taking quadratic time, but only if you actually try to debug-print something.

This issue was discovered by tongke on the rust community discord.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1
@theemathas theemathas added the C-bug Category: This is a bug. label Dec 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 6, 2024
@workingjubilee workingjubilee added I-compiletime Issue: Problems and improvements with respect to compile times. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 6, 2024
@workingjubilee workingjubilee changed the title Debug-printing an enum with very many variants takes a long time to compile Compiling #[derive(Debug)] enum + dbg! is quadratic with enum variants Dec 6, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 6, 2024
@randyli
Copy link

randyli commented Dec 20, 2024

I runned the compiling with -Z self-profile, and the result like this:

Item Self time % of total time
mir_borrowck 2.03s 58.174
check_match 340.29ms 9.772
run_linker 256.34ms 7.361

Is it a good idea to assign lable A-borrow-checker ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. I-compiletime Issue: Problems and improvements with respect to compile times. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants