dgkf / R

An experimental reimagining of R
https://dgkf.github.io/R
GNU General Public License v3.0
136 stars 5 forks source link

Use `phf` for primitive function lookup #174

Open dgkf opened 3 months ago

dgkf commented 3 months ago

Literal showerthought: why should we have to hash our primitive function names to look up their associated implementation if the list is going to be static? Surely we can build something that doesn't require extra hashing step and can map directly to an index.

And sure enough, it sounds like there's a better static map data structure for this. I found the phf crate after a very non-exaustive search. They even give a very similar example of looking up language keywords.

An added benefit of this is that it can be purely static. No lazy static or LazyLock needed, which should simplify this code even further and avoid the first-time-access cost of building the map (although I'm sure this cost is negligible in the current state).