Allocations in the loop

A Dict is a hash table. To find the key, it first hashes the value you input, r1, which it knowns to be an Int. So far so good. It then looks up in that hash value’s bucket and finds a single key, of unknown type at compile-time (because your dict is a Dict{Any, Any}). To know that this key is indeed r1, it calls ==(r1::Int, that_key::Any). That’s a dynamic call. At runtime, it’ll figure out that that_key is also an Int, and call ==(::Int,::Int).

Your test3 also allocates in the loop AFAICT. It’s just that because the loop isn’t nested, it doesn’t allocate as much.