Event Timeline
Comment Actions
The equivalent linear search (diff below) is ~800-850 times slower on my laptop:
--- parse_ana_patricia.py +++ parse_ana_linear.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import re -import pytricia +import ipaddress import urllib.request import random @@ -13,7 +13,7 @@ def parse(iterable): pattern = "^(?P<prefix>[^;]+);srcAS=(?P<asn>[^;]+);altpfx=(?P<altpfx>[^;]+);.*" - prefixes = pytricia.PyTricia(128) + prefixes = {} for line in iterable: match = re.search(pattern, line) @@ -26,6 +26,7 @@ else: altpfx = True + prefix = ipaddress.ip_network(prefix) prefixes[prefix] = altpfx return prefixes @@ -47,14 +48,16 @@ def lookup(ip): - try: - altpfx = prefixes[ip] - if altpfx: - result = "invalid-but-reachable" - else: - result = "unreachable" - except KeyError: - result = "valid-or-unverified" + result = "valid-or-unverified" + + ip = ipaddress.ip_address(ip) + for prefix, altpfx in prefixes.items(): + if ip in prefix: + if altpfx: + result = "invalid-but-reachable" + else: + result = "unreachable" + break return result