Skip to content

Commit 3bb1987

Browse files
authored
Revert "bpo-40521: Remove freelist from collections.deque() (GH-21073)" (GH-24944)
This reverts commit 32f2eda. It can be re-applied if the subinterpreter PEP is approved. Otherwise, the commit degraded performance with no offsetting benefit.
1 parent 929c903 commit 3bb1987

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Modules/_collectionsmodule.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,23 @@ static PyTypeObject deque_type;
118118
#define CHECK_NOT_END(link)
119119
#endif
120120

121+
/* A simple freelisting scheme is used to minimize calls to the memory
122+
allocator. It accommodates common use cases where new blocks are being
123+
added at about the same rate as old blocks are being freed.
124+
*/
125+
126+
#define MAXFREEBLOCKS 16
127+
static Py_ssize_t numfreeblocks = 0;
128+
static block *freeblocks[MAXFREEBLOCKS];
129+
121130
static block *
122131
newblock(void) {
123-
block *b = PyMem_Malloc(sizeof(block));
132+
block *b;
133+
if (numfreeblocks) {
134+
numfreeblocks--;
135+
return freeblocks[numfreeblocks];
136+
}
137+
b = PyMem_Malloc(sizeof(block));
124138
if (b != NULL) {
125139
return b;
126140
}
@@ -131,7 +145,12 @@ newblock(void) {
131145
static void
132146
freeblock(block *b)
133147
{
134-
PyMem_Free(b);
148+
if (numfreeblocks < MAXFREEBLOCKS) {
149+
freeblocks[numfreeblocks] = b;
150+
numfreeblocks++;
151+
} else {
152+
PyMem_Free(b);
153+
}
135154
}
136155

137156
static PyObject *

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy