Kaydet (Commit) 824db30b authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Issue #21487: Optimize ipaddress.summarize_address_range() and…

Issue #21487: Optimize ipaddress.summarize_address_range() and ipaddress.{IPv4Network,IPv6Network}.subnets().
üst 45aba189
...@@ -195,11 +195,7 @@ def _count_righthand_zero_bits(number, bits): ...@@ -195,11 +195,7 @@ def _count_righthand_zero_bits(number, bits):
""" """
if number == 0: if number == 0:
return bits return bits
for i in range(bits): return min(bits, (~number & (number-1)).bit_length())
if (number >> i) & 1:
return i
# All bits of interest were zero, even if there are more in the number
return bits
def summarize_address_range(first, last): def summarize_address_range(first, last):
...@@ -250,12 +246,11 @@ def summarize_address_range(first, last): ...@@ -250,12 +246,11 @@ def summarize_address_range(first, last):
while first_int <= last_int: while first_int <= last_int:
nbits = min(_count_righthand_zero_bits(first_int, ip_bits), nbits = min(_count_righthand_zero_bits(first_int, ip_bits),
(last_int - first_int + 1).bit_length() - 1) (last_int - first_int + 1).bit_length() - 1)
net = ip('%s/%d' % (first, ip_bits - nbits)) net = ip((first_int, ip_bits - nbits))
yield net yield net
first_int += 1 << nbits first_int += 1 << nbits
if first_int - 1 == ip._ALL_ONES: if first_int - 1 == ip._ALL_ONES:
break break
first = first.__class__(first_int)
def _collapse_addresses_recursive(addresses): def _collapse_addresses_recursive(addresses):
...@@ -949,20 +944,11 @@ class _BaseNetwork(_IPAddressBase): ...@@ -949,20 +944,11 @@ class _BaseNetwork(_IPAddressBase):
'prefix length diff %d is invalid for netblock %s' % ( 'prefix length diff %d is invalid for netblock %s' % (
new_prefixlen, self)) new_prefixlen, self))
first = self.__class__('%s/%s' % start = int(self.network_address)
(self.network_address, end = int(self.broadcast_address)
self._prefixlen + prefixlen_diff)) step = (int(self.hostmask) + 1) >> prefixlen_diff
for new_addr in range(start, end, step):
yield first current = self.__class__((new_addr, new_prefixlen))
current = first
while True:
broadcast = current.broadcast_address
if broadcast == self.broadcast_address:
return
new_addr = self._address_class(int(broadcast) + 1)
current = self.__class__('%s/%s' % (new_addr,
new_prefixlen))
yield current yield current
def supernet(self, prefixlen_diff=1, new_prefix=None): def supernet(self, prefixlen_diff=1, new_prefix=None):
......
...@@ -84,6 +84,9 @@ Core and Builtins ...@@ -84,6 +84,9 @@ Core and Builtins
Library Library
------- -------
- Issue #21487: Optimize ipaddress.summarize_address_range() and
ipaddress.{IPv4Network,IPv6Network}.subnets().
- Issue #21486: Optimize parsing of netmasks in ipaddress.IPv4Network and - Issue #21486: Optimize parsing of netmasks in ipaddress.IPv4Network and
ipaddress.IPv6Network. ipaddress.IPv6Network.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment