[Python-Dev] Caching float(0.0)
Kristján V. Jónsson
kristjan at ccpgames.com
Mon Oct 2 16:19:18 CEST 2006
Well, Skip made the argument when analyzing the testsuite:
"So for a largely non-floating point "application", a fair number of floats are allocated, a bit over 25% of them are -1.0, 0.0 or +1.0, and nearly 50% of them are whole numbers between -10.0 and 10.0, inclusive. "
In C, there is no need to treat 0.0 any different from any other value, since they are literals. You will find axis aligned unit vectors to be very common in any 3D app.
I can't say exactly where all those integral floats are coming from. I could investigate further, but it seems to me that they are simply quite common in real-world applications. Experience shows that 0.0 is _very_ common, even, and the test suite test skip made should make this abundantly clear.
I can't see how this situation is any different from the re-use of low ints. There is no fundamental law that says that ints below 100 are more common than other, yet experience shows that this is so, and so they are reused.
Rather than to view this as a programming error, why not simply accept that this is a recurring pattern and adjust python to be more efficient when faced by it? Surely a lot of karma lies that way?
Cheers,
Kristján
> -----Original Message-----
> From: "Martin v. Löwis" [mailto:martin at v.loewis.de]
> Sent: 2. október 2006 13:50
> To: Kristján V. Jónsson
> Cc: Bob Ippolito; python-dev at python.org
> Subject: Re: [Python-Dev] Caching float(0.0)
>
> 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