ipaddress: Speed up IPv4Address.ipv6_mapped by jstasiak · Pull Request #119814 · python/cpython
A random find from my side, I was going through this code when I noticed
some unnecessary conversions happening.
Nice performance improvement basically for free (numbers from my M1 Pro
laptop):
In [1]: from ipaddress import IPv4Address, IPv6Address
In [2]: v4 = IPv4Address('10.0.0.1')
In [3]: IPv6Address(f'::ffff:{v4}') == IPv6Address((0xffff << 32) + v4._ip)
Out[3]: True
In [4]: %timeit IPv6Address(f'::ffff:{v4}')
3.63 µs ± 12 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [5]: %timeit IPv6Address((0xffff << 32) + v4._ip)
184 ns ± 0.413 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
jstasiak
changed the title
Speed up IPv4Address.ipv6_mapped (numbers instead of strings)
Speed up IPv4Address.ipv6_mapped by removing unnecessary conversions
jstasiak
changed the title
Speed up IPv4Address.ipv6_mapped by removing unnecessary conversions
ipaddress: Speed up IPv4Address.ipv6_mapped
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters