summaryrefslogtreecommitdiffstats
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2015-01-18 15:22:47 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2015-01-18 15:22:47 (GMT)
commite6f250ed90d0efed83f9e3beef97e5f9e6398d00 (patch)
tree4eab1a386bb753827e51201a1d50feaa7710fad7 /Lib/ipaddress.py
parent82e07b92b330ec885968ca98fac5284c45037eac (diff)
downloadcpython-e6f250ed90d0efed83f9e3beef97e5f9e6398d00.zip
cpython-e6f250ed90d0efed83f9e3beef97e5f9e6398d00.tar.gz
cpython-e6f250ed90d0efed83f9e3beef97e5f9e6398d00.tar.bz2
Issue #23266: Much faster implementation of ipaddress.collapse_addresses() when there are many non-consecutive addresses.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index b32a7e1..8d369e3 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -170,16 +170,19 @@ def _find_address_range(addresses):
addresses: a list of IPv#Address objects.
Returns:
- A tuple containing the first and last IP addresses in the sequence.
+ A tuple containing the first and last IP addresses in the sequence,
+ and the number of distinct IP addresses in the sequence.
"""
first = last = addresses[0]
+ i = 1
for ip in addresses[1:]:
if ip._ip == last._ip + 1:
last = ip
+ i += 1
else:
break
- return (first, last)
+ return (first, last, i)
def _count_righthand_zero_bits(number, bits):
@@ -346,12 +349,13 @@ def collapse_addresses(addresses):
ip, nets[-1]))
nets.append(ip)
- # sort and dedup
- ips = sorted(set(ips))
+ # sort
+ ips = sorted(ips)
+ # find consecutive address ranges in the sorted sequence and summarize them
while i < len(ips):
- (first, last) = _find_address_range(ips[i:])
- i = ips.index(last) + 1
+ (first, last, items) = _find_address_range(ips[i:])
+ i = items + i
addrs.extend(summarize_address_range(first, last))
return _collapse_addresses_internal(addrs + nets)