New submission from Seth Bromberger:
ipaddress.ip_address instances should be flyweight. There's really no reason to
make them mutable:
a = ipaddress.ip_address(10.1.2.3)
b = ipaddress.ip_address(10.1.2.3)
id(a)
140066533772368
id(b)
140066504622544
Especially with IPv6 and large numbers
Seth Bromberger added the comment:
Confirmed on 3.4.0; likely exists in previous versions as well.
--
versions: +Python 3.4 -Python 3.6
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23103
Seth Bromberger added the comment:
What is your proposal? WeakValueDictionary mapping raw bytes object to single
instance of ipaddress that is queried from ipaddress's __new__? No built-in
has quite that extensive a level of caching and aggressive deduplication to my
knowledge.
I
Seth Bromberger added the comment:
1) However:
b = ipaddress.IPv4Address('1.2.3.4')
a = ipaddress.IPv4Address('1.2.3.4')
id(a)
4428380928
id(b)
4428381768
a == b
True
b._ip += 6
id(b)
4428381768
b
IPv4Address('1.2.3.10')
2) Isn’t _version already a class attribute? It’s set in _Basev4
Seth Bromberger added the comment:
The opposite argument is that it may be better left up to the application that
has to handle lots of ips to do the caching according to what it knows to be
an optimum pattern.
I'd agree with you wholeheartedly if ipaddress wasn't part of stdlib
Seth Bromberger added the comment:
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
Seth Bromberger added the comment:
Sorry for the additional followup, but I re-ran the test with approximately
real-world parameters. In this second test, I created 10 million Foo()
instances with random values from 1-2000 (using random.randint). This
corresponds to 2000 hosts generating 10
Seth Bromberger added the comment:
I'm just pointing out that if he thinks 56 bytes per object is too large, he's
going to be disappointed with what Python has to offer. General purpose
user-defined Python objects don't optimize for low memory usage, and even
__slots__ only gets you so far