apollo-rs icon indicating copy to clipboard operation
apollo-rs copied to clipboard

TypeDefinition should implement lightweight identity hashing rather than `derive(Hash)`

Open gmac opened this issue 2 years ago • 2 comments

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.

gmac avatar May 24 '23 16:05 gmac

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 🤔

lrlna avatar May 25 '23 09:05 lrlna

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.

goto-bus-stop avatar Aug 30 '23 09:08 goto-bus-stop