FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-155728: Untrack const tuples by sergey-miryanov · Pull Request #156419 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 13 additions & 1 deletion Lib/test/test_tuple.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
24 changes: 24 additions & 0 deletions Python/flowgraph.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL