Dict's different behavior for using and include

You probably need to rehash! the dictionary after precompilation since the serialized hashes are not valid anymore.

So you can add

function __init__()
    Base.rehash!(data)
end

to your module. Also, don’t use the .keys field for dictionaries, it’s an internal field.

Specifically, see this part in the manual Modules · The Julia Language

Dictionary and set types, or in general anything that depends on the output of a hash(key) method, are a trickier case. In the common case where the keys are numbers, strings, symbols, ranges, Expr , or compositions of these types (via arrays, tuples, sets, pairs, etc.) they are safe to precompile. However, for a few other key types, such as Function or DataType and generic user-defined types where you haven’t defined a hash method, the fallback hash method depends on the memory address of the object (via its objectid ) and hence may change from run to run. If you have one of these key types, or if you aren’t sure, to be safe you can initialize this dictionary from within your __init__ function. Alternatively, you can use the IdDict dictionary type, which is specially handled by precompilation so that it is safe to initialize at compile-time.

So a better solution than I wrote above is likely to use an IdDict

4 Likes