You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CPython has a specialized dictionary lookup function for str-only keys. The first time a dict instance is accessed with a non-str key, it's modified so future lookups use the generic function. Performance imapct is observable:
In [1]: d = {str(i): 1 for i in range(1_000)}
In [2]: %timeit d["1"]
26.7 ns ± 0.0895 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [3]: d[1] = 1
In [4]: %timeit d["1"]
33.2 ns ± 0.117 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
This is non-reversible - the particular dict instance will keep using the generic function forever, even if non-str keys are removed.
In [5]: del d[1]
In [6]: %timeit d["1"]
33.8 ns ± 1.1 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
I'll submit a PR containing adding a section explaining this
The text was updated successfully, but these errors were encountered:
CPython has a specialized dictionary lookup function for
str
-only keys. The first time adict
instance is accessed with a non-str
key, it's modified so future lookups use the generic function. Performance imapct is observable:This is non-reversible - the particular
dict
instance will keep using the generic function forever, even if non-str
keys are removed.I'll submit a PR containing adding a section explaining this
The text was updated successfully, but these errors were encountered: