Skip to content

Unnecessary operation in IPv4Interface and IPv6Interface #134012

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

Closed
mssalvatore opened this issue May 14, 2025 · 1 comment
Closed

Unnecessary operation in IPv4Interface and IPv6Interface #134012

mssalvatore opened this issue May 14, 2025 · 1 comment

Comments

@mssalvatore
Copy link

mssalvatore commented May 14, 2025

The __hash__() methods of the IPv4Interface and IPv6Interface classes are implemented on the main branch as follows:

    def __hash__(self):
        return hash((self._ip, self._prefixlen, int(self.network.network_address)))

https://github.com/python/cpython/blob/0afbd4e42ac28240b484cabe1710fdb6c572fb1f/Lib/ipaddress.py#L1457C1-L1459

As best as I can figure from tracing the relevant __init__() methods, self._ip and int(self.network.network_address) will always be identical. If that is so, this function could be simplified and save a few CPU cycles:

     def __hash__(self):
-        return hash((self._ip, self._prefixlen, int(self.network.network_address)))
+        return hash((self._ip, self._prefixlen))

Linked PRs

mssalvatore added a commit to mssalvatore/cpython that referenced this issue May 14, 2025
The value of `self._ip` and `int(self.network.network_address)` will
always be the identical. Therefore, it's unnecessary to include
`int(self.network.network_address)` in the tuple used to compute the
hash.
@mssalvatore
Copy link
Author

I missed some details and the conclusion that self._ip and self.network.network_address are always identical are incorrect.

@ZeroIntensity ZeroIntensity closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants