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

gh-139951: Do not track immutable tuples in GC by efimov-mikhail · Pull Request #140204 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (2) All 2 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
Prev Previous commit
Next Next commit
Remove statistics
  • Loading branch information
efimov-mikhail committed Oct 16, 2025
commit b281fe82dd5e6a4da578c29dc90635482745ecd4
5 changes: 0 additions & 5 deletions Include/internal/pycore_interp_structs.h
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 @@ -181,7 +181,6 @@ struct gc_collection_stats {
Py_ssize_t collected;
/* total number of uncollectable objects (put into gc.garbage) */
Py_ssize_t uncollectable;
Py_ssize_t untracked_tuples;
};

/* Running stats per generation */
Expand All @@ -192,10 +191,6 @@ struct gc_generation_stats {
Py_ssize_t collected;
/* total number of uncollectable objects (put into gc.garbage) */
Py_ssize_t uncollectable;
Py_ssize_t untracked_tuples;
Py_ssize_t total_untracked_tuples;
Py_ssize_t total_tuples;
Py_ssize_t tuples_by_size[33];
};

enum _GCPhase {
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_tuple.h
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 @@ -11,7 +11,7 @@ extern "C" {
#include "pycore_object.h" // _PyObject_GC_IS_TRACKED
#include "pycore_structs.h" // _PyStackRef

extern int _PyTuple_MaybeUntrack(PyObject *);
extern void _PyTuple_MaybeUntrack(PyObject *);
extern void _PyTuple_DebugMallocStats(FILE *out);

/* runtime lifecycle */
Expand Down
50 changes: 4 additions & 46 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 @@ -22,44 +22,6 @@ class tuple "PyTupleObject *" "&PyTuple_Type"

static inline int maybe_freelist_push(PyTupleObject *);

static uint8_t
_log2_int(Py_ssize_t size)
{
if (size == 0) {
return 0;
}

const uint64_t b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000};
const uint64_t S[] = {1, 2, 4, 8, 16, 32};

int64_t v = size;
uint8_t r = 0; // result of log2(v) will go here
for (int i = 5; i >= 0; i--) // unroll for speed...
{
if (v & b[i])
{
v >>= S[i];
r |= S[i];
}
}

#ifdef Py_DEBUG
uint8_t x = (uint8_t)log2((double)size);
assert(x == r);
#endif

return r + 1;
}

static void
_count_tuple(Py_ssize_t size)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
interp->gc.generation_stats[0].total_tuples += 1;
uint8_t size_index = _log2_int(size);
interp->gc.generation_stats[0].tuples_by_size[size_index] += 1;
}


/* Allocate an uninitialized tuple object. Before making it public, following
steps must be done:
Expand All @@ -84,7 +46,6 @@ tuple_alloc(Py_ssize_t size)
PyTupleObject *op = _Py_FREELIST_POP(PyTupleObject, tuples[index]);
if (op != NULL) {
_PyTuple_RESET_HASH_CACHE(op);
_count_tuple(size);
return op;
}
}
Expand All @@ -96,7 +57,6 @@ tuple_alloc(Py_ssize_t size)
PyTupleObject *result = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
if (result != NULL) {
_PyTuple_RESET_HASH_CACHE(result);
_count_tuple(size);
}
return result;
}
Expand All @@ -108,7 +68,6 @@ tuple_alloc(Py_ssize_t size)
static inline PyObject *
tuple_get_empty(void)
{
_count_tuple(0);
return (PyObject *)&_Py_SINGLETON(tuple_empty);
}

Expand Down Expand Up @@ -175,14 +134,14 @@ PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
return 0;
}

int
void
_PyTuple_MaybeUntrack(PyObject *op)
{
PyTupleObject *t;
Py_ssize_t i, n;

if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
return 0;
return;
t = (PyTupleObject *) op;
n = Py_SIZE(t);
for (i = 0; i < n; i++) {
Expand All @@ -192,10 +151,9 @@ _PyTuple_MaybeUntrack(PyObject *op)
them yet. */
if (!elt ||
_PyObject_GC_MAY_BE_TRACKED(elt))
return 0;
return;
}
_PyObject_GC_UNTRACK(op);
return 1;
}

PyObject *
Expand Down Expand Up @@ -414,7 +372,7 @@ tuple_item(PyObject *op, Py_ssize_t i)
}

PyObject *
PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
{
if (n == 0) {
return tuple_get_empty();
Expand Down
32 changes: 6 additions & 26 deletions Python/gc.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 @@ -753,20 +753,18 @@ move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
* and is much faster than a more complex approach that
* would untrack all relevant tuples.
*/
static Py_ssize_t
static void
untrack_tuples(PyGC_Head *head)
{
Py_ssize_t untracked = 0;
PyGC_Head *gc = GC_NEXT(head);
while (gc != head) {
PyObject *op = FROM_GC(gc);
PyGC_Head *next = GC_NEXT(gc);
if (PyTuple_CheckExact(op)) {
untracked += _PyTuple_MaybeUntrack(op);
_PyTuple_MaybeUntrack(op);
}
gc = next;
}
return untracked;
}

/* Return true if object has a pre-PEP 442 finalization method. */
Expand Down Expand Up @@ -1378,7 +1376,7 @@ gc_collect_young(PyThreadState *tstate,
validate_spaces(gcstate);
PyGC_Head *young = &gcstate->young.head;
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
stats->untracked_tuples += untrack_tuples(young);
untrack_tuples(young);
GC_STAT_ADD(0, collections, 1);

PyGC_Head survivors;
Expand Down Expand Up @@ -1656,7 +1654,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
GC_STAT_ADD(1, collections, 1);
GCState *gcstate = &tstate->interp->gc;
gcstate->work_to_do += assess_work_to_do(gcstate);
stats->untracked_tuples += untrack_tuples(&gcstate->young.head);
untrack_tuples(&gcstate->young.head);
if (gcstate->phase == GC_PHASE_MARK) {
Py_ssize_t objects_marked = mark_at_start(tstate);
GC_STAT_ADD(1, objects_transitively_reachable, objects_marked);
Expand Down Expand Up @@ -1718,7 +1716,7 @@ gc_collect_full(PyThreadState *tstate,
PyGC_Head *young = &gcstate->young.head;
PyGC_Head *pending = &gcstate->old[gcstate->visited_space^1].head;
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
stats->untracked_tuples += untrack_tuples(young);
untrack_tuples(young);
/* merge all generations into visited */
gc_list_merge(young, pending);
gc_list_validate_space(pending, 1-gcstate->visited_space);
Expand Down Expand Up @@ -1758,7 +1756,7 @@ gc_collect_region(PyThreadState *tstate,
gc_list_init(&unreachable);
deduce_unreachable(from, &unreachable);
validate_consistent_old_space(from);
stats->untracked_tuples += untrack_tuples(from);
untrack_tuples(from);

/* Move reachable objects to next generation. */
validate_consistent_old_space(to);
Expand Down Expand Up @@ -2100,30 +2098,12 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
default:
Py_UNREACHABLE();
}
gcstate->generation_stats[generation].untracked_tuples += stats.untracked_tuples;
gcstate->generation_stats[0].total_untracked_tuples += stats.untracked_tuples;
if (PyDTrace_GC_DONE_ENABLED()) {
PyDTrace_GC_DONE(stats.uncollectable + stats.collected);
}
if (reason != _Py_GC_REASON_SHUTDOWN) {
invoke_gc_callback(gcstate, "stop", generation, &stats);
}
else if (false) {
FILE *out = stderr;

fprintf(out, "GC[%d] total tuples : %zd\n", 0, gcstate->generation_stats[0].total_tuples);
fprintf(out, "GC[%d] total untracked_tuples : %zd\n", 0, gcstate->generation_stats[0].total_untracked_tuples);
for (int i = 0; i < 33; i++) {
fprintf(out, "GC[%d] by size %d : %zd\n", 0, i, gcstate->generation_stats[0].tuples_by_size[i]);
}

for (int i = 0; i < NUM_GENERATIONS; i++) {
fprintf(out, "GC[%d] collections : %zd\n", i, gcstate->generation_stats[i].collections);
fprintf(out, "GC[%d] collected : %zd\n", i, gcstate->generation_stats[i].collected);
fprintf(out, "GC[%d] uncollectable : %zd\n", i, gcstate->generation_stats[i].uncollectable);
fprintf(out, "GC[%d] untracked_tuples: %zd\n", i, gcstate->generation_stats[i].untracked_tuples);
}
}
_PyErr_SetRaisedException(tstate, exc);
GC_STAT_ADD(generation, objects_collected, stats.collected);
#ifdef Py_STATS
Expand Down
Loading

Back | FazBrowse Home | New Git URL