`IdDict{UInt64, Float64}` is much slower than `Dict{UInt64, Float64}` for retrieving values

MWE:

julia> k = rand(UInt, 5000);

julia> v = rand(Float64, 5000);

julia> using Random

julia> ks = shuffle(k);

julia> d1 = Dict(k .=> v);

julia> d2 = IdDict(k .=> v);

julia> using BenchmarkTools

julia> @benchmark begin
           for i in $ks
               ($d1)[i]
           end
       end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  11.500 μs … 67.900 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     11.700 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   11.786 μs ±  1.129 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▃██ ▆▂  ▁         ▁                                         ▂
  ███▁██▁▆█▁█▅▇▁█▅▁██▁▄▁▁▅▅▄▁▅▄▁▄▄▁▃▄▅▁▅▄▁▄▅▁▄▄▁▄▄▁▁▄▄▁▄▃▁▃▁▃ █
  11.5 μs      Histogram: log(frequency) by time      15.6 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark begin
           for i in $ks
               ($d2)[i]
           end
       end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  60.800 μs … 424.700 μs  ┊ GC (min … max): 0.00% … 79.09%
 Time  (median):     62.500 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   65.249 μs ±  16.455 μs  ┊ GC (mean ± σ):  1.88% ±  5.92%

  ▃█▅▃▂             ▁▁                                         ▁
  ███████▇▇▇▇▇▇▇▇▇████▇▇▆▅▆▆▆▅▅▅▅▄▅▆▃▅▄▄▅▅▄▅▄▃▅▄▄▅▄▅▄▅▁▄▃▁▅▅▄▃ █
  60.8 μs       Histogram: log(frequency) by time       118 μs <

 Memory estimate: 78.12 KiB, allocs estimate: 5000.

Is this expected? I thought IdDict would be at least as performant as Dict (even) for bits types. Thanks!

The IdDict is implemented in C and therefore doesn’t get quite as well optimized as a regular Dict.

1 Like

Cool. Thanks!