diff options
author | Guido van Rossum <guido@python.org> | 1997-05-13 21:00:42 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-05-13 21:00:42 (GMT) |
commit | 93ad0df06ccd61b927d08111dba3cfa0cebb4ce0 (patch) | |
tree | 2b61391765c4d24c43713cc984cb5d2d4cee82ed /Objects/floatobject.c | |
parent | 0609f191bc3f22af862696cd0002449bd48acb8d (diff) | |
download | cpython-93ad0df06ccd61b927d08111dba3cfa0cebb4ce0.zip cpython-93ad0df06ccd61b927d08111dba3cfa0cebb4ce0.tar.gz cpython-93ad0df06ccd61b927d08111dba3cfa0cebb4ce0.tar.bz2 |
Faster floating point allocator, same idea as the int allocator.
By Aaron Watters.
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r-- | Objects/floatobject.c | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c index d66bf85..7f7f507 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -83,6 +83,25 @@ extern double fmod Py_PROTO((double, double)); extern double pow Py_PROTO((double, double)); #endif +/* Special free list -- see comments for same code in intobject.c. */ +static PyFloatObject *free_list = NULL; +#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ +#define N_FLOATOBJECTS (BLOCK_SIZE / sizeof(PyFloatObject)) + +static PyFloatObject * +fill_free_list() +{ + PyFloatObject *p, *q; + p = PyMem_NEW(PyFloatObject, N_FLOATOBJECTS); + if (p == NULL) + return (PyFloatObject *)PyErr_NoMemory(); + q = p + N_FLOATOBJECTS; + while (--q > p) + *(PyFloatObject **)q = q-1; + *(PyFloatObject **)q = NULL; + return p + N_FLOATOBJECTS - 1; +} + PyObject * #ifdef __SC__ PyFloat_FromDouble(double fval) @@ -91,11 +110,13 @@ PyFloat_FromDouble(fval) double fval; #endif { - /* For efficiency, this code is copied from newobject() */ - register PyFloatObject *op = - (PyFloatObject *) malloc(sizeof(PyFloatObject)); - if (op == NULL) - return PyErr_NoMemory(); + register PyFloatObject *op; + if (free_list == NULL) { + if ((free_list = fill_free_list()) == NULL) + return NULL; + } + op = free_list; + free_list = *(PyFloatObject **)free_list; op->ob_type = &PyFloat_Type; op->ob_fval = fval; _Py_NewReference(op); @@ -104,9 +125,10 @@ PyFloat_FromDouble(fval) static void float_dealloc(op) - PyObject *op; + PyFloatObject *op; { - PyMem_DEL(op); + *(PyFloatObject **)op = free_list; + free_list = op; } double |