gh-118761: Improve import time for pstats and zipfile by removing imports to typing#128981
Conversation
|
@vstinner I plan to merge this one with the following commit title: And the following commit body: I don't think I'll mention the fact that |
It is inconsistent, but I've been taking to adding annotations when I write code, for clarity. If it's discouraged to add type annotations to Python code and just leave it to typeshed to keep up, I guess that's okay, though it sounds slightly terrible. If that's the case, though, and it's not possible to incrementally improve the native typing, I think we should document that somewhere in the dev guide (if it's not already). WDYT? Could this same performance improvement have been gained by putting the import in an |
|
The status quo is to use typeshed, correct. Do we have other stdlib modules which use type annotations? I'm mostly aware of test.libregrtest which is more an application than a stdlib module. |
|
@jaraco I think this thread represents the latest position. Typeshed overrides the stdlib, so there's no real benefit (and real costs) to having type annotations in the stdlib. Using A |
|
I would not have done this PR without evidence that there was an actual performance problem. In realistic applications, If the import time of (it was mentioned in https://discuss.python.org/t/static-type-annotations-in-cpython/65068/9) |
I'm not sure how it follows that "realistic applications" already import
That's an extremely reasonable point and can surely be done as well, but I don't see why it should constitute a valid criticism of making a different, unrelated speed improvement? |
Because it illustrates my point that this wasn't a "speed improvement" but more of a reactionary minor performance regression prevention without considering what could be done to actually optimize |
Okay, so I actually went in and took some timings. Here is my patch: Note that I don't delay the zipfile import; it's needed to handle crc32 consistently and it wasn't obviously something that would be significant to optimize. I have 3 timings:
I cannot remotely guarantee I know what I'm doing with a benchmarking tool, I've said so in other PRs too. ;) Still, the experimental results I got say that there are HUGE gains to be gotten from removing The reason I did these timings was because I thought it sounded like a great idea to solve this as a followup:
But based on my timings I've changed my mind and don't intend to submit this patch as it feels useless to waste time caring about this insignificant and not at all slow import. |
Those two imports are not really needed and we can reduce the import time of
zipfile, or anything import it. Roughly, importingzipfiletakes 8ms with this PR while it takes10mson main. Also,zipfileis not typed by default, so adding-> Selfis a bit inconsistent (though it helps type checkers). I think such typing should be left to typeshed.cc @jaraco as the one who added the type hints in
zipfile.