This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Possible lazy import opportunities in `pathlib`
Type: performance Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, njs, pitrou, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-05-07 12:28 by ncoghlan, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 6820 open agf, 2018-05-14 19:17
Messages (4)
msg316266 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-05-07 12:28
Due to a python-ideas discussion about reducing boilerplate for __file__-relative path calculations, I was running "./python -X importtime -S -c 'import pathlib'" and noticed three potential candidates where it may be worthwhile deferring the imports until the modules are actually needed:

- re (used in _WildcardSelector)
- fnmatch (used in PurePath.match and _WildcardSelector)
- urllib.parse (used in PurePath.as_uri, by way of self._flavour.make_uri)

Using an optimised Python 3.7 on an SSD with warm disk caches, commenting out those 3 imports reduced my typical import times for pathlib from 12-13 ms to 7-8 ms)
msg316276 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2018-05-07 17:21
Also it's a smaller win, but it might be worth considering whether we can avoid the import of 'nt' on posix platforms and vice-versa.
msg316567 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-14 19:51
Did you mean the idea about module-level alias to dirname(__file__)? I think this is a dead horse. See issue33277.
msg316570 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-14 20:03
Personally I support moving imports of rarely used modules into functions that need them. But this is against PEP 8 and should be discussed more widely. Perhaps this rule should be weaken, we have to state what exceptions are permitted.
History
Date User Action Args
2022-04-11 14:59:00adminsetgithub: 77621
2018-05-14 20:03:34serhiy.storchakasetmessages: + msg316570
2018-05-14 19:51:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg316567
2018-05-14 19:17:51agfsetkeywords: + patch
stage: patch review
pull_requests: + pull_request6505
2018-05-07 17:21:49njssetmessages: + msg316276
2018-05-07 12:28:40ncoghlancreate