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