Open carljm opened 4 days ago
As reference, this is how rustc interns types:
The data type:
A reference to a type (bound to the TyCtxt
lifetime)
The definition of Interned
And this is used in TyCtxt
which is rustcs arena where it interns all data.
I do like splitting out the type internet from the type inference result. I think it makes sense to have them separate and also allows more effective reuse.
The part that's unclear to me and that you already mentioned on your write up is how to collect unused types. How can we prevent that this is an ever-growing arena?
We could try to track type references by using an AtomicUsize
(not an Arc
because that would heap allocate each type!) It would give us very fine-grained reclamation of types, at least if we use a slab like arena that supports reusing IDs. An other possibility could be to rely on Salsa by using one interner per file, package or some other granularity, and the interner would be retrieved by calling a salsa-query. This won't give us type level collection, but salsa would evict the entire type store if e.g. the file gets deleted (or all files in a package).
Initial exploration of what it might look like to intern types independent of the type inference result, so the granularity of type inference and the granularity of type interning don't have to be coupled.
The goal of this is to make it possible to add
definition_ty
queries, but this PR doesn't do that yet.This PR also doesn't work yet :) Just for illustration, I'm using
Arc::get_mut
in the places where we need to mutate theFileTypeStore
, but we can't actually do this because Salsa is still holding on to a reference to the Arc also. To make this work, we'll need to introduce interior mutability to theFileTypeStore
, which is a bit tricky along with wanting to return references to type structs stored inside it. The oldTypeStore
in the oldred_knot
crate shows one approach to this, but I'm not sure it's the best option.Having the
FileTypeStore
returned by a salsa query at all might require the Salsa "mutable arena" feature discussed in https://github.com/salsa-rs/salsa/pull/490#discussion_r1641000198 -- if so, for now we could instead just keep our our own dashmap ofVfsFile
toFileTypeStore
on the Db, but not actually in Salsa.Some nice things about interning types ourselves: a lot fewer lifetimes (no lifetime on
Type
enum anymore), and we can eliminate theTypingContext
indirection layer and just use Db in all APIs.