Float object reuse

I thought I’d mention a cool little patch we did to Python some years back.

We work with database tables a lot.  Game configuration data is essentially rows in a vast database.  And those rows contain a lot of floats.  At some point I recognized that common float values were not being reused.  In particular, id(0.0) != id(0.0).  I was a bit surprized by this, since I figured, some floats must be more common than others.  Certainly, 0.0 is a bit special.

I mentioned this on python-dev some years back but with somewhat underwhelming results.  A summary of the discussion can be found here.

Anyway, I thought I’d mention this to people doing a lot of floating point.  We saved a huge amount of memory on our servers just caching integral floating point values between -10 and +10, including both the negative and positive 0.0.  These values are very frequent, for example as multipliers in tables, and so on.

Here’s some of the code:


PyObject *
PyFloat_FromDouble(double fval)
    register PyFloatObject *op;
    int ival;
    if (free_list == NULL) {
        if ((free_list = fill_free_list()) == NULL)
            return NULL;
        /* CCP addition, cache common values */
        if (!f_reuse[0]) {
            int i;
            for(i = 0; i<21; i++)
                f_reuse[i] = PyFloat_FromDouble((double)(i-10));
    /* CCP addition, check for recycling */
    ival = (int)fval;
    if ((double)ival == fval && ival>=-10 && ival <= 10) {
        /* ignore the negative zero */
        if (ival || _fpclass(fval) != _FPCLASS_NZ) {
        /* can't differentiate between positive and negative zeroes, ignore both */
        if (ival) {
            if (f_reuse[ival]) {
                return f_reuse[ival];

    /* Inline PyObject_New */
    op = free_list;
    free_list = (PyFloatObject *)Py_TYPE(op);
    PyObject_INIT(op, &PyFloat_Type);
    op->ob_fval = fval;
    return (PyObject *) op;


(Please excuse the lame syntax highlighter with its &amp; and &lt; thingies 🙂


3 thoughts on “Float object reuse

  1. Have you considered re-floating (sorry…) the idea? It seems like python-dev is more interested in optimizations now than it used to be, especially where the increase in complexity is minimal as it seems to be here.

    • Ah, I might do that.
      I´ve actually been taking stock of several of the changes we have made and making new ones. Just yesterday, for example, I added code to allow an embedder to specifiy its own memory allocator to use.
      But I am a bit hindered, as before, by the fact that active development is going on in 3.x only and porting these changes to that branch is always an extra effort.
      But you are right, it is time, I think, to review the delta of changes that we have done and see what, if any, of those would be of interest to the community.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s