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 1 year 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