Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-139389: Do not track immutable tuples in PyTuple_Pack#139390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
sergey-miryanov wants to merge6 commits intopython:main
base:main
Choose a base branch
Loading
fromsergey-miryanov:139389-donot-track-immutable-tuples
Draft
Show file tree
Hide file tree
Changes fromall 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
9 changes: 9 additions & 0 deletionsInclude/internal/pycore_interp_structs.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,6 +165,7 @@ typedef struct {
// Lowest two bits are used for flags documented later.
// Those bits are made available by the struct's minimum alignment.
uintptr_t _gc_prev;
uintptr_t _visited;
} PyGC_Head;

#define _PyGC_Head_UNUSED PyGC_Head
Expand All@@ -181,6 +182,8 @@ struct gc_collection_stats {
Py_ssize_t collected;
/* total number of uncollectable objects (put into gc.garbage) */
Py_ssize_t uncollectable;
Py_ssize_t tracked_tuples;
Py_ssize_t untracked_tuples;
};

/* Running stats per generation */
Expand All@@ -191,6 +194,12 @@ struct gc_generation_stats {
Py_ssize_t collected;
/* total number of uncollectable objects (put into gc.garbage) */
Py_ssize_t uncollectable;
Py_ssize_t tracked_tuples;
Py_ssize_t total_tracked_tuples;
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
4 changes: 2 additions & 2 deletionsInclude/internal/pycore_object.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -937,8 +937,8 @@ extern int _PyType_CacheInitForSpecialization(PyHeapTypeObject *type,
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-1)
# define MANAGED_WEAKREF_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-2)
#else
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-3)
# define MANAGED_WEAKREF_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-4)
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-4)
# define MANAGED_WEAKREF_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-5)
#endif

typedef union {
Expand Down
2 changes: 1 addition & 1 deletionInclude/internal/pycore_tuple.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,7 +11,7 @@ extern "C" {
#include "pycore_object.h" // _PyObject_GC_IS_TRACKED
#include "pycore_structs.h" // _PyStackRef

externvoid _PyTuple_MaybeUntrack(PyObject *);
externint _PyTuple_MaybeUntrack(PyObject *);
extern void _PyTuple_DebugMallocStats(FILE *out);

/* runtime lifecycle */
Expand Down
92 changes: 82 additions & 10 deletionsObjects/tupleobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,44 @@ 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@@ -46,6 +84,7 @@ 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@@ -57,6 +96,7 @@ 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@@ -68,6 +108,7 @@ tuple_alloc(Py_ssize_t size)
static inline PyObject *
tuple_get_empty(void)
{
_count_tuple(0);
return (PyObject *)&_Py_SINGLETON(tuple_empty);
}

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

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

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

PyObject *
Expand All@@ -175,12 +217,18 @@ PyTuple_Pack(Py_ssize_t n, ...)
return NULL;
}
items = result->ob_item;
bool track = false;
for (i = 0; i < n; i++) {
o = va_arg(vargs, PyObject *);
items[i] = Py_NewRef(o);
if (!track && _PyObject_GC_MAY_BE_TRACKED(items[i])) {
track = true;
}
}
va_end(vargs);
_PyObject_GC_TRACK(result);
if (track) {
_PyObject_GC_TRACK(result);
}
return (PyObject *)result;
}

Expand DownExpand Up@@ -376,12 +424,18 @@ PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
if (tuple == NULL) {
return NULL;
}
bool track = false;
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = src[i];
dst[i] = Py_NewRef(item);
if (!track && _PyObject_GC_MAY_BE_TRACKED(dst[i])) {
track = true;
}
}
if (track) {
_PyObject_GC_TRACK(tuple);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
}

Expand All@@ -395,11 +449,17 @@ _PyTuple_FromStackRefStealOnSuccess(const _PyStackRef *src, Py_ssize_t n)
if (tuple == NULL) {
return NULL;
}
bool track = false;
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
dst[i] = PyStackRef_AsPyObjectSteal(src[i]);
if (!track && _PyObject_GC_MAY_BE_TRACKED(dst[i])) {
track = true;
}
}
if (track) {
_PyObject_GC_TRACK(tuple);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
}

Expand All@@ -416,12 +476,18 @@ _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n)
}
return NULL;
}
bool track = false;
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = src[i];
dst[i] = item;
if (!track && _PyObject_GC_MAY_BE_TRACKED(item)) {
track = true;
}
}
if (track) {
_PyObject_GC_TRACK(tuple);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
}

Expand DownExpand Up@@ -494,7 +560,9 @@ tuple_concat(PyObject *aa, PyObject *bb)
dest[i] = Py_NewRef(v);
}

_PyObject_GC_TRACK(np);
if (_PyObject_GC_IS_TRACKED(a) || _PyObject_GC_IS_TRACKED(b)) {
_PyObject_GC_TRACK(np);
}
return (PyObject *)np;
}

Expand DownExpand Up@@ -543,7 +611,9 @@ tuple_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(a)) {
_PyObject_GC_TRACK(np);
}
return (PyObject *) np;
}

Expand DownExpand Up@@ -821,7 +891,9 @@ tuple_subscript(PyObject *op, PyObject* item)
dest[i] = it;
}

_PyObject_GC_TRACK(result);
if (_PyObject_GC_IS_TRACKED(self)) {
_PyObject_GC_TRACK(result);
}
return (PyObject *)result;
}
}
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2026 Movatter.jp