Thanks, I completely missed IdDict
!
Interestingly, while it seems to give ~20% edge over a normal dict when the value type is concrete, it slows down the code enormously, as it somehow gets in trouble with Vector
as its value type:
julia> const idd=IdDict{Type, Vector}( Int => [1], Float32 => [2], Vector{Int} => [3])
IdDict{Type,Array{T,1} where T} with 3 entries:
Float32 => [2]
Array{Int64,1} => [3]
Int64 => [1]
julia> @btime idd[Vector{Int}]
623.728 ns (2 allocations: 96 bytes) # 41.926 ns (0 allocations: 0 bytes) for a normal Dict{Type, Vector}
1-element Array{Int64,1}:
3
Please also let me note that - thanks to my ignorance - I found:
julia> const intvec_symbol = Symbol(string(Vector{Int}))
Symbol("Array{Int64,1}")
julia> const sd=Dict{Symbol,Vector}( :Int => [1], :Float32 => [2], intvec_symbol => [3])
Dict{Symbol,Array{T,1} where T} with 3 entries:
:Float32 => [2]
Symbol("Array{Int64,1}") => [3]
:Int => [1]
julia> @btime sd[intvec_symbol]
7.324 ns (0 allocations: 0 bytes)
1-element Array{Int64,1}:
3
Which allows the following implementation:
julia> const pools = Dict{Symbol, Vector}()
Dict{Symbol,Array{T,1} where T} with 0 entries
julia> function createpool(type::Type{T}) where T
pool = Vector{type}()
pools[Symbol(string(T))] = pool
return pool
end
createpool (generic function with 1 method)
julia> @generated function getpool(::Type{T}) where T
s = Symbol(string(T))
return quote
if !haskey(pools, $(Base.Meta.quot(s)))
createpool(T)
end
return pools[$(Base.Meta.quot(s))]
end
end
getpool (generic function with 1 method)
julia> @btime getpool(Dict{String,Symbol})
14.869 ns (0 allocations: 0 bytes)
0-element Array{Dict{String,Symbol},1}
This is my best one with only supported features used.
This could be the base of a fast type-keyed dict package, but I am not sure if anybody is interested in fast type-keyed dicts. What do you think?