-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-27860: clean up ipaddress #12774
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
Conversation
Lib/ipaddress.py
Outdated
args = _split_optional_netmask(address) | ||
addr = self._ip_int_from_string(args[0]) | ||
mask = args[1] if len(args) == 2 else self._max_prefixlen | ||
super().__init__(address) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is faster, _BaseNetwork.__init__()
or super().__init__()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my initial patch I changed this to the super().__init__()
on purpose from the version where the base class is mentioned explicitly. Not using the super()
version here makes it harder to write subclasses for those classes. Also, I personally think it's better style to use super()
.
I have not measured it but I don't think that this is a significant speed improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not using the
super()
version here makes it harder to write subclasses for those classes.
Simple subclass is OK without super(). It helps only complicated subclassing like:
class A(IPv4Network):
def __init__(self, address):
super().__init__(address)
class B(_BaseNetwork):
def __init__(self, address)
super().__init__(address)
class C(A, B):
def __init__(self, address):
super().__init__(address)
In this case, if we don't use super()
in IPv4Network
, __init__
is called for C -> A -> IPv4Network -> _BaseNetwork (B is skipped)
if we use super()
, C -> A -> IPv4Network -> B -> _BaseNetwork.
But note that _BaseNetwork
is private class.
Not using super()
here doesn't make write subclass of IPv4Network harder.
Also, I personally think it's better style to use
super()
.
I'm +0.1 on super in general.
I have not measured it but I don't think that this is a significant speed improvement.
It seems 3% slowdown.
$ ./python -m perf timeit -s 'from ipaddress import IPv4Network' 'IPv4Network("10.10.1.0/24")'
...
Mean +- std dev: [legacy] 8.96 us +- 0.09 us -> [super] 9.23 us +- 0.07 us: 1.03x slower (+3%)
I don't have any strong opinion here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now there is no __init__
call.
Lib/ipaddress.py
Outdated
self.netmask = self.network.netmask | ||
self.hostmask = self.network.hostmask | ||
@functools.cached_property | ||
def hostmask(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a new property as per current docs at https://docs.python.org/3.8/library/ipaddress.html#ipaddress.IPv4Interface . Should this be documented?
@methane This might increase the scope of the PR but there are functions where Explanation from Serhiy on the optimization : https://bugs.python.org/issue25430#msg294442 |
Patch by Moritz Sichert.
https://bugs.python.org/issue27860