python / python/cpython

`_PyStaticCode_InternStrings` is called ~800 times during startup.

Open
#96,459 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 interpreter-core performance topic-subinterpreters type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Description

The _Py_Deepfreeze_Init() function calls _PyStaticCode_InternStrings once for each string.

Creating the interned string dict in one go, from the table of static strings would be much faster.
something like this (error checking omitted, and other liberties taken for brevity)

_Py_CreateInternedDict(PyObject **strs, int n) {
   PyDictKeys *table = make_dict_keys(n);
   for (int i = 0; i < n; i++) {
        table->entries[i].key = table->entries[i].value = strs[i];
        table->entries[i].hashcode = strs[i].hash_code;
        // Fix up size and entries here.
   }
   return new_dict(table);
}

All the hash codes will need to be initialized first, but we probably should do that anyway.

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

Start by tracing _Py_Deepfreeze_Init() and its calls to _PyStaticCode_InternStrings. Review how the interned string dictionary and string hash codes are initialized, then determine whether the proposed bulk construction preserves the required dictionary state and startup behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
backend, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.