When do map keys hashed in Odin?

Hi all! So in Odin you have maps, which is good. What’s even better is that you can use typeid as a map key.

Question:

From my experience with C#, you can have a dictionary (map) with a string key. At runtime, that string will be implicitly used as a hash. A bit slower than using int as keys.

In Nim lang, as far as I can tell, something similar happens BUT I can hash the string at compile time and end up with int keys.

What about Odin? If I use typeid, what key will I get? Will it be converted to a hash at runtime or compile time? Same question about using strings as keys in Odin.

All hashing for maps will be done at run-time, to my knowledge. Odin is pretty light on compile-time evaluation, effectively only allowing expressions involving constants (and no proc calls). LLVM may also evaluate simple stuff at compile-time too, but I think hashing will be well beyond that.

A typeid is just an integer (uintptr, in essence) so its hashing will be relatively straightforward. It will still be done at run-time, though, to my knowledge.

2 Likes

In theory, some hashes could be done at compile-time, but they are all currently done at run-time. We could add this as an optimization in the future, but it’s not a high priority yet.

2 Likes