Skip to content

Commit

Permalink
Add convenience calls for rehashing
Browse files Browse the repository at this point in the history
This is relevant particularly to precompilation, which requires
that any module global dicts get rehashed by the __init__
function.
  • Loading branch information
timholy committed Dec 3, 2014
1 parent 3d6c915 commit e7ce4cb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/dict.jl
Expand Up @@ -414,7 +414,7 @@ isslotempty(h::Dict, i::Int) = h.slots[i] == 0x0
isslotfilled(h::Dict, i::Int) = h.slots[i] == 0x1
isslotmissing(h::Dict, i::Int) = h.slots[i] == 0x2

function rehash{K,V}(h::Dict{K,V}, newsz)
function rehash{K,V}(h::Dict{K,V}, newsz = length(h.keys))
olds = h.slots
oldk = h.keys
oldv = h.vals
Expand Down
2 changes: 2 additions & 0 deletions base/set.jl
Expand Up @@ -38,6 +38,8 @@ copy(s::Set) = union!(similar(s), s)
sizehint(s::Set, newsz) = (sizehint(s.dict, newsz); s)
empty!{T}(s::Set{T}) = (empty!(s.dict); s)

rehash(s::Set) = (rehash(s.dict); s)

start(s::Set) = start(s.dict)
done(s::Set, state) = done(s.dict, state)
# NOTE: manually optimized to take advantage of Dict representation
Expand Down

12 comments on commit e7ce4cb

@stevengj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that rehash is not yet exported (or documented).

@stevengj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also have rehash for ObjectIdDict.

@timholy
Copy link
Member Author

@timholy timholy commented on e7ce4cb Dec 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it shouldn't be exported.

@stevengj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not, if you think this is useful for external packages that want to be precompiled?

@simonster
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, ObjectIdDicts don't need to be rehashed when used in precompiled code (see #8665).

@stevengj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good to know.

@stevengj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be rehash! since it is mutating, BTW?

@timholy
Copy link
Member Author

@timholy timholy commented on e7ce4cb Dec 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably true. But in the same vein, should sizehint really be sizehint!? In neither case is the content of the container being mutated, it's only something about the internal representation.

@nalimilan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer sizehint to be sizehint!. :-)

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that @JeffBezanson has some strongish opinions about this.

@JeffBezanson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to consider something "mutating" only if it changes what values the argument would be == to. However I suppose there is another view, which says a function is mutating if you don't need its return value to get its effect. For example a non-mutating sizehint would look like

A = sizehint(B)
# now A grows more efficiently than B

Safe to say nobody will be asking for such a function.

Another way to look at it is the "permissions" view: if f were not allowed to mutate x, would it be ok for f to call sizehint or rehash?

Then there is the "concurrency" perspective: these functions write to the values, so even if there is no net effect it is not safe to do concurrently with other operations on the values.

So I'm not sure; this is a borderline case!

@johnmyleswhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having spent a bunch of time talking about Rust lately, I'd like Julia to adopt the concurrency/write viewpoint when deciding whether to use exclamation marks.

Please sign in to comment.