Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipaddress: Speed up IPv4Address.ipv6_mapped #119814

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jstasiak
Copy link
Contributor

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)

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
Copy link
Contributor Author

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 jstasiak changed the title Speed up IPv4Address.ipv6_mapped (numbers instead of strings) Speed up IPv4Address.ipv6_mapped by removing unnecessary conversions May 30, 2024
@jstasiak jstasiak changed the title Speed up IPv4Address.ipv6_mapped by removing unnecessary conversions ipaddress: Speed up IPv4Address.ipv6_mapped May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant