classification
Title: Statically allocate interpreter states as much as possible.
Type: Stage: patch review
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.snow Nosy List: Mark.Shannon, eric.snow, gvanrossum, vstinner
Priority: normal Keywords: patch

Created on 2021-12-01 18:50 by eric.snow, last changed 2022-01-14 01:22 by vstinner.

Pull Requests
URL Status Linked Edit
PR 29883 merged eric.snow, 2021-12-01 19:30
PR 30092 merged eric.snow, 2021-12-13 20:44
PR 30096 merged eric.snow, 2021-12-14 01:16
PR 30588 merged eric.snow, 2022-01-13 23:04
PR 30589 merged eric.snow, 2022-01-13 23:36
PR 30590 merged eric.snow, 2022-01-13 23:38
Messages (6)
msg407476 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2021-12-01 18:50
Currently we allocate each new PyInterpreterState in PyInterpreterState_New().  Furthermore, PyInterpreterState is full of pointers which are almost all allocated on the heap during runtime init.  We can statically allocate (and initialize?) most of what goes into PyInterpreterState, as well as the main interpreter itself (as part of _PyRuntimeState).  This includes each interpreter's initial PyThreadState.

TODO:

* [ ] add `PyInterpreterState main;` to _PyRuntimeState.interpreters and use it
* [ ] change references from the pointer to the value
* [ ] add `PyThreadState _main;` to PyInterpreterState.threads and use it
* [ ] change references from the pointer to the value
* [ ] change PyInterpreterState pointer fields to values (as much as possible)
* [ ] change PyThreadState pointer fields to values (as much as possible)


benefits:

* fewer possible failures (no memory) during runtime/interpreter/thread init
* improved memory locality for pointers looked up relative to interpreter/thread state


There is one non-trivial bit: embedding the various PyObject values in PyInterpreterState and PyThreadState means hard-coding the various pieces of the object there (e.g. for dict, its keys/values; for list, its array), as well as adding necessary init code to PyInterpreterState_New() and PyThreadState_New().  The resulting added complexity can be mitigated somewhat with macros or even code generation.  (In fact, there is probably significant overlap with Guido's deepfreeze tool.)  Regardless, we'll probably need to factor out init funcs for a number of object types, where currently there are only "Py*_New()" funcs that combine allocation and init.
msg408507 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2021-12-14 01:04
New changeset 121f1f893a39d0b58d3d2b5597505c154ecaac2a by Eric Snow in branch 'main':
bpo-45953: Statically initialize the small ints. (gh-30092)
https://github.com/python/cpython/commit/121f1f893a39d0b58d3d2b5597505c154ecaac2a
msg410315 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2022-01-11 16:37
New changeset cf496d657a1a82eaf9ebfb47d721676fef6effa5 by Eric Snow in branch 'main':
bpo-45953: Statically allocate and initialize global bytes objects. (gh-30096)
https://github.com/python/cpython/commit/cf496d657a1a82eaf9ebfb47d721676fef6effa5
msg410441 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2022-01-12 23:28
New changeset ed57b36c32e521162dbb97199e64a340d3bff827 by Eric Snow in branch 'main':
bpo-45953: Statically allocate the main interpreter (and initial thread state). (gh-29883)
https://github.com/python/cpython/commit/ed57b36c32e521162dbb97199e64a340d3bff827
msg410536 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2022-01-14 00:09
New changeset 324908ba936d5d262026deebb81f050803848c41 by Eric Snow in branch 'main':
bpo-45953: Statically initialize all the PyThreadState fields we can. (gh-30590)
https://github.com/python/cpython/commit/324908ba936d5d262026deebb81f050803848c41
msg410537 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2022-01-14 00:17
New changeset 322f962f3ee31d0dbde99e36379de8488ccc6804 by Eric Snow in branch 'main':
bpo-45953: Statically initialize all the non-object PyInterpreterState fields we can. (gh-30589)
https://github.com/python/cpython/commit/322f962f3ee31d0dbde99e36379de8488ccc6804
History
Date User Action Args
2022-01-14 01:22:24vstinnersetnosy: + vstinner
2022-01-14 00:17:46eric.snowsetmessages: + msg410537
2022-01-14 00:09:33eric.snowsetmessages: + msg410536
2022-01-13 23:38:17eric.snowsetpull_requests: + pull_request28788
2022-01-13 23:36:15eric.snowsetpull_requests: + pull_request28787
2022-01-13 23:04:42eric.snowsetpull_requests: + pull_request28786
2022-01-12 23:28:55eric.snowsetmessages: + msg410441
2022-01-11 16:37:34eric.snowsetmessages: + msg410315
2021-12-14 01:16:00eric.snowsetpull_requests: + pull_request28319
2021-12-14 01:04:10eric.snowsetmessages: + msg408507
2021-12-13 20:44:00eric.snowsetpull_requests: + pull_request28315
2021-12-01 20:48:09gvanrossumsetnosy: + gvanrossum
2021-12-01 19:30:34eric.snowsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request28109
2021-12-01 18:50:39eric.snowcreate