I’ve noticed that if a key in a Dict is a structure, the haskey function allocates memory:
d = Dict{Int64, Int64}()
@allocated haskey(d, 0) # 0 when key is an Int
struct Key
value :: Int64
end
d = Dict{Key, Int64}()
@allocated haskey(d, Key(0)) # 16 bytes!
I’m using Julia 1.1
Is there anything I can do to avoid those allocations while still using structures as the key?
Thanks,
Kuba
The allocations actually don’t happen in haskey at all, but because of the call to Key(0) instead. This can be safely ignored, because this will pretty much always just be on the stack.