Message233066
As a test, I tried the following (taken mostly from http://codesnipers.com/?q=python-flyweights):
class Foo(object):
_Foo = weakref.WeakValueDictionary()
def __new__(cls, addr):
obj = Foo._Foo.get(addr, None)
if obj is None:
obj = object.__new__(cls)
Foo._Foo[addr] = obj
obj.addr = addr
return obj
I created 10 million instances of Foo(34) in an array. Total space taken: ~80 MB. Times: CPU times: user 6.93 s, sys: 48.7 ms, total: 6.98 s
Wall time: 6.98 s
I then created 10 million instances of a non-flyweight object, assigning an int to an instance variable:
class Bar(object):
pass
Total space taken: ~1.4 GB. Times:
CPU times: user 7.64 s, sys: 794 ms, total: 8.44 s
Wall time: 8.44 s
This corresponds (roughly) to the space taken by 10 million IPAddr objects.
So it appears, informally, that caching / flyweight results in modest time and significant memory savings.
I understand that the ship has sailed for a stdlib implementation, but these results are compelling enough for me to create a separate package. |
|
Date |
User |
Action |
Args |
2014-12-23 23:18:35 | sbromberger | set | recipients:
+ sbromberger, lemburg, rhettinger, ncoghlan, pmoody, r.david.murray, serhiy.storchaka, josh.r |
2014-12-23 23:18:35 | sbromberger | set | messageid: <1419376715.58.0.734090310723.issue23103@psf.upfronthosting.co.za> |
2014-12-23 23:18:35 | sbromberger | link | issue23103 messages |
2014-12-23 23:18:35 | sbromberger | create | |
|