strum icon indicating copy to clipboard operation
strum copied to clipboard

Please document use_phf

Open tgrushka opened this issue 7 months ago • 1 comments

What does #[strum(use_phf)] do on a #[derive(strum::EnumString)] enum? The only documentation is:

/// If you have a large enum, you may want to consider using the `use_phf` attribute here. It leverages
/// perfect hash functions to parse much quicker than a standard `match`. (MSRV 1.46)

That's it. What does this do? What does it mean, for those of us without a PhD in computer science? It generates a hash per variant for the string representation or something? Which string does it use, the #[strum(to_string = "...")] one? I'm just guessing here, no idea, and #220 refers to #218 which refers to the phf crate. At this point, I have given up, there are too many rabbit holes to figure out what this does, how it works, what code it generates, how it impacts my performance / memory usage, etc.

Someone took hours to implement this feature. Would be worth 10 minutes to write up a paragraph describing what it does, and when and how to use it.

Thank you.

tgrushka avatar May 10 '25 20:05 tgrushka

I guess it's just normal Linear Search vs Hash Map lookup tradeoff.

For small Enums it's faster to just linearly search for a variant using match, since e.g. they fit into a cache line. But for bigger it's probably beneficial to store them in a hashmap and look up directly at the cost of storing this hash map in memory.

How bigger? That depends on the arch/CPU/whatever a lot I guess.

But docs would be for sure nice.

blind-oracle avatar May 15 '25 07:05 blind-oracle

Changed the docs slightly, but as @blind-oracle has said, it's really just a hash vs linear search trade off. The docs are deliberately a little bare because for most users, the linear search is the better choice.

Peternator7 avatar Jun 29 '25 23:06 Peternator7