From 8db8d7118e1ef22bc7cdc3d8b657bc10c22c2fd6 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 17 Feb 2024 10:03:10 +0900 Subject: [PATCH] =?UTF-8?q?gh-111968:=20Split=20=5FPy=5Fasync=5Fgen=5Fasen?= =?UTF-8?q?d=5Ffreelist=20out=20of=20=5FPy=5Fasync=5Fgen=5Ffr=E2=80=A6=20(?= =?UTF-8?q?gh-115546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Include/internal/pycore_freelist.h | 13 +++++--- Objects/genobject.c | 51 ++++++++++++++++++------------ 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/Include/internal/pycore_freelist.h b/Include/internal/pycore_freelist.h index 9900ce98037..e684e084b8b 100644 --- a/Include/internal/pycore_freelist.h +++ b/Include/internal/pycore_freelist.h @@ -105,11 +105,15 @@ struct _Py_async_gen_freelist { fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend are short-living objects that are instantiated for every __anext__() call. */ - struct _PyAsyncGenWrappedValue* value_freelist[_PyAsyncGen_MAXFREELIST]; - int value_numfree; + struct _PyAsyncGenWrappedValue* items[_PyAsyncGen_MAXFREELIST]; + int numfree; +#endif +}; - struct PyAsyncGenASend* asend_freelist[_PyAsyncGen_MAXFREELIST]; - int asend_numfree; +struct _Py_async_gen_asend_freelist { +#ifdef WITH_FREELISTS + struct PyAsyncGenASend* items[_PyAsyncGen_MAXFREELIST]; + int numfree; #endif }; @@ -129,6 +133,7 @@ struct _Py_object_freelists { struct _Py_slice_freelist slices; struct _Py_context_freelist contexts; struct _Py_async_gen_freelist async_gens; + struct _Py_async_gen_asend_freelist async_gen_asends; struct _Py_object_stack_freelist object_stacks; }; diff --git a/Objects/genobject.c b/Objects/genobject.c index a1b6db1b588..8d1dbb72ba9 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -1635,6 +1635,13 @@ get_async_gen_freelist(void) struct _Py_object_freelists *freelists = _Py_object_freelists_GET(); return &freelists->async_gens; } + +static struct _Py_async_gen_asend_freelist * +get_async_gen_asend_freelist(void) +{ + struct _Py_object_freelists *freelists = _Py_object_freelists_GET(); + return &freelists->async_gen_asends; +} #endif @@ -1659,25 +1666,27 @@ void _PyAsyncGen_ClearFreeLists(struct _Py_object_freelists *freelist_state, int is_finalization) { #ifdef WITH_FREELISTS - struct _Py_async_gen_freelist *state = &freelist_state->async_gens; + struct _Py_async_gen_freelist *freelist = &freelist_state->async_gens; - while (state->value_numfree > 0) { + while (freelist->numfree > 0) { _PyAsyncGenWrappedValue *o; - o = state->value_freelist[--state->value_numfree]; + o = freelist->items[--freelist->numfree]; assert(_PyAsyncGenWrappedValue_CheckExact(o)); PyObject_GC_Del(o); } - while (state->asend_numfree > 0) { + struct _Py_async_gen_asend_freelist *asend_freelist = &freelist_state->async_gen_asends; + + while (asend_freelist->numfree > 0) { PyAsyncGenASend *o; - o = state->asend_freelist[--state->asend_numfree]; + o = asend_freelist->items[--asend_freelist->numfree]; assert(Py_IS_TYPE(o, &_PyAsyncGenASend_Type)); PyObject_GC_Del(o); } if (is_finalization) { - state->value_numfree = -1; - state->asend_numfree = -1; + freelist->numfree = -1; + asend_freelist->numfree = -1; } #endif } @@ -1726,11 +1735,11 @@ async_gen_asend_dealloc(PyAsyncGenASend *o) Py_CLEAR(o->ags_gen); Py_CLEAR(o->ags_sendval); #ifdef WITH_FREELISTS - struct _Py_async_gen_freelist *async_gen_freelist = get_async_gen_freelist(); - if (async_gen_freelist->asend_numfree >= 0 && async_gen_freelist->asend_numfree < _PyAsyncGen_MAXFREELIST) { + struct _Py_async_gen_asend_freelist *freelist = get_async_gen_asend_freelist(); + if (freelist->numfree >= 0 && freelist->numfree < _PyAsyncGen_MAXFREELIST) { assert(PyAsyncGenASend_CheckExact(o)); _PyGC_CLEAR_FINALIZED((PyObject *)o); - async_gen_freelist->asend_freelist[async_gen_freelist->asend_numfree++] = o; + freelist->items[freelist->numfree++] = o; } else #endif @@ -1896,10 +1905,10 @@ async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval) { PyAsyncGenASend *o; #ifdef WITH_FREELISTS - struct _Py_async_gen_freelist *async_gen_freelist = get_async_gen_freelist(); - if (async_gen_freelist->asend_numfree > 0) { - async_gen_freelist->asend_numfree--; - o = async_gen_freelist->asend_freelist[async_gen_freelist->asend_numfree]; + struct _Py_async_gen_asend_freelist *freelist = get_async_gen_asend_freelist(); + if (freelist->numfree > 0) { + freelist->numfree--; + o = freelist->items[freelist->numfree]; _Py_NewReference((PyObject *)o); } else @@ -1931,10 +1940,10 @@ async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o) _PyObject_GC_UNTRACK((PyObject *)o); Py_CLEAR(o->agw_val); #ifdef WITH_FREELISTS - struct _Py_async_gen_freelist *async_gen_freelist = get_async_gen_freelist(); - if (async_gen_freelist->value_numfree >= 0 && async_gen_freelist->value_numfree < _PyAsyncGen_MAXFREELIST) { + struct _Py_async_gen_freelist *freelist = get_async_gen_freelist(); + if (freelist->numfree >= 0 && freelist->numfree < _PyAsyncGen_MAXFREELIST) { assert(_PyAsyncGenWrappedValue_CheckExact(o)); - async_gen_freelist->value_freelist[async_gen_freelist->value_numfree++] = o; + freelist->items[freelist->numfree++] = o; OBJECT_STAT_INC(to_freelist); } else @@ -2004,10 +2013,10 @@ _PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val) assert(val); #ifdef WITH_FREELISTS - struct _Py_async_gen_freelist *async_gen_freelist = get_async_gen_freelist(); - if (async_gen_freelist->value_numfree > 0) { - async_gen_freelist->value_numfree--; - o = async_gen_freelist->value_freelist[async_gen_freelist->value_numfree]; + struct _Py_async_gen_freelist *freelist = get_async_gen_freelist(); + if (freelist->numfree > 0) { + freelist->numfree--; + o = freelist->items[freelist->numfree]; OBJECT_STAT_INC(from_freelist); assert(_PyAsyncGenWrappedValue_CheckExact(o)); _Py_NewReference((PyObject*)o);