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

bpo-31581: Reduce the number of imports for functools #3757

Merged
merged 7 commits into from
Sep 30, 2017

Conversation

methane
Copy link
Member

@methane methane commented Sep 26, 2017

Since functools and collections are very commonly used, it's worth enough to optimize import.

https://bugs.python.org/issue31581

@methane
Copy link
Member Author

methane commented Sep 26, 2017

I added 'skip news' tag, since #1269 don't have news too.

Copy link
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functools.singledispatch change seems reasonable to me, since the cost of checking the import cache twice will be cheap compared to the overhead of defining a new function to apply @singledispatch to.

As per the comments on the argparse PR, this should be accompanied by a comment explaining the trade-off (i.e. there are many programs that use functools without singledispatch, so we trade-off making singledispatch marginally slower for the benefit of making start-up of such applications slightly faster)

However, I'm less sure of the appropriateness of the start-up vs runtime cost trade-off for Counter.most_common, so I'd prefer to see that change in a separate PR.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Lib/functools.py Outdated
# There are many programs that use functools without singledispatch, so we
# trade-off making singledispatch marginally slower for the benefit of
# making start-up of such applications slightly faster.
from types import MappingProxyType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is slightly faster to import types and use types.MappingProxyType once than import MappingProxyType from types. The from ... import case runs a Python code from _bootstrap.py, while the former case runs just a C code if the types module already is imported.

@@ -2033,9 +2035,9 @@ def __setitem__(self, key, value):
self.data[key] = value
def clear(self):
self.data.clear()
_orig_wkd = functools.WeakKeyDictionary
_orig_wkd = weakref.WeakKeyDictionary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use try ... finally or test.support.swap_attr() for guaranteeing that weakref.WeakKeyDictionary is restored if the following checks fail.

@methane
Copy link
Member Author

methane commented Sep 28, 2017

However, I'm less sure of the appropriateness of the start-up vs runtime cost trade-off for Counter.most_common, so I'd prefer to see that change in a separate PR.

fixed.

@methane methane merged commit 9811e80 into python:master Sep 30, 2017
@methane methane deleted the bpo31518-functools-import branch September 30, 2017 07:13
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.

6 participants