python / python/cpython

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

Open
#142,889 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core performance type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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.

Linked PRs
  • gh-145097
  • gh-150640

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the proposed _dictkeysobject layout and the linked PRs gh-145097 and gh-150640 first, since the work has moved there. Compare the current and proposed index, header, and keys arrangement; completion would require the implementation and its performance or correctness validation to support the new layout.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.