# Understanding the limitations of generated functions

**URL:** <https://discourse.julialang.org/t/understanding-the-limitations-of-generated-functions/43660>\
**Category:** General Usage\
**Tags:** question, performance, metaprogramming\
**Created:** [July 25, 2020, 10:37am UTC](https://discourse.julialang.org/t/understanding-the-limitations-of-generated-functions/43660 "2020-07-25T10:37:56Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![tisztamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tisztamo/32/16200_2.png) [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Post date:** [July 25, 2020, 8:50pm UTC](https://discourse.julialang.org/t/understanding-the-limitations-of-generated-functions/43660/5 "2020-07-25T20:50:55Z")

</div>

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
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
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
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?

---

_[View the full topic](https://discourse.julialang.org/t/understanding-the-limitations-of-generated-functions/43660)._
