Could the usage of `haskey` be unidiomatic/inefficient?

Only you can Profile.@profile whether this is a perf problem in your specific code. This does cause two lookups, but the first one will fetch the relevant cache-line. So the second lookup only costs CPU and no mem-traffic nor does it have to wait an eternity for main memory.

If profiling shows that the second lookup is a significant part of your time, then my recommendation is Base.ht_keyindex2! (look up position of key; -pos if not present; may rehash dict to make space for new element) and Base.ht_keyindex (look up position of key, -1 if not present).

The optimizer might be capable of removing the second hash computation. I don’t think that the optimizer will be capable of removing the comparisons (the linear search of all colliding elements).

1 Like