-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
optimize v4 & v6 netmask parsing #65685
Comments
Here is a patch to optimize ipv4 netmask parsing by maintaining a cache (there are not many valid ipv4 netmask representations). This should be especially useful with bpo-16531. |
Updated patch, also optimizing v6 netmask parsing (same principle). Before patch: $ ./python -m timeit -s "import ipaddress" "net = ipaddress.IPv6Network(('2001:db8::', 96))"
10000 loops, best of 3: 26.1 usec per loop
$ ./python -m timeit -s "import ipaddress" "net = ipaddress.IPv4Network(('10.0.0.0', 23))"
100000 loops, best of 3: 17 usec per loop After patch: $ ./python -m timeit -s "import ipaddress" "net = ipaddress.IPv6Network(('2001:db8::', 96))"
100000 loops, best of 3: 13.8 usec per loop
$ ./python -m timeit -s "import ipaddress" "net = ipaddress.IPv4Network(('10.0.0.0', 23))"
100000 loops, best of 3: 14.3 usec per loop |
Why not just use functools.lru_cache? |
Because that would incur the cost of LRU logic and locking, which we don't need here. |
With C implementation (bpo-14373) functools.lru_cache is so fast as manually written specialized code. |
What I want to say, the patch LGTM, but after committing bpo-14373 we should simplify the code by using functools.lru_cache(). |
Actually, using lru_cache(maxsize=None) would enable a simple infinite cache like in the patch. But it's not like a lot of code would be saved. |
New changeset 2158614e1607 by Antoine Pitrou in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: