Skip to content

GH-155728: Untrack const tuples - #156419

Open
sergey-miryanov wants to merge 8 commits into
python:mainfrom
sergey-miryanov:bug/155728-untrack-const-tuples
Open

sergey-miryanov wants to merge 8 commits into
python:mainfrom
sergey-miryanov:bug/155728-untrack-const-tuples

Conversation

@sergey-miryanov

@sergey-miryanov sergey-miryanov commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Comment thread Python/flowgraph.c Resolved
@sergey-miryanov sergey-miryanov added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Aug 26, 2026
@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

It is ready for review, could you please take a look?

@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

@Eclips4 Could you please take a look?

Comment thread Lib/test/test_tuple.py

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.

Add a test for subclasses, please

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.

Not sure actually whether Python-level subclass would catch this kind of bug (those probably are always tracked?)

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.

Yeah it seems that Python-level tuple subclasses are always tracked by PyType_GenericAlloc. It has to be a struct sequence created via PyStruct_SequenceNew (which doesn't track), something like this:

st = os.stat(os.curdir)
self.assertFalse(gc.is_tracked(st))
self._tracked_instantly(st + (1,))
self._tracked_instantly((1,) + st)
self._tracked_instantly(st * 2)
self._tracked_instantly(2 * st)

Comment thread Objects/tupleobject.c
Comment on lines +596 to +597
if (_PyObject_GC_IS_TRACKED(aa) || _PyObject_GC_IS_TRACKED(bb))
{

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
if (_PyObject_GC_IS_TRACKED(aa) || _PyObject_GC_IS_TRACKED(bb))
{
if (_PyObject_GC_MAY_BE_TRACKED(aa) || _PyObject_GC_MAY_BE_TRACKED(bb)) {

I suppose we can't use _PyObject_GC_IS_TRACKED here because of subclasses. Model like "If the inputs are untracked, their elements are untracked too" only holds for exact tuples. Plus PEP 7 nit

Comment thread Objects/tupleobject.c
Comment on lines +648 to +649
if (_PyObject_GC_IS_TRACKED(self))
{

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
if (_PyObject_GC_IS_TRACKED(self))
{
if (_PyObject_GC_MAY_BE_TRACKED(self)) {

Same here

Comment on lines +1 to +2
The flowgraph optimizer now untracks tuples from the GC when possible. Patch
by Sergey Miryanov.

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.

Let's also mention the changes in tupleobject.c since they are user-visible. E.g. "Now tuples produced by + and * are no longer tracked by the GC when none of their elements can be tracked".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants