Skip to content

Possible lazy import opportunities in pathlib #77621

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

Closed
ncoghlan opened this issue May 7, 2018 · 9 comments
Closed

Possible lazy import opportunities in pathlib #77621

ncoghlan opened this issue May 7, 2018 · 9 comments
Labels
3.12 only security fixes performance Performance or resource usage stdlib Python modules in the Lib dir topic-pathlib

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented May 7, 2018

BPO 33440
Nosy @ncoghlan, @pitrou, @njsmith, @serhiy-storchaka
PRs
  • bpo-33440: Defer imports in pathlib to reduce its import time. #6820
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2018-05-07.12:28:40.679>
    labels = ['3.8', 'library', 'performance']
    title = 'Possible lazy import opportunities in `pathlib`'
    updated_at = <Date 2018-05-14.20:03:34.394>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2018-05-14.20:03:34.394>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-05-07.12:28:40.679>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33440
    keywords = ['patch']
    message_count = 4.0
    messages = ['316266', '316276', '316567', '316570']
    nosy_count = 4.0
    nosy_names = ['ncoghlan', 'pitrou', 'njs', 'serhiy.storchaka']
    pr_nums = ['6820']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue33440'
    versions = ['Python 3.8']

    Linked PRs

    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented May 7, 2018

    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)

    @ncoghlan ncoghlan added 3.8 (EOL) end of life stdlib Python modules in the Lib dir performance Performance or resource usage labels May 7, 2018
    @njsmith
    Copy link
    Contributor

    njsmith commented May 7, 2018

    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.

    @serhiy-storchaka
    Copy link
    Member

    Did you mean the idea about module-level alias to dirname(file)? I think this is a dead horse. See bpo-33277.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel added 3.12 only security fixes and removed 3.8 (EOL) end of life labels Sep 7, 2022
    @barneygale
    Copy link
    Contributor

    I've moved the urllib imports into as_uri() / from_uri() as their use is uncommon. Everything else is likely to be hit in general usage, I think.

    @AlexWaygood
    Copy link
    Member

    AlexWaygood commented Nov 18, 2023

    You could probably make this one lazy:

    import warnings

    Users probably shouldn't pay the cost of importing warnings unless they're actually using a deprecated function. There are quite a few uses of warnings in pathlib, though, which is why I held off from changing warnings to be a lazy import in pathlib as part of #110286 -- maybe making it a lazy import in pathlib just adds too much ugliness for it to be worthwhile.

    The import-time speedup from making warnings a lazy import is small, but measurable.

    @barneygale
    Copy link
    Contributor

    Three of four uses of warnings will be removed in 3.14, so perhaps we could look again when main becomes 3.14?

    @AlexWaygood
    Copy link
    Member

    Three of four uses of warnings will be removed in 3.14, so perhaps we could look again when main becomes 3.14?

    SGTM!

    barneygale added a commit to barneygale/cpython that referenced this issue Nov 18, 2023
    Import `contextlib`, `glob` and `re` only as required.
    barneygale added a commit that referenced this issue Nov 25, 2023
    Import `contextlib`, `glob` and `re` only as required.
    
    Co-authored-by: Alex Waygood <[email protected]>
    @barneygale
    Copy link
    Contributor

    Resolving this issue as I believe there's nothing left to do!

    aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
    Import `contextlib`, `glob` and `re` only as required.
    
    Co-authored-by: Alex Waygood <[email protected]>
    Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
    Import `contextlib`, `glob` and `re` only as required.
    
    Co-authored-by: Alex Waygood <[email protected]>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 only security fixes performance Performance or resource usage stdlib Python modules in the Lib dir topic-pathlib
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants