Sometimes not so marginal. A while ago I wrote this (simplified here)
function merge_dict(ds...) t = eltype(ds) merge_dict!(t(),ds) end function merge_dict!(d::AbstractDict, others) for other in others @inbounds for (k,v) in other d[k] = v end end return d end
julia> v = [Dict(rand(1:1000) => rand() for _ in 1:10) for _ in 1:10^4]; julia> @btime merge_dict(v...); 2.191 ms (21 allocations: 327.00 KiB) julia> @btime merge(v...); ERROR: StackOverflowError: julia> v = [Dict(rand(1:1000) => rand() for _ in 1:10) for _ in 1:10^3]; julia> @btime merge_dict(v...); 236.527 μs (21 allocations: 116.06 KiB) julia> @btime merge(v...); 49.820 ms (11963 allocations: 27.52 MiB)
I submitted a PR about a year ago making this a method for
merge. But, my version breaks type-stability, important in some cases, obviously not here. Still my code is useful in many situations (and I actually a use a version that takes a
Vector rather than splatting. However splatting 10^3 and 10^4 arguments entails very little performance penalty.)
So I changed the PR, upon suggestion, to improve the efficiency of
reduce(merge, dicts) instead. There were many choices, but getting the basic implementation was fast. Pushing the PR through was an odyssey. Because
merge are so widely used, things have to be done carefully. I forgot, just looked now, that the PR still has not been merged. It was bumped less than a day ago.
It was a great learning experience, but quite a bit of effort. This depends also on how experienced you are at making PRs to base.