GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-45953: Statically allocate the main interpreter (and initial thread state). by ericsnowcurrently · Pull Request #29883 · python/cpython

Conversation

@ericsnowcurrently

@ericsnowcurrently ericsnowcurrently commented Dec 1, 2021 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Currently the main interpreter is allocated on the heap during runtime initialization. Here we are instead embedding it into _PyRuntimeState, which means it is statically allocated as part of the _PyRuntime global. The same goes for the initial thread state (of each interpreter, including the main one). Consequently there are fewer allocations during runtime/interpreter init, fewer possible failures, and better memory locality.

FYI, this also helps efforts to consolidate globals, which in turns helps work on subinterpreter isolation.

https://bugs.python.org/issue45953

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ericsnowcurrently added 11 commits December 1, 2021 10:54
@ericsnowcurrently
Return void from _PyEval_InitState().
5e6eda9
@ericsnowcurrently
Fix a memory leak.
de12b98
@ericsnowcurrently
Move thread-related interpreter state into a sub-struct.
035eff3
@ericsnowcurrently
Factor out init_threadstate().
1569a53
@ericsnowcurrently
Do not initialize a thread state if already initialized.
518e3f3
@ericsnowcurrently
Avoid allocating the first thread state.
9995a84
@ericsnowcurrently
Factor out init_interpreter().
f22d0da
@ericsnowcurrently
Preallocate the main interpreter.
cf39d57
@ericsnowcurrently
Add a note about expectations for init_interpreter().
b840213
@ericsnowcurrently
Add a NEWS entry.
3bf69b0
@ericsnowcurrently
Add _PyInterpreterState_Main().
4de466e
@ericsnowcurrently
ericsnowcurrently requested a review from markshannon as a code owner December 1, 2021 19:30
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Dec 1, 2021
@bedevere-bot bedevere-bot added the awaiting core review label Dec 1, 2021
@ericsnowcurrently ericsnowcurrently mentioned this pull request Dec 1, 2021
Use member non-pointer declarations in Py*State structs instead of heap-allocated pointers. faster-cpython/ideas#79
Closed
markshannon
markshannon reviewed Dec 2, 2021
View reviewed changes
Comment thread Python/ceval.c
int
_PyEval_InitState(struct _ceval_state *ceval)
void
_PyEval_InitState(struct _ceval_state *ceval, PyThread_type_lock pending_lock)

@markshannon markshannon Dec 2, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be a good opportunity to remove this function?
The _pending_calls struct belongs on the interpreter state, not on the ceval as only the main interpreter can handle them.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ericsnowcurrently ericsnowcurrently Dec 2, 2021

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's enough going on in this PR that I'd rather not.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
markshannon
markshannon reviewed Dec 2, 2021
View reviewed changes
Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

markshannon
markshannon reviewed Dec 2, 2021
View reviewed changes
Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

markshannon
markshannon requested changes Dec 2, 2021
View reviewed changes

@markshannon markshannon left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of tests have been inverted.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot removed the awaiting core review label Dec 2, 2021
@bedevere-bot

bedevere-bot commented Dec 2, 2021

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting changes label Dec 2, 2021
ericsnowcurrently added 8 commits December 2, 2021 16:43
@ericsnowcurrently
Fix a check for the main interpreter.
9bb32a6
@ericsnowcurrently
Ensure no subinterpreters are created until after the main interprete…
385f298
…r is.
@ericsnowcurrently
Make sure the interpreter is fully initialized.
354997f
@ericsnowcurrently
Use PyMem_RawCalloc() to allocate thread states.
9bbf8a0
@ericsnowcurrently
Make sure the thread state is fully initialized.
8d1cf35
@ericsnowcurrently
Clear the preallocated states when "deleted".
188aed8
@ericsnowcurrently
Statically initialize the "preallocated" states.
e0da483
@ericsnowcurrently
Fix a typo.
46c7767
markshannon
markshannon reviewed Jan 10, 2022
View reviewed changes
Comment thread Include/cpython/pystate.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_interp.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_interp.h Outdated
} _preallocated;
};

#define _PyInterpreterState_INIT \

@markshannon markshannon Jan 10, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used once, please remove it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericsnowcurrently reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@ericsnowcurrently ericsnowcurrently Jan 11, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I brought this back to avoid so much nesting and clutter in _PyRuntimeState_INIT.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_runtime.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_runtime.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_runtime.h
} _preallocated;
} _PyRuntimeState;

#define _PyRuntimeState_INIT \

@markshannon markshannon Jan 10, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well remove this as well. It is also only used once.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericsnowcurrently reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@ericsnowcurrently ericsnowcurrently Jan 10, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should _Py_global_objects_INIT also move? It's pretty big and likely to get much bigger.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ericsnowcurrently ericsnowcurrently Jan 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, _PyRuntimeState_INIT is used twice.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

This comment was marked as off-topic.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericsnowcurrently added 5 commits January 10, 2022 13:55
@ericsnowcurrently
Fold _PyThreadState_INIT into _PyInterpreterState_INIT.
5b34b42
@ericsnowcurrently
Fold _PyInterpreterState_INIT into _PyRuntimeState_INIT.
837bfe6
@ericsnowcurrently
Drop PyThreadState._preallocated.
ebefa35
@ericsnowcurrently
tstate -> initial_thread.
eb7a080
@ericsnowcurrently
Mirror the top-level structure in _preallocated.
1ef4a39
@ericsnowcurrently
ericsnowcurrently requested a review from markshannon January 11, 2022 00:16
ericsnowcurrently added 13 commits January 11, 2022 09:41
@ericsnowcurrently
Merge branch 'main' into preallocate-main
771e574
@ericsnowcurrently
Clarify some comments.
b5c43f1
@ericsnowcurrently
Fold in _PyRuntimeState._preallocated.
bfc3906
@ericsnowcurrently
Make "initial" const.
9b25c8b
@ericsnowcurrently
Fold in PyInterpreterState._preallocated.
e776c31
@ericsnowcurrently
Move the initial thread to the bottom of PyInterpreterState.
c6bda1f
@ericsnowcurrently
Split up _PyRuntimeState_INIT for less nesting and clutter.
8ab6261
@ericsnowcurrently
Fix a comment.
ff8de28
@ericsnowcurrently
Move the main interpreter to the bottom of _PyRuntimeState.
b28afb3
@ericsnowcurrently
Clarify some comments.
0d30300
@ericsnowcurrently
Add a comment for the _static field.
e193794
@ericsnowcurrently
Move the comments about pre-allocated data down.
e9f1262
@ericsnowcurrently
Fix a typo.
a6a6471
@ericsnowcurrently

ericsnowcurrently commented Jan 12, 2022

Copy link
Copy Markdown
Member Author

With basically all feedback addressed I plan on merging this soon, to unblock other changes I have waiting. If I missed something, I'd be glad to address it in a follow-up PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Jan 12, 2022 via email

Copy link
Copy Markdown
Member
Fine with me.
…
On Wed, Jan 12, 2022 at 2:56 PM Eric Snow ***@***.***> wrote: With basically all feedback addressed I plan on merging this soon, to unblock other changes I have waiting. If I missed something, I'd be glad to address it in a follow-up PR. — Reply to this email directly, view it on GitHub <#29883 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAWCWMXNEYDUC6YIRLTJAEDUVYBIXANCNFSM5JFJMYAA> . Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>. You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
-- --Guido van Rossum (python.org/~guido)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently
ericsnowcurrently merged commit ed57b36 into python:main Jan 12, 2022
@bedevere-bot bedevere-bot removed the awaiting change review label Jan 12, 2022
@ericsnowcurrently
ericsnowcurrently deleted the preallocate-main branch January 12, 2022 23:28
tacaswell added a commit to tacaswell/typed_ast that referenced this pull request Jan 25, 2022
@tacaswell
FIX: Account form stdbool.h being included in Python.h
9b849ca
As of CPython 3.11 (via python/cpython#29883) stdbool.h
is now included in Python.h so do attempt to redefine bool/true/false.
@tacaswell tacaswell mentioned this pull request Jan 25, 2022
FIX: Account form stdbool.h being included in Python.h python/typed_ast#184
Merged
srittau pushed a commit to python/typed_ast that referenced this pull request Jan 25, 2022
@tacaswell
FIX: Account form stdbool.h being included in Python.h (#184)
36110cf
As of CPython 3.11 (via python/cpython#29883) stdbool.h
is now included in Python.h so do attempt to redefine bool/true/false.
@ericsnowcurrently ericsnowcurrently mentioned this pull request Nov 8, 2022
Statically allocate interpreter states as much as possible. #90111
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@arhadthedev arhadthedev arhadthedev left review comments
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@markshannon markshannon

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@ericsnowcurrently @bedevere-bot @markshannon @gvanrossum @arhadthedev @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.