#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];
+
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;
}
static void
freeblock(block *b)
{
- PyMem_Free(b);
+ if (numfreeblocks < MAXFREEBLOCKS) {
+ freeblocks[numfreeblocks] = b;
+ numfreeblocks++;
+ } else {
+ PyMem_Free(b);
+ }
}
static PyObject *