Skip to content

Performance impact of non-str dict keys #208

Closed
@Jongy

Description

@Jongy

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions