There is a common pattern for working with Dict which is to look up the value and modify it in some way. DataStructures.jl’s Accumulator uses the value as an accumulator to keep sum, StatsBase.countmap uses it to do a frequency count and there are implementation in FreqTable.jl and SplitApplyCombine.jl
The slow way to do this is
#let dict be a Dict{T,S}
szero = zero(S)
dict[key] = some_function(get(dict, key, szero))
In here the lookup happens twice one for dict[key]
and one for get(dict, key, szero)
. This method is used in DataStructures.jl accumulator, another method is used by groupreduce
in SplitApplyCombine which uses Base.ht_keyindex2 to avoid two lookups and the speed difference is huge in favor of the latter.
Given the commonality of this pattern, I think it’s worthwhile to have a function called getindexmodify
that can get an index and apply a function to its value to update it in one lookup.