Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance impact of non-str dict keys #208

Closed
Jongy opened this issue Jun 30, 2020 · 1 comment · Fixed by #209
Closed

Performance impact of non-str dict keys #208

Jongy opened this issue Jun 30, 2020 · 1 comment · Fixed by #209

Comments

@Jongy
Copy link
Contributor

Jongy commented Jun 30, 2020

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

@satwikkansal
Copy link
Owner

satwikkansal commented Jul 1, 2020

Hi @Jongy, thanks.

This will be a good addition to the collection. Looking forward to your PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants