Understanding how broadcasting works with Dict


I’m trying to work out what happens under the hood in the following broadcasting situation:

d = Dict{Int,String}(1=>"a")
get.(d, [1,2], "nothing here")

The output is as I expected, ie ["a", "nothing here"]. But what I am worried about is whether this operation is efficient if d is large. That is, do we get something like [ get(d, i, "nothing here") for i in [1,2] ] under the hood (which would be fairly efficient when d is large), or is d copied out to match the length of [1,2], which would be problematic?




On 0.6, Dicts are treated as “scalar-like” — that is, they are just passed along to all the expanded dimensions in broadcast. That’s your “efficient” first example. This behavior is deprecated on 0.7, and it’ll likely be an error in 1.0 as we may want to experiment with broadcasting across a dictionary’s key-value pairs in some manner in the future.

In general, broadcast doesn’t copy values to do its expansions, be it with arrays or scalar-like objects.


Thanks for responding. Understood.

(I’m actually using v0.7 and didn’t get a deprecation warning, but I’m guessing that is because my build is 37 days old). :slight_smile: