Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93ad0df

Browse files
committed
Faster floating point allocator, same idea as the int allocator.
By Aaron Watters.
1 parent 0609f19 commit 93ad0df

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

Objects/floatobject.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ extern double fmod Py_PROTO((double, double));
8383
extern double pow Py_PROTO((double, double));
8484
#endif
8585

86+
/* Special free list -- see comments for same code in intobject.c. */
87+
static PyFloatObject *free_list = NULL;
88+
#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
89+
#define N_FLOATOBJECTS (BLOCK_SIZE / sizeof(PyFloatObject))
90+
91+
static PyFloatObject *
92+
fill_free_list()
93+
{
94+
PyFloatObject *p, *q;
95+
p = PyMem_NEW(PyFloatObject, N_FLOATOBJECTS);
96+
if (p == NULL)
97+
return (PyFloatObject *)PyErr_NoMemory();
98+
q = p + N_FLOATOBJECTS;
99+
while (--q > p)
100+
*(PyFloatObject **)q = q-1;
101+
*(PyFloatObject **)q = NULL;
102+
return p + N_FLOATOBJECTS - 1;
103+
}
104+
86105
PyObject *
87106
#ifdef __SC__
88107
PyFloat_FromDouble(double fval)
@@ -91,11 +110,13 @@ PyFloat_FromDouble(fval)
91110
double fval;
92111
#endif
93112
{
94-
/* For efficiency, this code is copied from newobject() */
95-
register PyFloatObject *op =
96-
(PyFloatObject *) malloc(sizeof(PyFloatObject));
97-
if (op == NULL)
98-
return PyErr_NoMemory();
113+
register PyFloatObject *op;
114+
if (free_list == NULL) {
115+
if ((free_list = fill_free_list()) == NULL)
116+
return NULL;
117+
}
118+
op = free_list;
119+
free_list = *(PyFloatObject **)free_list;
99120
op->ob_type = &PyFloat_Type;
100121
op->ob_fval = fval;
101122
_Py_NewReference(op);
@@ -104,9 +125,10 @@ PyFloat_FromDouble(fval)
104125

105126
static void
106127
float_dealloc(op)
107-
PyObject *op;
128+
PyFloatObject *op;
108129
{
109-
PyMem_DEL(op);
130+
*(PyFloatObject **)op = free_list;
131+
free_list = op;
110132
}
111133

112134
double

0 commit comments

Comments
 (0)