How to use a sequence as dict key?

I’ve added the TupleKeyDict as a new recipe in Working with tuples — Numba-how-to documentation

Thanks @luk-f-a.

As queried: Dynamic, heterogeneous dictionaries with tuple keys and non-constant tuple slicing - #5 by ulupo

is the idea used in the overloaded setitem method of TupleKeyDict safe to collisions?

I suggested hashing the tuple here How to use a sequence as dict key? - #4 by stuartarchibald, what amounts to a suitable hash depends on the use case!

The example I wrote is relying on the hashing implementation for tuples-of-ints. This has been strengthed a lot in recent versions of Python, Numba replicates the algorithm. It’s not guaranteed collision free but in practice may be ok for a given use case. Practically, the values are often bounded and so it may be feasible to just test them all!?

With regards to using “unsafe” parts of numba: the drop_elements function defined above (which relies on numba.cpython.unsafe.tuple.tuple_setitem) ended up becoming an essential part of my solution to the problem mentioned in the related topic https://numba.discourse.group/t/dynamic-heterogeneous-dictionaries-with-tuple-keys-and-non-constant-tuple-slicing. There, I also ended up being able to solve the bottleneck caused by populating a TypedDict in a pure Python context by using another “unsafe” component, namely to_fixed_tuple from numba.np.unsafe.ndarray. The way I did this other one might be of independent interest and I will likely create a new topic about it.

To remain on-topic here, I wonder generically: what exactly is “unsafe” about these components? Is there something to worry about beyond the lack of stability guarantees as these components might change or disappear in new numba versions? At the moment, I would potentially be happy to fix the numba dependency in my project to a specific version so that my users can benefit from the added performance, and keep an eye open for how things change in future versions.

In the end, I thought a pre-existing GitHub issue was a good place to mention this: The tuple built-in is not supported in nopython mode · Issue #2771 · numba/numba · GitHub