This repository has been archived by the owner. It is now read-only.

Trait system rewrite #8

aturon opened this Issue Jan 31, 2017 · 8 comments


None yet
6 participants

aturon commented Jan 31, 2017

Point of contact

@nikomatsakis @aturon


The current trait system implementation is written in a "direct" style, where we perform search directly on impls and so on. While we've taken pains to keep this implementation clean, it's still quite complicated and ad hoc, and has a number of pitfalls:

  • Performance is limited by the amount of caching we can perform. Caching currently takes into account irrelevant information, meaning that cache misses are more common than they should be. Changing this in the current setup is not easy.

  • There are a lot of limitations around associated types and higher-ranked trait bounds, which again are hard to lift within the current setup.

  • There are a number of extensions we'd like to make to the trait system, but doing so would require substantial refactorings.

We're now exploring a re-write based on logic programming, where we understand traits and impls as a logic program against which we can make queries. This should allow for a much more principled implementation, better caching, and easier extension to new features (like associated type constructors).


The Chalk project is a standalone trait system/prolog engine, which serves as a prototype and specification for what we want in rustc itself. It's quite far along at this point, but is still missing a few crucial pieces (like specialization).

We're also doing some refactoring in rustc, ahead of the work to port Chalk's ideas into rustc proper.

In the long run, we'd like to have the ability to run Chalk side-by-side with rustc, as an addon to the compiler's test suite.

Blog posts:


This comment has been minimized.

mrkgnao commented Feb 7, 2017

Something I noticed: @nikomatsakis' proposed "stages of typing" reminds me of how Haskell is compiled, especially in how they propose RFCs should be evaluated. The GHC section of The Architecture of Open Source Applications mentions something similar with respect to the GHC Core intermediate language that might be interesting to look at.


This comment has been minimized.


aturon commented Apr 13, 2017

Note that ongoing work is happening in the Chalk repository. This is a standalone trait system/prolog engine, which we hope will double as an executable specification/test system for rustc. There are a few more major pieces to land here (e.g., specialization), and then we'll focus on porting the ideas to rustc proper.


This comment has been minimized.


aturon commented Apr 24, 2017

Added links to blog posts.


This comment has been minimized.

JoeyAcc commented Jun 26, 2017

Just out of curiosity:
Roughly how does Chalk perform, compared to the current Trait implementation?
I suppose what I'm really asking is: Can we, the rust community, expect any kind of significant performance delta in either direction once the switch is made to Chalk?


This comment has been minimized.


steveklabnik commented Jun 26, 2017

My understanding is that Chalk is mostly about correctness and ease of adding things in the future, but also is supposed to perform better.

However, you gotta think about this in an Amdahls' law sense: figuring out what traits apply is rarely a significant part of compilation time, so even speeding it up a lot probably won't speed up compile times overall a ton.

I'm not on the compiler team though so I may be wrong about any part of this.


This comment has been minimized.


jonathandturner commented Jun 26, 2017

@steveklabnik - iirc, @nikomatsakis mentioned at one point that the trait stuff is threaded through the typechecking so improvements in it may have some wall-clock improvement in compile time, though not sure what percent improvement it could reasonably give. Maybe he could speak to it?


This comment has been minimized.


aturon commented Jul 3, 2017

figuring out what traits apply is rarely a significant part of compilation time

That's incorrect; it can be substantial. Chalk should help in part by providing a much better caching strategy.


This comment has been minimized.

Kixunil commented Sep 13, 2017

Is this the correct issue to track if I'm interested in status of wiring Chalk into the compiler or is there something better?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.