]> jfr.im git - yt-dlp.git/commitdiff
[utils] Improve performance using `functools.cache`
authorpukkandan <redacted>
Thu, 19 May 2022 14:06:31 +0000 (19:36 +0530)
committerpukkandan <redacted>
Thu, 19 May 2022 14:53:53 +0000 (20:23 +0530)
Closes #3786

yt_dlp/compat/functools.py [new file with mode: 0644]
yt_dlp/update.py
yt_dlp/utils.py

diff --git a/yt_dlp/compat/functools.py b/yt_dlp/compat/functools.py
new file mode 100644 (file)
index 0000000..36c9836
--- /dev/null
@@ -0,0 +1,12 @@
+# flake8: noqa: F405
+from functools import *  # noqa: F403
+
+from .compat_utils import passthrough_module
+
+passthrough_module(__name__, 'functools')
+del passthrough_module
+
+try:
+    cache  # >= 3.9
+except NameError:
+    cache = lru_cache(maxsize=None)
index 8dcf260f5c6f65c231f35f4d5882a85de647fb7e..d627ae269a6b6c04ed51d72b0690e44ea721c61a 100644 (file)
@@ -7,11 +7,12 @@
 import traceback
 from zipimport import zipimporter
 
 import traceback
 from zipimport import zipimporter
 
-from .compat import compat_realpath
+from .compat import compat_realpath, functools
 from .utils import Popen, encode_compat_str, write_string
 from .version import __version__
 
 
 from .utils import Popen, encode_compat_str, write_string
 from .version import __version__
 
 
+@functools.cache
 def detect_variant():
     if hasattr(sys, 'frozen'):
         prefix = 'mac' if sys.platform == 'darwin' else 'win'
 def detect_variant():
     if hasattr(sys, 'frozen'):
         prefix = 'mac' if sys.platform == 'darwin' else 'win'
index 41157f5de46a9105c9e0d419c25f293fcbab6884..0274e330d3fb6287e1329843154f55a91c2fe98d 100644 (file)
@@ -11,7 +11,6 @@
 import email.header
 import email.utils
 import errno
 import email.header
 import email.utils
 import errno
-import functools
 import gzip
 import hashlib
 import hmac
 import gzip
 import hashlib
 import hmac
@@ -39,8 +38,8 @@
 import xml.etree.ElementTree
 import zlib
 
 import xml.etree.ElementTree
 import zlib
 
+from .compat import asyncio, functools  # Modules
 from .compat import (
 from .compat import (
-    asyncio,
     compat_chr,
     compat_cookiejar,
     compat_etree_fromstring,
     compat_chr,
     compat_cookiejar,
     compat_etree_fromstring,
@@ -248,6 +247,7 @@ def random_user_agent():
 NUMBER_RE = r'\d+(?:\.\d+)?'
 
 
 NUMBER_RE = r'\d+(?:\.\d+)?'
 
 
+@functools.cache
 def preferredencoding():
     """Get preferred encoding.
 
 def preferredencoding():
     """Get preferred encoding.
 
@@ -1883,6 +1883,7 @@ def platform_name():
     return res
 
 
     return res
 
 
+@functools.cache
 def get_windows_version():
     ''' Get Windows version. None if it's not running on Windows '''
     if compat_os_name == 'nt':
 def get_windows_version():
     ''' Get Windows version. None if it's not running on Windows '''
     if compat_os_name == 'nt':
@@ -2079,6 +2080,7 @@ def __iter__(self):
         return iter(self.f)
 
 
         return iter(self.f)
 
 
+@functools.cache
 def get_filesystem_encoding():
     encoding = sys.getfilesystemencoding()
     return encoding if encoding is not None else 'utf-8'
 def get_filesystem_encoding():
     encoding = sys.getfilesystemencoding()
     return encoding if encoding is not None else 'utf-8'
@@ -5092,6 +5094,7 @@ def jwt_decode_hs256(jwt):
     return payload_data
 
 
     return payload_data
 
 
+@functools.cache
 def supports_terminal_sequences(stream):
     if compat_os_name == 'nt':
         from .compat import WINDOWS_VT_MODE  # Must be imported locally
 def supports_terminal_sequences(stream):
     if compat_os_name == 'nt':
         from .compat import WINDOWS_VT_MODE  # Must be imported locally