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
7 changes: 6 additions & 1 deletion Lib/test/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,15 @@ 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))

# Test for _PyTuple_Repeat
self._not_tracked_instantly((1, 2) * 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.
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))
{
_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))
{
_PyObject_GC_TRACK(np);
}
return (PyObject *) np;
}

Expand Down
4 changes: 4 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "pycore_opcode_metadata.h" // OPCODE_HAS_ARG, etc
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_stackref.h" // PyStackRef_AsPyObjectBorrow()
#include "pycore_tuple.h" // _PyTuple_MaybeUntrack()

#include <stdbool.h>

Expand Down Expand Up @@ -1563,6 +1564,7 @@ fold_tuple_of_constants(basicblock *bb, int i, PyObject *consts,
}
PyTuple_SET_ITEM(const_tuple, i, element);
}
_PyTuple_MaybeUntrack(const_tuple);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could avoid a second pass there, just like in marshal, by checking each element. But I'm not sure we should do it, since it adds a bit of complexity.

I'm open to change this, if it is worth.

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.

IMO, we should do the same logic in each case, because we don't want to expose partially constructed tuples to the GC.
@markshannon, WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, will fix this.


nop_out(const_instrs, seq_size);
return instr_make_load_const(instr, const_tuple, consts, const_cache, consts_index);
Expand Down Expand Up @@ -1650,6 +1652,7 @@ fold_constant_seq_into_load_const(basicblock *bb, int i,
}
nop_out(&instr, 1);
}
_PyTuple_MaybeUntrack(newconst);
assert(consts_found == 0);

if (build_op == BUILD_SET) {
Expand Down Expand Up @@ -1737,6 +1740,7 @@ optimize_lists_and_sets(basicblock *bb, int i, int nextop,
}
PyTuple_SET_ITEM(const_result, i, element);
}
_PyTuple_MaybeUntrack(const_result);

if (instr->i_opcode == BUILD_SET) {
PyObject *frozenset = PyFrozenSet_New(const_result);
Expand Down
Loading