Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion 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)

Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ def test_repr(self):
def _not_tracked_instantly(self, t):
self.assertFalse(gc.is_tracked(t), t)

def _tracked_instantly(self, t):
self.assertTrue(gc.is_tracked(t), t)

# Checks that t is not tracked after GC collection.
def _not_tracked(self, t):
# Nested tuples can take several collections to untrack
Expand All @@ -323,10 +326,19 @@ def test_track_literals(self):
self._not_tracked_instantly((1,))
self._not_tracked_instantly((1, 2))
self._not_tracked_instantly((1, 2, "a"))
self._not_tracked_instantly((1, 2) * 5)
self._not_tracked_instantly((12, 10**10, 'a_' * 100))
self._not_tracked_instantly((object(),))

# Test for _PyTuple_Concat
self._not_tracked_instantly((1, 2) + (2, 3, 4))
self._not_tracked_instantly((x, y,) + (2, 3))
self._tracked_instantly((3, 4, 5, [],) + (2, 3))

# Test for _PyTuple_Repeat
self._not_tracked_instantly((1, 2) * 5)
self._not_tracked_instantly((1, x) * 5)
self._tracked_instantly((1, []) * 5)

self._not_tracked(((1, x), y, (2, 3)))
self._not_tracked((1, 2, (None, True, False, ()), int))
self._not_tracked((object(), ()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The flowgraph optimizer now untracks tuples from the GC when possible. Patch
by Sergey Miryanov.
Comment on lines +1 to +2

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".

10 changes: 8 additions & 2 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ _PyTuple_Concat(PyObject *aa, PyObject *bb)
dest[i] = Py_NewRef(v);
}

_PyObject_GC_TRACK(np);
if (_PyObject_GC_IS_TRACKED(aa) || _PyObject_GC_IS_TRACKED(bb))
{
Comment on lines +596 to +597

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

_PyObject_GC_TRACK(np);
}
return (PyObject *)np;
}

Expand Down Expand Up @@ -642,7 +645,10 @@ _PyTuple_Repeat(PyObject *self, Py_ssize_t n)
_Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
}
_PyObject_GC_TRACK(np);
if (_PyObject_GC_IS_TRACKED(self))
{
Comment on lines +648 to +649

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

_PyObject_GC_TRACK(np);
}
return (PyObject *) np;
}

Expand Down
24 changes: 24 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,8 @@ fold_tuple_of_constants(basicblock *bb, int i, PyObject *consts,
if (const_tuple == NULL) {
return ERROR;
}
PyObject_GC_UnTrack(const_tuple);
bool track_tuple = false;

for (int i = 0; i < seq_size; i++) {
cfg_instr *inst = const_instrs[i];
Expand All @@ -1562,6 +1564,12 @@ fold_tuple_of_constants(basicblock *bb, int i, PyObject *consts,
return ERROR;
}
PyTuple_SET_ITEM(const_tuple, i, element);
if (!track_tuple && PyObject_GC_IsTracked(element)) {
track_tuple = true;
}
}
if (track_tuple) {
_PyObject_GC_TRACK(const_tuple);
}

nop_out(const_instrs, seq_size);
Expand Down Expand Up @@ -1632,6 +1640,8 @@ fold_constant_seq_into_load_const(basicblock *bb, int i,
if (newconst == NULL) {
return ERROR;
}
PyObject_GC_UnTrack(newconst);
bool track_tuple = false;

int newpos_start = expected_append ? i - 1 : i;
for (int newpos = newpos_start; newpos >= pos; newpos--) {
Expand All @@ -1647,9 +1657,15 @@ fold_constant_seq_into_load_const(basicblock *bb, int i,
}
assert(consts_found > 0);
PyTuple_SET_ITEM(newconst, --consts_found, constant);
if (!track_tuple && PyObject_GC_IsTracked(constant)) {
track_tuple = true;
}
}
nop_out(&instr, 1);
}
if (track_tuple) {
_PyObject_GC_TRACK(newconst);
}
assert(consts_found == 0);

if (build_op == BUILD_SET) {
Expand Down Expand Up @@ -1726,6 +1742,8 @@ optimize_lists_and_sets(basicblock *bb, int i, int nextop,
if (const_result == NULL) {
return ERROR;
}
PyObject_GC_UnTrack(const_result);
bool track_tuple = false;

for (int i = 0; i < seq_size; i++) {
cfg_instr *inst = const_instrs[i];
Expand All @@ -1736,6 +1754,12 @@ optimize_lists_and_sets(basicblock *bb, int i, int nextop,
return ERROR;
}
PyTuple_SET_ITEM(const_result, i, element);
if (!track_tuple && PyObject_GC_IsTracked(element)) {
track_tuple = true;
}
}
if (track_tuple) {
_PyObject_GC_TRACK(const_result);
}

if (instr->i_opcode == BUILD_SET) {
Expand Down
Loading