Skip to content

Commit 38aff47

Browse files
timholyaviatesk
andauthored
Require n::Integer for sizehint!(a, n) (#59168)
Currently we're a bit hit-and-miss about any type-restriction on the second argument to `sizehint!`: of 11 methods, 4 require `n::Integer` and the remainder are unrestricted. Arguably, our dispatch is "prettier" if methods differ only in their first argument and are consistent about type-restrictions on `n`. This imposes `::Integer` restrictions on all methods that specify a non-abstract type for the first argument. Only the fallback methods for `AbstractDict`, `AbstractSet`, and `AbstractArray` are left unrestricted. It also makes similar changes to a `rehash!` and `hashindex` method. On 1.12-rc1, JET shows 40 runtime dispatches for `report_opt(merge!, (AbstractDict, AbstractDict))`, most of which are triggered from a branch on `sizehint!(::WeakKeyDict, ::Any)`. This seems likely to silence most of those, but it's not possible to check due to JET currently throwing errors on master. Because of these unrestricted fallbacks, there is one funny thing which applies to both this PR and master: ```julia julia> v = [1, 2, 3]; julia> sizehint!(v, 7.0); # no-op ``` This is mitigated by the fact that `sizehint!` is just a hint. To me it still seems like it might be better to throw an error in such cases, but that is not implemented by this PR. --------- Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent 2eb2bf9 commit 38aff47

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

base/dict.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ _shorthash7(hsh::UInt) = (hsh >> (8sizeof(UInt)-7))%UInt8 | 0x80
124124
# hashindex (key, sz) - computes optimal position and shorthash7
125125
# idx - optimal position in the hash table
126126
# sh::UInt8 - short hash (7 highest hash bits)
127-
function hashindex(key, sz)
127+
function hashindex(key, sz::Integer)
128+
sz = Int(sz)::Int
128129
hsh = hash(key)::UInt
129-
idx = (((hsh % Int) & (sz-1)) + 1)::Int
130+
idx = ((hsh % Int) & (sz-1)) + 1
130131
return idx, _shorthash7(hsh)
131132
end
132133

base/iddict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ IdDict(kv) = dict_with_eltype((K, V) -> IdDict{K, V}, kv, eltype(kv))
5858

5959
empty(d::IdDict, ::Type{K}, ::Type{V}) where {K, V} = IdDict{K,V}()
6060

61-
function rehash!(d::IdDict, newsz = length(d.ht)%UInt)
61+
function rehash!(d::IdDict, newsz::Integer = length(d.ht)%UInt)
6262
d.ht = ccall(:jl_idtable_rehash, Memory{Any}, (Any, Csize_t), d.ht, newsz)
6363
d
6464
end
6565

66-
function sizehint!(d::IdDict, newsz)
66+
function sizehint!(d::IdDict, newsz::Integer)
6767
newsz = _tablesz(newsz*2) # *2 for keys and values in same array
6868
oldsz = length(d.ht)
6969
# grow at least 25%

base/idset.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pop!(s::IdSet, @nospecialize(x)) = _pop!(s, x) == -1 ? throw(KeyError(x)) : x
7878
pop!(s::IdSet, @nospecialize(x), @nospecialize(default)) = _pop!(s, x) == -1 ? default : x
7979
delete!(s::IdSet, @nospecialize(x)) = (_pop!(s, x); s)
8080

81-
function sizehint!(s::IdSet, newsz)
81+
function sizehint!(s::IdSet, newsz::Integer)
8282
# TODO: grow/compact list and perform rehash, if profitable?
8383
# TODO: shrink?
8484
# s.list = resize(s.list, newsz)

base/set.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ copymutable(s::Set{T}) where {T} = Set{T}(s)
169169
# Set is the default mutable fall-back
170170
copymutable(s::AbstractSet{T}) where {T} = Set{T}(s)
171171

172-
sizehint!(s::Set, newsz; shrink::Bool=true) = (sizehint!(s.dict, newsz; shrink); s)
172+
sizehint!(s::Set, newsz::Integer; shrink::Bool=true) = (sizehint!(s.dict, newsz; shrink); s)
173173
empty!(s::Set) = (empty!(s.dict); s)
174174
rehash!(s::Set) = (rehash!(s.dict); s)
175175

base/weakkeydict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function _cleanup_locked(h::WeakKeyDict)
7676
return h
7777
end
7878

79-
sizehint!(d::WeakKeyDict, newsz; shrink::Bool = true) = @lock d sizehint!(d.ht, newsz; shrink = shrink)
79+
sizehint!(d::WeakKeyDict, newsz::Integer; shrink::Bool = true) = @lock d sizehint!(d.ht, newsz; shrink = shrink)
8080
empty(d::WeakKeyDict, ::Type{K}, ::Type{V}) where {K, V} = WeakKeyDict{K, V}()
8181

8282
IteratorSize(::Type{<:WeakKeyDict}) = SizeUnknown()

0 commit comments

Comments
 (0)