r/rust • • 2d ago

Optimizing rustc's next trail solver

https://donsz.nl/blog/optimizing-rustcs-next-trait-solver

The next rust trait solver, a large subsystem of the rust compiler that we've been trying to replace, will finally land in rust 1.102.0. In the last stages, before stabilizing it, its performance was a major blocker. But what is "a trait solver"? And why is its performance a problem? My goal for this blog post is to answer those questions in a way where you don't need any prior knowledge. I think discussions of the trait solver can quickly become quite involved, and daunting. So here, we'll start from the very basics!

This is a writeup of my eurorust 2026 talk of the same name, that I gave on October 15, 2026

203 Upvotes

17 comments sorted by

30

u/janameow 2d ago

Note, this was previously posted here as just the video by eurorust. See https://www.reddit.com/r/rust/comments/1y4adhs/optimizing_rustcs_next_trait_solver_jana/

16

u/matthieum [he/him] 2d ago

Thanks for the write-up, I much prefer reading an article to watching a video. Mostly because I'm so much faster at reading.

86

u/noop_noob 2d ago

We love solving trails.

9

u/admalledd 2d ago

I mean, is the new solver not solving a trail of questions sign posted by traits and bounds? :P

OP, thanks for the talk and write up! Excited for all the new features this'll unlock (Move Trait maybe!)

9

u/janameow 2d ago

Glad you liked it! Move trait pretty much has my full attention, together with Forget now that the new solver is merging :)

1

u/JuicyGleamy 2d ago

bet you say that to all the solvers around here

1

u/Demiu 2d ago

Shadow hiker money gang

2

u/kevinbaiv 2d ago

The opaque type retry path is what I want to understand better: when an answer was recorded assuming opaque types don't matter and the retry says otherwise, do you evict the entry or keep both keyed by the recorded state? And does the hit rate in the chart count those speculative answers as hits? Either way, 200 billion cycles down to 7 billion on wg-grammar is the kind of number I want to see in more optimization writeups.

2

u/janameow 2d ago

Hi there! Good question. So technically even when we use opaque type tracking, the list is still part of the key. We just set it to be an empty list. That means that in the rerun, we un-empty it and we get a different cache key. Now because we keep the cache entry with the empty list around too, a new query will empty the list , find a cache hit with no opaques but with tracking information (so it's not redoing work getting that tracking). Then if it decides it needs to rerun it adds back the opaques and could totally get a cache hit again if we've tried with those exact opaques before. 

Does that make sense?

2

u/janameow 2d ago

Of note is also that sometimes we run multipel dependent queries without their opaques like this. We can only un-empty the list once we get back up to the root where we emptied the list. To make sure we don't spend too much time running giant query trees we have a bailing mechanism built in to the tracking. Once a query and it's dependencies try to access opaques more than 3 times (or when they do so in very specific ways) we assume that it's unlikely for opaques not to influence the outcome in any way, and we bail out of the attempts (recording in each dependent result that it's likely a rerun is necessary) and rerun without knowing the original result, since it's unlikely to be correct.

Now there's an even more subtle thing going on. Sometimes assuming that opaques are empty (in a specific path related to coroutines) leads to query cycles. So, in that case we are forced to rerun with opaques even if the answer would've been correct with the stripped environment, because the compiler would crash if it encountered the cycle and we want to avoid that at all costs

2

u/janameow 2d ago

I enjoyed your question a lot! I added it to the main blog post, if you don't mind :)

5

u/popzxc 2d ago

Loved the talk, and good to see it in text format too!

3

u/candyFawny 2d ago

yeah me too!

4

u/ebkalderon amethyst · renderdoc-rs · tower-lsp · cargo2nix 2d ago

Fantastic talk and writeup! I watched the Eurorust recording yesterday and enjoyed it very much. One question I still have for the OP: would you mind elaborating further on the Q&A question about negative trait bounds and why you believe that feature will never be stabilized? I'd love to understand the fundamental limitations a bit better. Thanks again for sharing.

3

u/scook0 2d ago

I’ve encountered some of these trait-solver details (like canonicalization) while cleaning up the rustc query system, but I never had much context for what they were actually doing, so it’s neat to see more elaboration in this post.

2

u/spin81 2d ago

I don't have time to read the post now as I'm supposed to be working, but will check it out as it's tailored just for me in terms of previous knowledge!

I can see why this would be an important way to improve rustc performance, I bet in big projects there's a LOT of trait solving the compiler has to do and that it can get pretty complex in terms of the Cartesian products of possibilities there can be. Wanted to thank you for sharing!

Unrelatedly I am thinking of doing Advent of Code in a language I don't yet know and I saw Prolog mentioned in the post. Can anyone say if Prolog is a fun one to do AoC in for a Prolog noob?