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

Prevent unnecessary trait resolution #140

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

jacob-hughes
Copy link
Collaborator

Trait resolution (and monomorphization, which can be thought of as a variant of this) is used throughout the FSA algorithm (e.g. checking if T implements Send / Sync, resolving calls to trait methods to their concrete implementation, etc). Unfortunately this can be slow, particularly during a MIR pass, where functions must be monomorphized on demand. This slowdown becomes particularly apparent when trying to extend FSA to look into other function calls which are reachable via drop. On large codebases like rustc (e.g. when bootstrapping) or Alacritty, this can lead to over a 2x slowdown in compile time.

The good news is that it's fairly easy to fix this. This commit does this using two simple tricks:

  1. We now make use of rustc's internal type-query caching by implementing our "does T implement Trait" methods directly on the Ty.
  2. We prevent monomorphizing trait methods unless we really need to by adding more trivial checks which let us bail out on the fast path.

These two changes are enough to put us back in the range of about 1-2% compile-time slowdown for FSA for Gc heavy programs, and almost indistinguishable for programs which don't use Gc.

Trait resolution (and monomorphization, which can be thought of as a
variant of this) is used throughout the FSA algorithm (e.g. checking if
`T` implements `Send` / `Sync`, resolving calls to trait methods to
their concrete implementation, etc). Unfortunately this can be slow,
particularly during a MIR pass, where functions must be monomorphized on
demand. This slowdown becomes particularly apparent when trying to
extend FSA to look into other function calls which are reachable via
drop. On large codebases like rustc (e.g. when bootstrapping) or
Alacritty, this can lead to over a 2x slowdown in compile time.

The good news is that it's fairly easy to fix this. This commit does
this using two simple tricks:

    1. We now make use of rustc's internal type-query caching by
       implementing our "does T implement Trait" methods directly on the
       `Ty`.
    2. We prevent monomorphizing trait methods unless we really need to
       by adding more trivial checks which let us bail out on the fast
       path.

These two changes are enough to put us back in the range of about 1-2%
compile-time slowdown for FSA for `Gc` heavy programs, and almost
indistinguishable for programs which don't use `Gc`.
@ltratt ltratt added this pull request to the merge queue Nov 10, 2024
Merged via the queue into softdevteam:master with commit be32cbb Nov 10, 2024
2 checks passed
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.

2 participants