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

improve effects of objectid and getindex(::Dict) #49447

Merged
3 changes: 2 additions & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ function empty!(h::Dict{K,V}) where V where K
end

# get the index where a key is stored, or -1 if not present
function ht_keyindex(h::Dict{K,V}, key) where V where K
@assume_effects :terminates_locally function ht_keyindex(h::Dict{K,V}, key) where V where K
isempty(h) && return -1
sz = length(h.keys)
iter = 0
maxprobe = h.maxprobe
maxprobe < sz || throw(AssertionError()) # This error will never trigger, but is needed for terminates_locally to be valid
index, sh = hashindex(key, sz)
keys = h.keys

Expand Down
33 changes: 22 additions & 11 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,6 @@ macro locals()
return Expr(:locals)
end

"""
objectid(x) -> UInt

Get a hash value for `x` based on object identity.

If `x === y` then `objectid(x) == objectid(y)`, and usually when `x !== y`, `objectid(x) != objectid(y)`.

See also [`hash`](@ref), [`IdDict`](@ref).
"""
objectid(@nospecialize(x)) = ccall(:jl_object_id, UInt, (Any,), x)

# concrete datatype predicates

datatype_fieldtypes(x::DataType) = ccall(:jl_get_fieldtypes, Core.SimpleVector, (Any,), x)
Expand Down Expand Up @@ -600,6 +589,28 @@ Return `true` if `x` is an instance of an [`isbitstype`](@ref) type.
"""
isbits(@nospecialize x) = isbitstype(typeof(x))

"""
objectid(x) -> UInt

Get a hash value for `x` based on object identity.

If `x === y` then `objectid(x) == objectid(y)`, and usually when `x !== y`, `objectid(x) != objectid(y)`.

See also [`hash`](@ref), [`IdDict`](@ref).
"""
function objectid(x)
# objectid is foldable iff it isn't a pointer.
if isidentityfree(typeof(x))
return _foldable_objectid(x)
end
return _objectid(x)
end
function _foldable_objectid(@nospecialize(x))
@_foldable_meta
_objectid(x)
end
_objectid(@nospecialize(x)) = ccall(:jl_object_id, UInt, (Any,), x)

"""
isdispatchtuple(T)

Expand Down
6 changes: 6 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1363,3 +1363,9 @@ end
sizehint!(d, 10)
@test length(d.slots) < 100
end

# getindex is effect free and nothrow but not consistent
for T in (String, Module, Symbol, Int, Float64)
@test Core.Compiler.is_effect_free(Base.infer_effects(getindex, (Dict{T, BigInt},T)))
@test Core.Compiler.is_terminates(Base.infer_effects(getindex, (Dict{T, BigInt},T)))
end
6 changes: 6 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,12 @@ ambig_effects_test(a, b) = 1
@test Base.infer_effects(===, (Any,Any)) |> Core.Compiler.is_foldable_nothrow
@test (Base.infer_effects(setfield!, ()); true) # `builtin_effects` shouldn't throw on empty `argtypes`
@test (Base.infer_effects(Core.Intrinsics.arraylen, ()); true) # `intrinsic_effects` shouldn't throw on empty `argtypes`
for T in (Int, String, Symbol, Some{Int}, Some{String}, Some{Symbol}, Some{Some{Int}}, Some{Some{String}}, Some{Some{Symbol}})
@test Core.Compiler.is_foldable(Base.infer_effects(hash, (T,)))
end
# objectid for datatypes is inconsistant for types that have unbound type parameters.
@test !Core.Compiler.is_consistent(Base.infer_effects(hash, (DataType,)))
@test !Core.Compiler.is_consistent(Base.infer_effects(hash, (Tuple{Vector{Int}},)))
aviatesk marked this conversation as resolved.
Show resolved Hide resolved
end

@test Base._methods_by_ftype(Tuple{}, -1, Base.get_world_counter()) == Any[]
Expand Down