Skip to content

Improved performance and arguably simpler code for dictionaries by changing the keys layout #142889

@markshannon

Description

@markshannon

Current layout

Currently the _dictkeysobject struct is laid out like this:

ptr ---->  +--------------+
           |     header   |
           +--------------+
           |    indices   |
           +--------------+
           |     keys     |
           +--------------+

which requires some relatively expensive calculation to find the start of the keys, as the indices are not only variable in number, but variable in size also.

Proposed layout

If instead it is laid out as follows:

           +--------------+
           |    indices   |
ptr ---->  +--------------+
           |     header   |
           +--------------+
           |     keys     |
           +--------------+

and the indices laid from highest to lowest with 0 just before ptr, finding the start of the keys is as simple as ptr->keys . Accessing an index is no slower, and the code barely any more complex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions