Skip to content

Revert "bpo-40521: Remove freelist from collections.deque() (GH-21073)" #24944

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Changes from all 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
23 changes: 21 additions & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,23 @@ static PyTypeObject deque_type;
#define CHECK_NOT_END(link)
#endif

/* A simple freelisting scheme is used to minimize calls to the memory
allocator. It accommodates common use cases where new blocks are being
added at about the same rate as old blocks are being freed.
*/

#define MAXFREEBLOCKS 16
static Py_ssize_t numfreeblocks = 0;
static block *freeblocks[MAXFREEBLOCKS];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you define the freelist in PyInterpreterState to make it per-interpreter, and so compatible with subinterpreters?

Copy link
Contributor Author

@rhettinger rhettinger Mar 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subinterpreters PEP is not approved a likely won't be for Python 3.10, so I don't want to pay that cost now. These macros are used heavily throughout the rest of the module and are performance sensitive. For Python 3.11 and later, this can be reapplied. Even then, it would probably be better to remove the freelists entirely than to add fenced logic to get the current interpreter.


static block *
newblock(void) {
block *b = PyMem_Malloc(sizeof(block));
block *b;
if (numfreeblocks) {
numfreeblocks--;
return freeblocks[numfreeblocks];
}
b = PyMem_Malloc(sizeof(block));
if (b != NULL) {
return b;
}
Expand All @@ -131,7 +145,12 @@ newblock(void) {
static void
freeblock(block *b)
{
PyMem_Free(b);
if (numfreeblocks < MAXFREEBLOCKS) {
freeblocks[numfreeblocks] = b;
numfreeblocks++;
} else {
PyMem_Free(b);
}
}

static PyObject *
Expand Down