Skip to content

gh-148871: extend and improve LOAD_COMMON_CONSTANT#148971

Open
NekoAsakura wants to merge 4 commits intopython:mainfrom
NekoAsakura:gh-148871/load-common-constant
Open

gh-148871: extend and improve LOAD_COMMON_CONSTANT#148971
NekoAsakura wants to merge 4 commits intopython:mainfrom
NekoAsakura:gh-148871/load-common-constant

Conversation

@NekoAsakura
Copy link
Copy Markdown
Contributor

@NekoAsakura NekoAsakura commented Apr 24, 2026

Behaviour changes

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.

Python/bltinmodule.c - PyFilter_Type -
Python/bltinmodule.c - PyMap_Type -
Python/bltinmodule.c - PyZip_Type -
Python/bltinmodule.c - _PyBuiltin_All -
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.

I think this should be in the ignored.tsv files as we don't want to "fix" them.

@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. :')

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.

2 participants