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

gh-118761: Improve import time of mimetypes #126979

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
read_mime_types(file) -- parse one file, return a dictionary or None
"""

import os
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if lazy import of os module is worth it here, it is needed three times and the improvement is small and only in a special case.

Copy link
Member

Choose a reason for hiding this comment

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

Python always imports os and sys at startup. Moving import sys makes sense, it's only used by _main(). But I'm not sure about moving os.

Copy link
Contributor

Choose a reason for hiding this comment

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

In the OP, @hugovk provides timings for when the python is run without the site.py module (python -S), in which case os and sys are not loaded I believe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it makes a difference with python -S, compare the last three images above.

But I'm fine reverting any of these, because I expect with site.py is by far the most usual thing.

import sys
import posixpath
import urllib.parse

try:
from _winapi import _mimetypes_read_windows_registry
except ImportError:
Expand Down Expand Up @@ -119,6 +114,10 @@ def guess_type(self, url, strict=True):
Optional 'strict' argument when False adds a bunch of commonly found,
but non-standard types.
"""
# Lazy import to improve module import time
import os
import urllib.parse

# TODO: Deprecate accepting file paths (in particular path-like objects).
url = os.fspath(url)
p = urllib.parse.urlparse(url)
Expand Down Expand Up @@ -146,13 +145,20 @@ def guess_type(self, url, strict=True):
if '=' in type or '/' not in type:
type = 'text/plain'
return type, None # never compressed, so encoding is None

# Lazy import to improve module import time
import posixpath

return self._guess_file_type(url, strict, posixpath.splitext)

def guess_file_type(self, path, *, strict=True):
"""Guess the type of a file based on its path.

Similar to guess_type(), but takes file path instead of URL.
"""
# Lazy import to improve module import time
import os

path = os.fsdecode(path)
path = os.path.splitdrive(path)[1]
return self._guess_file_type(path, strict, os.path.splitext)
Expand Down Expand Up @@ -399,6 +405,9 @@ def init(files=None):
else:
db = _db

# Lazy import to improve module import time
import os

for file in files:
if os.path.isfile(file):
db.read(file)
Expand Down Expand Up @@ -445,7 +454,7 @@ def _default_mime_types():
}

# Before adding new types, make sure they are either registered with IANA,
# at http://www.iana.org/assignments/media-types
# at https://www.iana.org/assignments/media-types/media-types.xhtml
# or extensions, i.e. using the x- prefix

# If you add to these, please keep them sorted by mime type.
Expand Down Expand Up @@ -637,6 +646,7 @@ def _default_mime_types():

def _main():
import getopt
import sys

USAGE = """\
Usage: mimetypes.py [options] type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve import time of :mod:`mimetypes` by around 11-16 times. Patch by Hugo
van Kemenade.
Loading