Skip to content

Can we skip writing the key into a Dict if we know it's already present? #52066

@Zentrik

Description

@Zentrik

julia/base/dict.jl

Lines 404 to 417 in e7345b8

function setindex!(h::Dict{K,Any}, v, key::K) where K
@nospecialize v
index, sh = ht_keyindex2_shorthash!(h, key)
if index > 0
h.age += 1
@inbounds h.keys[index] = key
@inbounds h.vals[index] = v
else
@inbounds _setindex!(h, v, key, -index, sh)
end
return h
end

ht_keyindex2_shorthash!only returnsindex > 0` if

if key === k || isequal(key, k)

As isequal(x,y) implies hash(x) == hash(y) and the Dict docs say 'Keys are compared with isequal and hashed with hash', seems like it should be fine to skip the h.keys[index] = key.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions