I’m working in a package where the user provides a Dict that is never changed by my package. While I know the structure of the entries, I do not know the which types it contains. Is there a way to “convert” a poorly typed dict in a fully specified one for optimal performance?
A simplified example would look like this. A user provides d1:
Your first issue is gonna be that the type of your values is not fully inferred. This is an optimization sometimes done by Julia instead of dealing with weird Unions:
but if there were more deeply nested data structures, it could have made a difference. This is particularly useful when deserializing data from a format that loses type information, such as JSON.
If your code needs to do a lot of work for each entry of the Dict, then consider using a function barrier like this:
for (k,v) in d1
do_the_work(k,v)
end
and then later
function do_the_work(k,v)
...
end
The main routine still needs to pay the price for deciphering the types of k and v at run-time. But do_the_work will get specialized and compiled separately for each different type of k and v.