You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current check "address in network" is seems a bit odd:
int(self.network_address) <= int(other._ip) < int(self.broadcast_address)
This patch make this in bit-operation manner. Perfomace test:
importipaddressimporttimeitclassIPv6Network2(ipaddress.IPv6Network):
def__contains__(self, other):
# always false if one is v4 and the other is v6.ifself._version!=other._version:
returnFalse# dealing with another network.ifisinstance(other, ipaddress._BaseNetwork):
returnFalseelse:
# addressreturnother._ip&self.netmask._ip==self.network_address._ipclassIPv4Network2(ipaddress.IPv4Network):
def__contains__(self, other):
# always false if one is v4 and the other is v6.ifself._version!=other._version:
returnFalse# dealing with another network.ifisinstance(other, ipaddress._BaseNetwork):
returnFalse# dealing with another addresselse:
# addressreturnother._ip&self.netmask._ip==self.network_address._ipipv6_test_net=ipaddress.IPv6Network("::/0")
ipv6_test_net2=IPv6Network2("::/0")
ipv4_test_net=ipaddress.IPv4Network("0.0.0.0/0")
ipv4_test_net2=IPv4Network2("0.0.0.0/0")
dataipv6=list()
dataipv4=list()
forxinrange(2000000):
dataipv6.append(ipaddress.IPv6Address(x))
dataipv4.append(ipaddress.IPv4Address(x))
deftest():
fordindataipv6:
dinipv6_test_netdeftest2():
fordindataipv6:
dinipv6_test_net2deftest3():
fordindataipv4:
dinipv4_test_netdeftest4():
fordindataipv4:
dinipv4_test_net2t=timeit.Timer("test()", "from __main__ import test")
print("ipv6 test origin __contains__", t.timeit(number=1))
t=timeit.Timer("test2()", "from __main__ import test2")
print("ipv6 test new __contains__", t.timeit(number=1))
t=timeit.Timer("test3()", "from __main__ import test3")
print("ipv4 test origin __contains__", t.timeit(number=1))
t=timeit.Timer("test4()", "from __main__ import test4")
print("ipv4 test new __contains__", t.timeit(number=1))
Output:
ipv6 test origin __contains__ 4.265904285013676
ipv6 test new __contains__ 1.551749340025708
ipv4 test origin __contains__ 3.689626455074176
ipv4 test new __contains__ 2.0175559649942443
I think this patch can be easily applied. There are no any breaking changes. My colleagues and I assume that ipaddress module would work fast because it is easy to imagine tasks, operate with ton of networks.
Also, current comparison implementation works as not so good pattern for other developers.
I confirm that the proposed code is equivalent to the existing code and is faster. The largest part (~70%) of the speed up is caused by replacing int() calls with direct attribute access to _ip. I'm wondering whether it is worth to get rid of int() calls in the rest of the code.
Though this is out of the scope of the issue I tried converting num_addresses, __hash__, __getitem__ and __eq__ as per Serhiy's idea for IPv6Network replacing the stdlib implementation's int calls with _ip in a custom class. I can see up to 50% speedups as below and no test case failures converting rest of the call sites to use _ip instead of int in stdlib. But some of the methods may not be used as frequently as in this benchmark like thus being not worthy enough of change. Shall I open a new issue for further discussion?
$ python3.7 bpo25430_1.py
ipv6 test num_addresses with int 1.54065761
ipv6 test num_addresses without int 0.8266360819999998
ipv6 test hash with int 1.320016881
ipv6 test hash without int 0.6266323200000001
ipv6 test equality with int 1.6104001990000008
ipv6 test equality without int 1.0374885390000008
ipv6 test get item with int 2.092343390000001
ipv6 test get item without int 1.5606673410000003
$ bpo25430_1.py
importipaddressimporttimeitclassIPv6Network2(ipaddress.IPv6Network):
@propertydefnum_addresses(self):
returnself.broadcast_address._ip-self.network_address._ip+1def__hash__(self):
returnhash(self.network_address._ip^self.netmask._ip)
def__eq__(self, other):
try:
return (self._version==other._versionandself.network_address==other.network_addressandself.netmask._ip==other.netmask._ip)
exceptAttributeError:
returnNotImplementeddef__getitem__(self, n):
network=self.network_address._ipbroadcast=self.broadcast_address._ipifn>=0:
ifnetwork+n>broadcast:
raiseIndexError('address out of range')
returnself._address_class(network+n)
else:
n+=1ifbroadcast+n<network:
raiseIndexError('address out of range')
returnself._address_class(broadcast+n)
ipv6_test_net=ipaddress.IPv6Network("::/0")
ipv6_test_net2=IPv6Network2("::/0")
deftest1_num_address():
returnipv6_test_net.num_addressesdeftest2_num_address():
returnipv6_test_net2.num_addressesdeftest1_hash_address():
returnhash(ipv6_test_net)
deftest2_hash_address():
returnhash(ipv6_test_net2)
if__name__=="__main__":
t=timeit.Timer("test1_num_address()", "from __main__ import test1_num_address")
print("ipv6 test num_addresses with int", t.timeit(number=1000000))
t=timeit.Timer("test2_num_address()", "from __main__ import test2_num_address")
print("ipv6 test num_addresses without int", t.timeit(number=1000000))
t=timeit.Timer("test1_hash_address()", "from __main__ import test1_hash_address")
print("ipv6 test hash with int", t.timeit(number=1000000))
t=timeit.Timer("test2_hash_address()", "from __main__ import test2_hash_address")
print("ipv6 test hash without int", t.timeit(number=1000000))
t=timeit.Timer("ipv6_test_net == ipv6_test_net", "from __main__ import ipv6_test_net")
print("ipv6 test equality with int", t.timeit(number=1000000))
t=timeit.Timer("ipv6_test_net2 == ipv6_test_net2", "from __main__ import ipv6_test_net2")
print("ipv6 test equality without int", t.timeit(number=1000000))
t=timeit.Timer("ipv6_test_net[10000]", "from __main__ import ipv6_test_net")
print("ipv6 test get item with int", t.timeit(number=1000000))
t=timeit.Timer("ipv6_test_net2[10000]", "from __main__ import ipv6_test_net2")
print("ipv6 test get item without int", t.timeit(number=1000000))
Activity
gescheit commentedon Oct 17, 2015
Current check "address in network" is seems a bit odd:
int(self.network_address) <= int(other._ip) < int(self.broadcast_address)
This patch make this in bit-operation manner. Perfomace test:
Output:
ipv6 test origin __contains__ 4.265904285013676
ipv6 test new __contains__ 1.551749340025708
ipv4 test origin __contains__ 3.689626455074176
ipv4 test new __contains__ 2.0175559649942443
gescheit commentedon May 23, 2017
I think this patch can be easily applied. There are no any breaking changes. My colleagues and I assume that ipaddress module would work fast because it is easy to imagine tasks, operate with ton of networks.
Also, current comparison implementation works as not so good pattern for other developers.
pitrou commentedon May 23, 2017
Hi Aleksandr,
well, sorry for the detail. We now use GitHub for patch submission, would you like to submit a PR at https://github.com/python/cpython/ (see devguide at https://cpython-devguide.readthedocs.io/ for more information).
If you don't want to do so, that's fine, too. A core developer may take care of it for you.
pitrou commentedon May 23, 2017
s/sorry for the detail/sorry for the delay/
serhiy-storchaka commentedon May 25, 2017
I confirm that the proposed code is equivalent to the existing code and is faster. The largest part (~70%) of the speed up is caused by replacing int() calls with direct attribute access to _ip. I'm wondering whether it is worth to get rid of int() calls in the rest of the code.
The overhead of using int():
tirkarthi commentedon Dec 16, 2018
Though this is out of the scope of the issue I tried converting num_addresses, __hash__, __getitem__ and __eq__ as per Serhiy's idea for IPv6Network replacing the stdlib implementation's int calls with _ip in a custom class. I can see up to 50% speedups as below and no test case failures converting rest of the call sites to use _ip instead of int in stdlib. But some of the methods may not be used as frequently as in this benchmark like thus being not worthy enough of change. Shall I open a new issue for further discussion?
ipv6 test num_addresses with int 1.54065761
ipv6 test num_addresses without int 0.8266360819999998
ipv6 test hash with int 1.320016881
ipv6 test hash without int 0.6266323200000001
ipv6 test equality with int 1.6104001990000008
ipv6 test equality without int 1.0374885390000008
ipv6 test get item with int 2.092343390000001
ipv6 test get item without int 1.5606673410000003
methane commentedon Apr 30, 2019
New changeset 3bbcc92 by Inada Naoki (gescheit) in branch 'master':
bpo-25430: improve performance of IPNetwork.__contains__ (GH-1785)
3bbcc92
tirkarthi commentedon Oct 24, 2019
Closing it as resolved since the optimization was merged in 3.8.0 . Thanks @gescheit for the report and patch.