Skip to content

gh-148871: extend and improve LOAD_COMMON_CONSTANT#148971

Merged
markshannon merged 12 commits intopython:mainfrom
NekoAsakura:gh-148871/load-common-constant
May 2, 2026
Merged

gh-148871: extend and improve LOAD_COMMON_CONSTANT#148971
markshannon merged 12 commits intopython:mainfrom
NekoAsakura:gh-148871/load-common-constant

Conversation

@NekoAsakura
Copy link
Copy Markdown
Contributor

@NekoAsakura NekoAsakura commented Apr 24, 2026

Behaviour changes (Reverted)

Change main branch
1 all.__self__ / any.__self__ <module 'builtins'> None
2 gc.is_tracked(all) / gc.is_tracked(any) True False

Copy link
Copy Markdown
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Mostly looks good.

You need to take care distinguishing between process-wide data and per-interpreter data. See the comments below.

Comment thread Objects/methodobject.c Outdated
meth_getsets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you remove this list of zeros and use a named initializer: .tp_is_gc = cfunction_is_gc

Comment thread Python/flowgraph.c Outdated
return PyLong_FromLong(oparg);
}
if (opcode == LOAD_COMMON_CONSTANT) {
switch (oparg) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
switch (oparg) {
return load_common_const(oparg);

All these objects are immortal, so no need to worry about reference counts

Comment thread Python/optimizer_bytecodes.c Outdated
op(_LOAD_COMMON_CONSTANT, (-- value)) {
assert(oparg < NUM_COMMON_CONSTANTS);
PyObject *val = _PyInterpreterState_GET()->common_consts[oparg];
PyObject *val;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why aren't you using the constants table?

Comment thread Python/ceval_macros.h Outdated
static inline _PyStackRef
load_common_constant(unsigned int oparg)
{
switch (oparg) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this needed? These values should be in the table

Comment thread Python/bltinmodule.c Outdated
.vectorcall = _PyCFunction_vectorcall_O,
};

PyObject *_PyCommonConsts[NUM_COMMON_CONSTANTS];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For this to be process-wide (not per-interpreter) PyExc_AssertionError and PyExc_NotImplementedError will need to be statically allocated.

Doing that might be complex, so maybe keep the constants per-interpreter for this PR, and make everything statically allocation in a later PR?

Comment thread Python/pylifecycle.c Outdated
(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS - 1];
for (int i = 0; i < NUM_COMMON_CONSTANTS; i++) {
assert(_PyCommonConsts[i] != NULL);
assert(_Py_IsImmortal(_PyCommonConsts[i]));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
assert(_Py_IsImmortal(_PyCommonConsts[i]));
assert(_Py_IsStaticImmortal(_PyCommonConsts[i]));

If _PyCommonConsts is to be statically allocated.

Comment thread Tools/c-analyzer/cpython/globals-to-fix.tsv Outdated
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 25, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

@NekoAsakura
Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed the lot. :')

@markshannon
Copy link
Copy Markdown
Member

This looks good, except the changes to any and all.

Sorry to go and forth on this, but I think we shouldn't make any and all static after all.

I'm worried that the changes to __self__ will break something. Maybe the GC change as well, but that seems less likely. Also, since the common constants table is still attached to the interpreter state, there is no performance advantage in doing so.

@NekoAsakura
Copy link
Copy Markdown
Contributor Author

Sound call. Reverted. Thanks again for the review.

@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32502817 | 📁 Comparing b017ae9 against main (95559d2)

  🔍 Preview build  

61 files changed · + 1 added · ± 60 modified

+ Added

± Modified

@markshannon markshannon self-requested a review May 2, 2026 18:57
Copy link
Copy Markdown
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

This fixes the compiler and selection of constants.

There are performance improvements to be made, but that can be done in another PR.

@markshannon markshannon merged commit 7c9ad27 into python:main May 2, 2026
74 of 75 checks passed
@markshannon
Copy link
Copy Markdown
Member

#148871 (comment)

@NekoAsakura NekoAsakura deleted the gh-148871/load-common-constant branch May 2, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants