Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of rehash! in constructor #9

Open
StephenVavasis opened this issue Sep 20, 2018 · 3 comments
Open

use of rehash! in constructor #9

StephenVavasis opened this issue Sep 20, 2018 · 3 comments

Comments

@StephenVavasis
Copy link
Contributor

StephenVavasis commented Sep 20, 2018

I noticed the following snippet in ordered_dict.jl:

    function OrderedDict{K,V}(d::OrderedDict{K,V}) where {K,V}
        if d.ndel > 0
            rehash!(d)
        end
        @assert d.ndel == 0
        new{K,V}(copy(d.slots), copy(d.keys), copy(d.vals), 0)
    end

Isn't this a known source of bugs? See JuliaCollections/DataStructures.jl#335

@odow
Copy link

odow commented Sep 13, 2020

Something weird related to rehash! happens when OrderedDicts are returned from a remote process. They need to be rehash!ed in order to work correctly.

@everywhere begin
    using OrderedCollections
    struct Foo
        x::Vector{Int}
    end
    function foo(i)    
        x = Foo(Int[])
        return x, OrderedDict(x => 2)
    end
end

function bar()
    x, y = pmap(foo, 1:1)[1]
    z = copy(y)
    OrderedCollections.rehash!(z)
    return haskey(y, x), haskey(z, x)
end
a, b = bar()  # false, true ?!?

@kmsquire
Copy link
Member

kmsquire commented Feb 1, 2021

@odow In general, it would be good to open another issue for things like this (and bump it if it's important and not being responded to), or ask a question on Discourse.

In this case, the issue is that you haven't defined a hash function for Foo, so it's defaulting to using the object ID as the hash key, and that isn't the same between different processes (it's basically equivalent to the memory address of the item). Rehashing "fixes" this.

It also won't generally work (locally or remotely) if you define a hash function that depends on Foo.x, and then modify Foo.x (you'll get a different hash value, which would put this value at a different location in the hash table.).

@odow
Copy link

odow commented Feb 1, 2021

@kmsquire I don't remember opening this, or what it was related to, so I guess I figured out the reason. Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants