apollo-rs
apollo-rs copied to clipboard
TypeDefinition should implement lightweight identity hashing rather than `derive(Hash)`
The TypeDefinition enum derives the Hash trait, which is extremely slow for large types. For example, this simple wrapper struct that just hashes typename provides a dramatic improvement when inserting type definitions into hashmaps and hashsets:
#[derive(PartialEq, Eq)]
struct TypeKey {
ty: TypeDefinition,
}
impl Hash for TypeKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ty.name().hash(state);
}
}
Compiler should do this directly and implement a custom Hash trait for TypeDefinition that uses minimally unique criteria. I suspect that the derived hash implementation gets into fields, making larger types hash increasingly slowly.
we've been thinking about this a bunch too; especially about the fact that Hash accounts for node location information which interferes with memoisation for internal db queries 🤔
Simon has been working on a new AST+HIR to support modifying and creating new documents, which uses shared pointers to nodes that cache the hash. It's a little different from the suggestion in this issue, as the hash is initially still quite expensive, but it won't add up over time.