GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-120974: Use common freelist code in asyncio by colesbury · Pull Request #122132 · python/cpython

gh-120974: Use common freelist code in asyncio #122132

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.

Sign up for GitHub

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

Already on GitHub? Sign in to your account

Merged
kumaraditya303 merged 1 commit into python:main from colesbury:gh-120974-asyncio-freelist
Jul 23, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-120974: Use common freelist code in asyncio #122132

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
21cdcd2
gh-120974: Use common freelist code in asyncio
colesbury Jul 17, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 Include/internal/pycore_freelist_state.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
# define Py_contexts_MAXFREELIST 255
# define Py_async_gens_MAXFREELIST 80
# define Py_async_gen_asends_MAXFREELIST 80
# define Py_futureiters_MAXFREELIST 255
# define Py_object_stack_chunks_MAXFREELIST 4
#else
# define PyTuple_MAXSAVESIZE 0
Expand Down Expand Up @@ -47,6 +48,7 @@ struct _Py_freelists {
struct _Py_freelist contexts;
struct _Py_freelist async_gens;
struct _Py_freelist async_gen_asends;
struct _Py_freelist futureiters;
struct _Py_freelist object_stack_chunks;
#else
char _unused; // Empty structs are not allowed.
Expand Down
73 changes: 4 additions & 69 deletions 73 Modules/_asynciomodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
#include "pycore_freelist.h" // _Py_FREELIST_POP()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_object.h" // _Py_SetImmortalUntracked
Expand Down Expand Up @@ -75,8 +76,6 @@ typedef struct {
#define Future_Check(state, obj) PyObject_TypeCheck(obj, state->FutureType)
#define Task_Check(state, obj) PyObject_TypeCheck(obj, state->TaskType)

#define FI_FREELIST_MAXLEN 255

#ifdef Py_GIL_DISABLED
# define ASYNCIO_STATE_LOCK(state) PyMutex_Lock(&state->mutex)
# define ASYNCIO_STATE_UNLOCK(state) PyMutex_Unlock(&state->mutex)
Expand Down Expand Up @@ -138,11 +137,6 @@ typedef struct {
/* Counter for autogenerated Task names */
uint64_t task_name_counter;

#ifndef Py_GIL_DISABLED
futureiterobject *fi_freelist;
Py_ssize_t fi_freelist_len;
#endif

/* Linked-list of all tasks which are instances of asyncio.Task or subclasses
of it. Third party tasks implementations which don't inherit from
asyncio.Task are tracked separately using the 'non_asyncio_tasks' WeakSet.
Expand Down Expand Up @@ -1584,24 +1578,7 @@ FutureIter_dealloc(futureiterobject *it)
PyObject_GC_UnTrack(it);
tp->tp_clear((PyObject *)it);

#ifndef Py_GIL_DISABLED
// GH-115874: We can't use PyType_GetModuleByDef here as the type might have
// already been cleared, which is also why we must check if ht_module != NULL.
PyObject *module = ((PyHeapTypeObject*)tp)->ht_module;
asyncio_state *state = NULL;
if (module && _PyModule_GetDef(module) == &_asynciomodule) {
state = get_asyncio_state(module);
}

// TODO GH-121621: This should be moved to thread state as well.
if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
state->fi_freelist_len++;
it->future = (FutureObj*) state->fi_freelist;
state->fi_freelist = it;
}
else
#endif
{
if (!_Py_FREELIST_PUSH(futureiters, it, Py_futureiters_MAXFREELIST)) {
PyObject_GC_Del(it);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -1805,17 +1782,8 @@ future_new_iter(PyObject *fut)
asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
ENSURE_FUTURE_ALIVE(state, fut)

#ifndef Py_GIL_DISABLED
if (state->fi_freelist_len) {
state->fi_freelist_len--;
it = state->fi_freelist;
state->fi_freelist = (futureiterobject*) it->future;
it->future = NULL;
_Py_NewReference((PyObject*) it);
}
else
#endif
{
it = _Py_FREELIST_POP(futureiterobject, futureiters);
if (it == NULL) {
it = PyObject_GC_New(futureiterobject, state->FutureIterType);
if (it == NULL) {
return NULL;
Expand Down Expand Up @@ -3683,27 +3651,6 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
return tasks;
}

static void
module_free_freelists(asyncio_state *state)
{
#ifndef Py_GIL_DISABLED
PyObject *next;
PyObject *current;

next = (PyObject*) state->fi_freelist;
while (next != NULL) {
assert(state->fi_freelist_len > 0);
state->fi_freelist_len--;

current = next;
next = (PyObject*) ((futureiterobject*) current)->future;
PyObject_GC_Del(current);
}
assert(state->fi_freelist_len == 0);
state->fi_freelist = NULL;
#endif
}

static int
module_traverse(PyObject *mod, visitproc visit, void *arg)
{
Expand Down Expand Up @@ -3732,16 +3679,6 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)

Py_VISIT(state->context_kwname);

#ifndef Py_GIL_DISABLED
// Visit freelist.
PyObject *next = (PyObject*) state->fi_freelist;
while (next != NULL) {
PyObject *current = next;
Py_VISIT(current);
next = (PyObject*) ((futureiterobject*) current)->future;
}
#endif

return 0;
}

Expand Down Expand Up @@ -3773,8 +3710,6 @@ module_clear(PyObject *mod)

Py_CLEAR(state->context_kwname);

module_free_freelists(state);

return 0;
}

Expand Down
5 changes: 4 additions & 1 deletion 5 Objects/object.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,9 @@ static void
free_object(void *obj)
{
PyObject *op = (PyObject *)obj;
Py_TYPE(op)->tp_free(op);
PyTypeObject *tp = Py_TYPE(op);
tp->tp_free(op);
Py_DECREF(tp);
Comment thread
kumaraditya303 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

}

#endif
Expand All @@ -851,6 +853,7 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
clear_freelist(&freelists->contexts, is_finalization, free_object);
clear_freelist(&freelists->async_gens, is_finalization, free_object);
clear_freelist(&freelists->async_gen_asends, is_finalization, free_object);
clear_freelist(&freelists->futureiters, is_finalization, free_object);
if (is_finalization) {
// Only clear object stack chunks during finalization. We use object
// stacks during GC, so emptying the free-list is counterproductive.
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.