Skip to content

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

Open
@theemathas

Description

@theemathas

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.I-compiletimeIssue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions