I don’t understand the details of the implementation of Dict
, but I don’t think the V
in Dict{K,V}
is involved in the “essential functionality” in haskey
.
For example, when I start a REPL session, there are method instances such as haskey(::Dict{String, ::Any}, ::String)
. However, within ordinary call of haskey
, full specialization will be done. Is there an easy way to avoid the full specialization on the caller side?
julia> dict = Dict("key" => (1, 2))
Dict{String, Tuple{Int64, Int64}} with 1 entry:
"key" => (1, 2)
julia> using MethodAnalysis
julia> methodinstance(Tuple{typeof(haskey), Dict{String, Any}, String}) # just for example
MethodInstance for haskey(::Dict{String, Any}, ::String)
julia> methodinstance(Tuple{typeof(haskey), Dict{String, Tuple{Int, Int}}, String})
julia> haskey(dict, "key")
true
julia> methodinstance(Tuple{typeof(haskey), Dict{String, Tuple{Int, Int}}, String})
MethodInstance for haskey(::Dict{String, Tuple{Int64, Int64}}, ::String)
julia> versioninfo()
Julia Version 1.8.0-DEV.442
Commit 897c8e607f (2021-08-29 18:44 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, tigerlake)
Of course, I know that:
julia> Dict{String, Tuple{Int, Int}} <: Dict{String, Any}
false
julia> Dict{String, Tuple{Int, Int}} <: Dict{String}
true
So, I thought the following method would be helpful, but in reality it was not.
_haskey(@nospecialize(dict::Dict{String}), key::String) = haskey(dict, key)
I picked up the example of haskey
as a simple case, and “I” don’t have any performance troubles with haskey
itself. I am more interested in more general solutions than the Dict
specific techniques.