cpython icon indicating copy to clipboard operation
cpython copied to clipboard

ipaddress: Speed up IPv4Address.ipv6_mapped

Open jstasiak opened this issue 1 year ago • 1 comments

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 avatar May 30 '24 23:05 jstasiak

I think this is small enough to not need an issue number. Since this is a 3.13+ feature I don't believe a news entry is needed either.

jstasiak avatar May 30 '24 23:05 jstasiak