HashMap with customer Hasher in library API

while making a library we've used HashMap with the default Hasher. Now we're finding that the default Hasher is not always a good fit while using the library (the perf aspect). What would be a recommended way of using custom Hasher (fnv for instance)? Is it better to internalize it in library (e.g. use FnvHashMap instead of HashMap) or would it be better to surface hasher type (although it would feel rather weird seeing hasher type on containing structs..)? Or is there another way to handle this?

1 Like

I'd probably just make a local type alias which uses your hasher under the hood (which may be fnv or whatever) and use that everywhere. Then anyone in your library will import my_crate::HashMap instead of std::collections::HashMap and they'll gain the performance boost of using a faster Hasher without having to change anything else.

I feel like this is one of those times where you don't necessarily need to make it explicit that you're using a slightly different HashMap. The hasher being used is more of an implementation detail.

The only issue you might have is when you have both HashMaps from std and HashMaps from your library being used in the same area. Then if you're not careful you'll end up with compile errors saying HashMap<u32, String> isn't the same type as HashMap<u32, String>. That'd be kinda annoying for the half second it takes you to remember they use different hashers...

That's just my opinion though. Please correct me if you think there's a better way!