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

set! is not copy safe #98

Closed
mtfishman opened this issue Apr 22, 2022 · 3 comments · Fixed by #136
Closed

set! is not copy safe #98

mtfishman opened this issue Apr 22, 2022 · 3 comments · Fixed by #136

Comments

@mtfishman
Copy link
Contributor

julia> using Dictionaries

julia> d = Dictionary(["X", "Y"], [1, 2])
2-element Dictionary{String, Int64}
 "X"1
 "Y"2

julia> dc = copy(d)
2-element Dictionary{String, Int64}
 "X"1
 "Y"2

julia> set!(dc, "Z", 3)
3-element Dictionary{String, Int64}
 "X"1
 "Y"2
 "Z"3

julia> dc
3-element Dictionary{String, Int64}
 "X"1
 "Y"2
 "Z"3

julia> d
3-element Dictionary{String, Int64}
 "X"1
 "Y"2
 "Z"#undef

deepcopy is ok:

julia> d = Dictionary(["X", "Y"], [1, 2])
2-element Dictionary{String, Int64}
 "X"1
 "Y"2

julia> dc = deepcopy(d)
2-element Dictionary{String, Int64}
 "X"1
 "Y"2

julia> set!(dc, "Z", 3)
3-element Dictionary{String, Int64}
 "X"1
 "Y"2
 "Z"3

julia> d
2-element Dictionary{String, Int64}
 "X"1
 "Y"2
@andyferris
Copy link
Owner

Yes. Currently copy only copies values and not the keys, so that keys(d) === keys(copy(d)). Internally it uses similar and copyto! to copy the values.

This is something I have come to regret, and we might like to change the semantics of copy or else introduce a new function to copy keys and values. I have found in practice the (theoretically faster) value-only copy hurts more than it helps, and it's easier to e.g. map(identity, d) when you want that then it is to make a proper copy of both keys and values.

@andyferris
Copy link
Owner

(I'd welcome a PR on this one)

@mtfishman
Copy link
Contributor Author

Yes, I think the surprise of the behavior above outweighs the performance gain from not copying the indices (the current behavior led to a bug that took me a while to pin down). I changed the copying behavior in #101.

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

Successfully merging a pull request may close this issue.

2 participants