-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Improve import performance of assertion rewrite. Fixes #3918. #3919
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
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.
Great work @fabioz, thanks!
f40c9d8
to
d53e449
Compare
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.
I fixed the tests and added unittests for the early bailout.
It would be nice to have a second set of eyes from other maintainers over this change. 👍
Codecov Report
@@ Coverage Diff @@
## master #3919 +/- ##
==========================================
- Coverage 96.24% 94.99% -1.25%
==========================================
Files 108 108
Lines 23353 23437 +84
Branches 0 2332 +2332
==========================================
- Hits 22475 22265 -210
- Misses 878 881 +3
- Partials 0 291 +291
Continue to review full report at Codecov.
|
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.
As the def is_package(self, name):
was added back, it should probably use the new self._imp_find_module
instead of imp.find_module
.
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.
Also, imp.find_module
is actually deprecated (seeing 3.6 docs) -- if it's only going to be used for imp.find_module
, its contents can probably be extracted just for the PKG_DIRECTORY
case (there's a pure-python version which may be seen in imp.find_module
).
I added it back because a test was failing without it. Good catch about |
Yeah, as we talked in person, it is nightmarish to keep this code working across all Python versions we support, so we are kind of forced to use the deprecated module. We will probably revisit the rewriting hook completely when we can drop Python 2 in a year or so. 😁 |
Well, in this case I was actually talking about the |
Oh sorry, I did not catch the entirety of the message, my bad. Please feel free to experiment and push other commits here if you want. 👍 |
I'll do that later on (I'd like to get this one merged first and then experiment more on that idea afterwards as one improvement is not dependent on the other). |
Sounds good! |
@thedrow pytest is already internally using pathlib and keeps adding more uses (using the backports in pathlib2 for python <3.6) - so we can do this right now |
@thedrow / @RonnyPfannschmidt was the |
Some notes:
On my use case there are many imports (around 3000 imports) and there are many entries in
sys.path
(67 to be more exact) -- this makesimp.find_import
very slow (it does way too many calls toos.stat
in the scenario) -- on my use case imports are around 30% faster with my patch in place in Py 2 and 40% faster on Py 3 on my measurements (granted my case is probably an edge case forimp.find_import
).I took a look at https://www.python.org/dev/peps/pep-0302/ and it says the following:
The following set of methods may be implemented if support for (for example) Freeze-like tools is desirable. It consists of three additional methods which, to make it easier for the caller, each of which should be implemented, or none at all:
loader.is_package(fullname)
loader.get_code(fullname)
loader.get_source(fullname)
So, as the loader had
is_package
and not the others I simply removedis_package
and things still seem to be working -- which is good because it was also callingimp.find_module
(only on Python 3, it seems it was unused on Python 2 -- which also means imports were slower on Python 3 than on Python 2).Fix #3918