[Python-Dev] Caching float(0.0)

"Martin v. Löwis" martin at v.loewis.de
Mon Oct 2 15:49:50 CEST 2006


Kristján V. Jónsson schrieb:
> Well, a lot of extension code, like ours use PyFloat_FromDouble(foo);
> This can be from vectors and stuff.

Hmm. If you get a lot of 0.0 values from vectors and stuff, I would
expect that memory usage is already high.

In any case, a module that creates a lot of copies of 0.0 that way
could do its own caching, right?

> Very often these are values from a database.  Integral float values
> are very common in such case and id didn't occur to me that they
> weren't being reused, at least for small values.

Sure - but why are keeping people them in memory all the time?
Also, isn't it a mis-design of the database if you have many float
values in it that represent natural numbers? Shouldn't you use
a more appropriate data type, then?

> Also, a lot of arithmetic involving floats is expected to end in
> integers, like computing some index from a float value.  Integers get
> promoted to floats when touched by them, as you know.

Again, sounds like a programming error to me.

> Anyway, I now precreate integral values from -10 to 10 with great
> effect.  The cost is minimal, the benefit great.

In an extension module, the knowledge about the application domain
is larger, so it may be reasonable to do the caching there. I would
still expect that in the typical application where this is an issue,
there is some kind of larger design bug.

Regards,
Martin


More information about the Python-Dev mailing list