Struct containing a dictionary of cache buffers?

First, I’ll remark that Function is an abstract type (i.e., definitely not concrete) so your closure attempt is not remotely stable. This explains why you see Any pop out the type-inferred code.

Second, there is no way that you can have dict[key] be type stable if dict contains elements of different types (note that different functions have different types). The best you can do is have a small Union of types that it will union-split for you. But if you have more than 4(ish) types, it won’t even do that.

Which is to say that you are left to things like function barrirers or index-site annotations. These aren’t the end of the world and, depending on the situation, may suffer only a negligible performance penalty.

Another option, if you insist on full stability, is to use a NamedTuple (or custom struct) instead of a Dict. A NamedTuple has keys just like a Dict but can encode every entry with its own type (and similar for the fields of a struct). Although if you have many fields it might get unwieldy and if you have very many its performance might suffer.

1 Like