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

Improve codegen for enums with many cases #9122

Merged
merged 4 commits into from
Aug 12, 2024

Conversation

alexcrichton
Copy link
Member

This commit improves the compile time of generating bindings for enums with many cases in them (e.g. 1000+). This is done by optimizing for enums specifically rather than handling them generically like other variants which can reduce the amount of code going into rustc to O(1) instead of O(N) with the number of cases. This in turn can greatly reduce compile time.

The tradeoff made in this commit is that enums are now required to have #[repr(...)] annotations along with no Rust-level discriminants specified. This enables the use of a transmute to lift a discriminant into Rust with a simple bounds check. Previously this was one large match statement.

Closes #9081

This commit improves the compile time of generating bindings for enums
with many cases in them (e.g. 1000+). This is done by optimizing for
enums specifically rather than handling them generically like other
variants which can reduce the amount of code going into rustc to O(1)
instead of O(N) with the number of cases. This in turn can greatly
reduce compile time.

The tradeoff made in this commit is that enums are now required to have
`#[repr(...)]` annotations along with no Rust-level discriminants
specified. This enables the use of a `transmute` to lift a discriminant
into Rust with a simple bounds check. Previously this was one large
`match` statement.

Closes bytecodealliance#9081
@alexcrichton alexcrichton requested a review from a team as a code owner August 12, 2024 20:16
@alexcrichton alexcrichton requested review from pchickey and removed request for a team August 12, 2024 20:16
@alexcrichton alexcrichton added this pull request to the merge queue Aug 12, 2024
github-merge-queue bot pushed a commit that referenced this pull request Aug 12, 2024
* Improve codegen for enums with many cases

This commit improves the compile time of generating bindings for enums
with many cases in them (e.g. 1000+). This is done by optimizing for
enums specifically rather than handling them generically like other
variants which can reduce the amount of code going into rustc to O(1)
instead of O(N) with the number of cases. This in turn can greatly
reduce compile time.

The tradeoff made in this commit is that enums are now required to have
`#[repr(...)]` annotations along with no Rust-level discriminants
specified. This enables the use of a `transmute` to lift a discriminant
into Rust with a simple bounds check. Previously this was one large
`match` statement.

Closes #9081

* Fix some tests

* Add repr tag in fuzzing
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 12, 2024
@alexcrichton alexcrichton added this pull request to the merge queue Aug 12, 2024
Merged via the queue into bytecodealliance:main with commit fa41d13 Aug 12, 2024
37 checks passed
@alexcrichton alexcrichton deleted the better-enum-codegen branch August 12, 2024 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

large enums slow down compilation
2 participants